php 如何使用php将文件移动到另一个文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8206011/
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
How can I move a file to another folder using php?
提问by zeckdude
I have an upload form where users can upload images that are currently being uploaded to a folder I made called 'temp' and their locations are saved in an array called $_SESSION['uploaded_photos']. Once the user pushes the 'Next Page' button, I want it to move the files to a new folder that is dynamically created right before that.
我有一个上传表单,用户可以在其中上传当前正在上传到我创建的名为“temp”的文件夹中的图像,并且他们的位置保存在名为 $_SESSION['uploaded_photos'] 的数组中。一旦用户按下“下一页”按钮,我希望它将文件移动到在此之前动态创建的新文件夹中。
if(isset($_POST['next_page'])) {
if (!is_dir('../images/uploads/listers/'.$_SESSION['loggedin_lister_id'])) {
mkdir('../images/uploads/listers/'.$_SESSION['loggedin_lister_id']);
}
foreach($_SESSION['uploaded_photos'] as $key => $value) {
$target_path = '../images/uploads/listers/'.$_SESSION['loggedin_lister_id'].'/';
$target_path = $target_path . basename($value);
if(move_uploaded_file($value, $target_path)) {
echo "The file ". basename($value). " has been uploaded<br />";
} else{
echo "There was an error uploading the file, please try again!";
}
} //end foreach
} //end if isset next_page
An example for a $value that is being used is:
正在使用的 $value 的示例是:
../images/uploads/temp/IMG_0002.jpg
../images/uploads/temp/IMG_0002.jpg
And an example of a $target_path that is being used is:
正在使用的 $target_path 示例是:
../images/uploads/listers/186/IMG_0002.jpg
../images/uploads/listers/186/IMG_0002.jpg
I can see the file sitting in the temp folder, both of those paths look good to me and I checked to make sure that the mkdir function actually created the folder which it did well.
我可以看到位于临时文件夹中的文件,这两个路径对我来说都很好,我检查以确保 mkdir 函数实际上创建了它运行良好的文件夹。
How can I move a file to another folder using php?
如何使用php将文件移动到另一个文件夹?
回答by HorusKol
As I read your scenario, it looks like you've handled the upload and moved the files to your 'temp' folder, and now you want to move the file when they perfom a new action (clicking on the Next button).
当我阅读您的方案时,您似乎已经处理了上传并将文件移动到您的“临时”文件夹,现在您想在他们执行新操作时移动文件(单击“下一步”按钮)。
As far as PHP is concerned - the files in your 'temp' are not uploaded files anymore, so you can no longer use move_uploaded_file.
就 PHP 而言 - 您的“临时”中的文件不再是上传文件,因此您不能再使用 move_uploaded_file。
All you need to do is use rename:
您需要做的就是使用rename:
if(isset($_POST['next_page'])) {
if (!is_dir('../images/uploads/listers/'.$_SESSION['loggedin_lister_id'])) {
mkdir('../images/uploads/listers/'.$_SESSION['loggedin_lister_id']);
}
foreach($_SESSION['uploaded_photos'] as $key => $value) {
$target_path = '../images/uploads/listers/'.$_SESSION['loggedin_lister_id'].'/';
$target_path = $target_path . basename($value);
if(rename($value, $target_path)) {
echo "The file ". basename($value). " has been uploaded<br />";
} else{
echo "There was an error uploading the file, please try again!";
}
} //end foreach
} //end if isset next_page