[英]PHP - Move a file into a different folder on the server
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.我以前在 PHP 中使用过unlink
函数,但后来被告知这可能非常危险且存在安全问题。 (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:然后在那个名为 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
The rename
function does this rename
功能执行此操作
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');
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
上传的文件,这可以防止移动本地文件
docs move_uploaded_file文档 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");
}
}
code snipet from docs来自文档的代码片段
使用rename()函数。
rename("user/image1.jpg", "user/del/image1.jpg");
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));
I using shell read all data file then assign to array.我使用shell读取所有数据文件然后分配给数组。 Then i move file in top position.然后我将文件移动到顶部位置。
i=0
for file in /home/*.gz; do
$file
arr[i]=$file
i=$((i+1))
done
mv -f "${arr[0]}" /var/www/html/
Some solution is first to copy() the file (as mentioned above) and when the destination file exists - unlink() file from previous localization.一些解决方案是首先 copy() 文件(如上所述),当目标文件存在时 - unlink() 来自先前本地化的文件。 Additionally you can validate the MD5 checksum before unlinking to be sure此外,您可以在取消链接之前验证 MD5 校验和以确保
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;
}
use copy() and unlink() function使用copy()和unlink()函数
$moveFile="path/filename";
if (copy($csvFile,$moveFile))
{
unlink($csvFile);
}
Use file this code使用文件此代码
function move_file($path,$to){
if(copy($path, $to)){
unlink($path);
return true;
} else {
return false;
}
}
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");
//echo "--->".basename("$file")."<br>";
}
}
closedir($dir);
rmdir($src);
return true;
}else
return false;
}
shell_exec('mv 文件名 dest_filename');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.