![](/img/trans.png)
[英]How can I move an email message from one folder to another using PHP imap
[英]How can I move a file to another folder using php?
我有一个上传表单,用户可以将当前正在上传的图像上传到我创建的名为“ 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
一个使用$ value的示例是:
../images/uploads/temp/IMG_0002.jpg
一个正在使用的$ target_path的示例是:
../images/uploads/listers/186/IMG_0002.jpg
我可以看到文件位于temp文件夹中,这两个路径对我来说看起来都很不错,并且我检查了mkdir函数是否确实创建了性能良好的文件夹。
如何使用php将文件移动到另一个文件夹?
在我阅读您的方案时,您似乎已经处理了上载并将文件移到“ temp”文件夹中,现在您想在执行新操作时移动文件(单击“下一步”按钮)。
就PHP而言-“临时”中的文件不再是上传文件,因此您不能再使用move_uploaded_file。
您需要做的就是使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.