簡體   English   中英

使用PHP將文件從目錄移動到另一個文件

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

我在codiginitor的一個應用程序文件夾中有一個文件,我想將該文件復制到另一個目錄中的codignitor的另一個應用程序文件夾中。 我試過下面的代碼,但它似乎不起作用:

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

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

還有另一種方法可以將文件從一個目錄復制到另一個目錄嗎?

copy要求輸入文件作為第一個參數,輸出文件作為第二個參數

所以這樣

$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,請根據需要找到以下代碼

// 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