我需要允许我的网站上的用户删除他们的图像从服务器上上传后,如果他们不再想要他们。我以前在PHP中使用unlink函数,但后来被告知这可能是相当危险的和安全问题。(之前的代码如下:)

if(unlink($path.'image1.jpg')){ 
     // deleted
}

相反,我现在想简单地移动文件到一个不同的文件夹。这必须能够在他们第一次上传文件后很长一段时间内完成,所以任何时候他们登录到他们的帐户。如果我有存储用户图像(s)的主文件夹:

user/

然后在这个文件夹中有一个del文件夹,用来存放他们不想要的图片:

user/del/

是否有将文件移动到不同文件夹的命令?所以他们说:

user/image1.jpg

移动/变得

user/del/image1.jpg

当前回答

创建一个函数来移动它:

function move_file($file, $to){
    $path_parts = pathinfo($file);
    $newplace   = "$to/{$path_parts['basename']}";
    if(rename($file, $newplace))
        return $newplace;
    return null;
}

其他回答

function xmove($src,$dst)
{

  if(is_dir($src)===true)
  { $dir=opendir($src);
    while (($file = readdir($dir)) !== false) {
        if ($file === '.' || $file === '..') {
            continue;
        }

        if (is_dir($src."/".$file) === true) {
            if (is_dir("$dst/$file") === false) {
                mkdir("$dst/$file");
            }
            //echo basename("$file")."<br>";
                xmove($src."/".$file,$dst."/".$file);
            
        }
        else {
            copy("$src/$file", "$dst/$file");
            unlink("$src/$file");
            
        }
    }

    closedir($dir);
    rmdir($src);
    return true;

  }else
  return false;

}

Shell_exec ('mv filename dest_filename');

创建一个函数来移动它:

function move_file($file, $to){
    $path_parts = pathinfo($file);
    $newplace   = "$to/{$path_parts['basename']}";
    if(rename($file, $newplace))
        return $newplace;
    return null;
}

使用文件此代码

function move_file($path,$to){
   if(copy($path, $to)){
      unlink($path);
      return true;
   } else {
     return false;
   }
 }

使用copy()和unlink()函数

$moveFile="path/filename";
if (copy($csvFile,$moveFile)) 
{
  unlink($csvFile);
}