【问题标题】:Upload image on multiple folder php在多个文件夹php上上传图像
【发布时间】:2015-09-10 13:10:04
【问题描述】:

我需要将图片上传到两个不同的文件夹 有我的代码 在第一个文件夹上它正在移动 但是在第二个文件夹上它会生成无法移动第二个文件的异常

$target_path = "uploads/";
$target_path = $target_path . basename($_FILES['image']['name']);


$target_path1 = "thumbnails/";
$target_path1 = $target_path1 . basename($_FILES['image']['name']);

try {
    //throw exception if can't move the file
 if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
        throw new Exception('Could not move file');
    }
 if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path1))
    {
       throw new Exception('Could not move 2nd file');
    }

【问题讨论】:

    标签: php file-upload image-upload


    【解决方案1】:

    move_uploaded_file() 已将文件移至您的$target_path 路径。因此,您的 temp 中没有任何内容,这是您第二次使用 copy()。命令上传。

    if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
        throw new Exception('Could not move file');
    }
    if(!copy ( $target_path , $target_path1 ))
            {  
              throw new Exception('Could not move 2nd file');
            }
    

    【讨论】:

      【解决方案2】:

      上传后,它会删除临时文件 ($_FILES['image']['tmp_name'])。您必须从第一次上传时复制该文件。

      $success=true;
      if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
          throw new Exception('Could not move file');
          $success=false;
      }
      if($success) {
          if (!move_uploaded_file($target_path, $target_path1))
          {
              throw new Exception('Could not move 2nd file');
          }
      }
      

      【讨论】:

        【解决方案3】:

        上传到第二个文件夹

        if (!move_uploaded_file($target_path, $target_path1)){
             throw new Exception('Could not move 2nd file');
        }
        

        【讨论】:

          猜你喜欢
          • 2022-12-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-04-18
          • 2021-09-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多