![](/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.