将图像上传到多个文件夹php

问题描述 投票:2回答:3

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

$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
3个回答
4
投票

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');
        }

1
投票

上传后,它将删除临时文件( $_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');
    }
}

0
投票

上载到第二个文件夹

if (!move_uploaded_file($target_path, $target_path1)){
     throw new Exception('Could not move 2nd file');
}
© www.soinside.com 2019 - 2024. All rights reserved.