简体   繁体   English

使用PHP将文件从目录移动到另一个文件

[英]Move a file from directory to another using PHP

I have a file on one application folder of codiginitor and I want to copy that file to another application folder of codignitor in another directory. 我在codiginitor的一个应用程序文件夹中有一个文件,我想将该文件复制到另一个目录中的codignitor的另一个应用程序文件夹中。 I have tried below code but it doesn't seem to be working: 我试过下面的代码,但它似乎不起作用:

$file = 'http://xxxxxx/jakson_solar/ftp.php';
$newfile = './mobile_image/transfer/';

if ( copy($file, $newfile) ) {
    echo "Copy success!"; die;
}else{
    echo "Copy failed."; die;
}

is there another way to copy file from one directory to another directory? 还有另一种方法可以将文件从一个目录复制到另一个目录吗?

copy requires as first parameter an input file and as second parameter an output file copy要求输入文件作为第一个参数,输出文件作为第二个参数

So do it this way 所以这样

$file = 'path_to/ftp.php';
$newfile = './mobile_image/transfer/ftp.php';

if ( copy($file, $newfile) ) {
    echo "Copy success!"; die;
} else {
    echo "Copy failed."; die;
}

@dev please find the code below for your needs @dev,请根据需要找到以下代码

// 1 check if your input from the form is not empty
if ((isset($_FILES['file']))){

    // 2. files inside the session
    $_SESSION['FILES'] = $_FILES;

    // 3. your path
    $PATH = $_SERVER["DOCUMENT_ROOT"]."/mobile_image/transfer/";

    // 4. temporary name -> optional step but recommended
    $TEMP_NAME = explode(".",$_FILES["file"]);

    // 5. new name of the file
    $NAME_NEW = substr(number_format(time() * rand(),0,'',''),0,20) . '.' .end($TEMP_NAME);
    $UPLOAD_FILE = $PATH . basename($NAME_NEW);

    // 6. check before moving the file that is not empty
    if (isset($UPLOAD_FILE)) {
        move_uploaded_file($_FILES['file'], $PATH);   }

    // 7. custom errors
    $json = array();
    $json['status'] = "OK";
    $json['message'] = 'File moved successfully';
    echo json_encode($json);

}else{
    // 7. custom errors
    $json = array();
    $json['status'] = "NO FILE";
    $json['message'] = 'No files found';
    echo json_encode($json);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM