The foll snippets explains the code in brief :-
define('UPLOAD_DONE_PATH','');//This comes from config
$uploaddir = '/home/upload';//This comes as functn parameter
$uploaddir_done = $uploaddir.UPLOAD_DONE_PATH;
$uploaddir_done = str_replace("//","/",$uploaddir_done);
$file_array=array();
if (is_dir($uploaddir)) {
if ($dh = opendir($uploaddir)) {
while (($allfile = readdir($dh)) !== false) {
if ($allfile == '.' or $allfile == '..') continue;
{
if (strpos($allfile, 'abc') !== false ) {
$file_array[]=$allfile;
}
}
}
closedir($dh);
}
}
...//some code
foreach ($file_array as $key => $csvfile) {
//Process the file.
//Move the processed file
// Method 1 : Not Working
// copy file from upload to processed folder
if (copy($uploaddir.$csvfile,$uploaddir_done.$csvfile)) {
unlink($uploaddir.$csvfile);
$this->log->info("CSV file is copy into destination folder.".PHP_EOL);
}else{
$this->log->info("CSV file is not copy into destination folder.".PHP_EOL);
}
//Method 2 : Also not working(Move with shell_exec)
$from = $uploaddir.$csvfile;
$fromSpace = $from;
$to = $uploaddir_done.$csvfile;
$toSpace = $to;
$fromSpace = preg_replace('/\s/i', '\\ ', $fromSpace);
$toSpace = preg_replace('/\s/i', '\\ ', $toSpace);
$ch_permissions = 'sudo chmod +755 -R '.$fromSpace;
$mv_cmd = 'mv '.$fromSpace.' '.$toSpace;
$ch_permission_result = shell_exec($ch_permissions);
$file_move_result = shell_exec($mv_cmd);
}
}
In both cases, Moving the file results in error "mv: cannot move ‘/home/upload/abc 09.25.2020.csv’ to ‘/home/upload/processed/abc 09.25.2020.csv’: No such file or directory " It appears that this is due to space in filename.How do i go about resolving this?