207

I need to allow users on my website to delete their images off the server after they have uploaded them if they no longer want them. I was previously using the unlink function in PHP but have since been told that this can be quite risky and a security issue. (Previous code below:)

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

Instead i now want to simply move the file into a different folder. This must be able to be done a long time after they have first uploaded the file so any time they log into their account. If i have the main folder which stores the users image(s):

user/

and then within that a folder called del which is the destination to put their unwanted images:

user/del/

Is there a command to move a file into a different folder? So that say:

user/image1.jpg

moves to/becomes

user/del/image1.jpg
4

10 回答 10

477

rename函数执行此操作

文档重命名

rename('image1.jpg', 'del/image1.jpg');

如果您想将现有文件保留在同一个地方,您应该使用copy

文档副本

copy('image1.jpg', 'del/image1.jpg');

如果您想移动上传的文件,请使用,move_uploaded_file尽管这与此功能几乎相同,但它rename还会检查给定文件是否是通过POST

文档 move_uploaded_file

$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}

文档中的代码片段

于 2013-10-02T14:33:09.057 回答
109

使用重命名()函数。

rename("user/image1.jpg", "user/del/image1.jpg");
于 2013-10-02T14:32:53.313 回答
23

如果要在新路径中移动文件并保留原始文件名。用这个:

$source_file = 'foo/image.jpg';
$destination_path = 'bar/';
rename($source_file, $destination_path . pathinfo($source_file, PATHINFO_BASENAME));
于 2018-01-24T16:38:29.680 回答
2

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

i=0 
for file in /home/*.gz; do
    $file
    arr[i]=$file
    i=$((i+1)) 
done 
mv -f "${arr[0]}" /var/www/html/
于 2019-03-22T09:52:26.177 回答
1

一些解决方案是首先复制()文件(如上所述),当目标文件存在时 - 来自先前本地化的 unlink() 文件。此外,您可以在取消链接之前验证 MD5 校验和以确保

于 2018-11-06T13:25:06.027 回答
1

创建一个函数来移动它:

function move_file($file, $to){
    $path_parts = pathinfo($file);
    $newplace   = "$to/{$path_parts['basename']}";
    if(rename($file, $newplace))
        return $newplace;
    return null;
}
于 2019-03-19T16:49:18.543 回答
0

使用copy()unlink()函数

$moveFile="path/filename";
if (copy($csvFile,$moveFile)) 
{
  unlink($csvFile);
}
于 2019-03-20T10:00:26.843 回答
0

使用文件此代码

function move_file($path,$to){
   if(copy($path, $to)){
      unlink($path);
      return true;
   } else {
     return false;
   }
 }
于 2021-01-17T18:03:58.080 回答
0
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;

}
于 2021-11-01T17:29:50.857 回答
-5

shell_exec('mv 文件名 dest_filename');

于 2018-09-21T06:28:13.327 回答