简体   繁体   中英

How to move files to another folder in php

I have 4 csv files in "files" folder & I want to move these files with content to another folder (ie backups),Files are given below.

1) abc.csv 2) abc1.csv 3) abc2.csv

 $files = scandir('files'); $destination = 'backups/'; $date = date('Ym-d'); foreach($files as $file){ $rename_file = $file.'_'.$date; move_uploaded_file($rename_file, "$destination"); } 

Since you are not uploading any files, try rename() function instead.

$Ignore = array(".","..","Thumbs.db");
$OriginalFileRoot = "files";
$OriginalFiles = scandir($OriginalFileRoot);
$DestinationRoot = "backups";
# Check to see if "backups" exists
if(!is_dir($DestinationRoot)){
    mkdir($DestinationRoot,0777,true);
}
$Date = date('Y-m-d');
foreach($OriginalFiles as $OriginalFile){
    if(!in_array($OriginalFile,$Ignore)){
        $FileExt = pathinfo($OriginalFileRoot."\\".$OriginalFile, PATHINFO_EXTENSION); // Get the file extension
        $Filename = basename($OriginalFile, ".".$FileExt); // Get the filename
        $DestinationFile = $DestinationRoot."\\".$Filename.'_'.$Date.".".$FileExt; // Create the destination filename 
        rename($OriginalFileRoot."\\".$OriginalFile, $DestinationFile); // rename the file            
    }
}

The function move_uploaded_file is relevant for uploading files, not for other things.

To move file in the filesystem you should use the rename function:

$files = scandir('files');
$destination = 'backups/';
$date = date('Y-m-d');
foreach($files as $file){
    if (!is_file($file)) {
        continue;
    }
    $rename_file = $destination.$file.'_'.$date;
    rename($file, $rename_file);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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