如何将文件移动到另一个文件夹使用PHP

如何将文件移动到另一个文件夹使用PHP

本文介绍了如何将文件移动到另一个文件夹使用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。基本名($值);

if(move_uploaded_file($ value,$ target_path)){
echoThe file。基本名($值)。 已上传< br />;
} else {
echo上传文件时出现错误,请重试!;
}

} // end foreach

} // end if isset next_page

正在使用的$值的例子是:

lockquote

../ images /uploads/temp/IMG_0002.jpg

正在使用的$ target_path的示例是:

我可以看到临时文件夹中的文件,这两个路径对我来说都很好,我检查确认mkdir函数实际上创建了它的文件夹。



如何使用php将文件移动到另一个文件夹?

当我阅读你的场景时,看起来你已经处理了上传,并将文件移动到你的临时文件夹,现在你想要在文件执行新动作(点击下一步按钮)时移动文件。



就PHP而言 - 你的'temp'中的文件不再是上传的文件,所以你不能再使用move_uploaded_file。

所有你需要做的就是使用:

  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。基本名($值);

if(rename($ value,$ target_path)){
echoThe file。基本名($值)。 已上传< br />;
} else {
echo上传文件时出现错误,请重试!;
}

} // end foreach

} // end if isset next_page


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.

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:

And an example of a $target_path that is being used is:

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.

How can I move a file to another folder using php?

解决方案

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.

All you need to do is use 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

这篇关于如何将文件移动到另一个文件夹使用PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 01:48