PHP - 将文件移动到服务器上的不同文件夹中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19139434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - Move a file into a different folder on the server
提问by odd_duck
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:)
我需要允许我网站上的用户在上传图像后从服务器上删除他们不再需要的图像。我以前unlink
在 PHP 中使用过该函数,但后来被告知这可能非常危险且存在安全问题。(以前的代码如下:)
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:
然后在那个名为 del 的文件夹中,该文件夹是放置不需要的图像的目的地:
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
回答by MKroeders
The rename
function does this
该rename
函数执行此操作
rename('image1.jpg', 'del/image1.jpg');
rename('image1.jpg', 'del/image1.jpg');
If you want to keep the existing file on the same place you should use copy
如果要将现有文件保留在同一位置,则应使用 copy
copy('image1.jpg', 'del/image1.jpg');
copy('image1.jpg', 'del/image1.jpg');
If you want to move an uploaded file use the move_uploaded_file
, although this is almost the same as rename
this function also checks that the given file is a file that was uploaded via the POST
, this prevents for example that a local file is moved
如果您想移动上传的文件,请使用move_uploaded_file
,尽管这与rename
此功能几乎相同,但也会检查给定文件是否是通过 上传的文件POST
,这可以防止例如移动本地文件
$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");
}
}
code snipet from docs
来自文档的代码片段
回答by Ben Fortune
回答by Nabi K.A.Z.
If you want to move the file in new path with keep original file name. use this:
如果要将文件移动到新路径并保留原始文件名。用这个:
$source_file = 'foo/image.jpg';
$destination_path = 'bar/';
rename($source_file, $destination_path . pathinfo($source_file, PATHINFO_BASENAME));
回答by quardas
Some solution is first to copy() the file (as mentioned above) and when the destination file exists - unlink() file from previous localization. Additionally you can validate the MD5 checksum before unlinking to be sure
一些解决方案是首先 copy() 文件(如上所述),当目标文件存在时 - unlink() 来自先前本地化的文件。此外,您可以在取消链接之前验证 MD5 校验和以确保
回答by Marcelo
Create a function to move it:
创建一个函数来移动它:
function move_file($file, $to){
$path_parts = pathinfo($file);
$newplace = "$to/{$path_parts['basename']}";
if(rename($file, $newplace))
return $newplace;
return null;
}
回答by To Quoc Diep
I using shell read all data file then assign to array. Then i move file in top position.
我使用shell读取所有数据文件然后分配给数组。然后我将文件移动到顶部位置。
i=0
for file in /home/*.gz; do
$file
arr[i]=$file
i=$((i+1))
done
mv -f "${arr[0]}" /var/www/html/
回答by Arunprabakaran M
use copy()and unlink()function
使用copy()和unlink()函数
$moveFile="path/filename";
if (copy($csvFile,$moveFile))
{
unlink($csvFile);
}
回答by user10395079
shell_exec('mv filename dest_filename');
shell_exec('mv 文件名 dest_filename');