我有一个上传表单,用户可以将当前正在上传的图像上传到我称为“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
正在使用的$值的示例是:
../images/uploads/temp/IMG_0002.jpg
并且正在使用的$target_path的示例是:
../images/uploads/listers/186/IMG_0002.jpg
当我阅读你的场景,看起来你已经处理了上传,并将文件移动到’temp’文件夹,现在你想移动文件,当他们执行一个新的动作(点击下一步按钮).
就PHP而言,’temp’中的文件不再是上传的文件,所以你不能再使用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,please try again!"; } } //end foreach } //end if isset next_page