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

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

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

user/

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

user/del/

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

user/image1.jpg

移动/变得

user/del/image1.jpg

当前回答

如果您想移动文件在新的路径,并保持原来的文件名。用这个:

$source_file = 'foo/image.jpg';
$destination_path = 'bar/';
rename($source_file, $destination_path . pathinfo($source_file, PATHINFO_BASENAME));

其他回答

我使用shell读取所有数据文件,然后分配到数组。 然后我移动文件在顶部的位置。

i=0 
for file in /home/*.gz; do
    $file
    arr[i]=$file
    i=$((i+1)) 
done 
mv -f "${arr[0]}" /var/www/html/

使用rename()函数。

rename("user/image1.jpg", "user/del/image1.jpg");

使用文件此代码

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

创建一个函数来移动它:

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

Shell_exec ('mv filename dest_filename');