php - 在多个文件夹php上上传图像

标签 php file-upload image-upload

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

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

最佳答案

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

关于php - 在多个文件夹php上上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32502831/

相关文章:

php - laravel php项目"The requested URL was not found on this server"错误

symfony - mp3 文件转换为 mpga

javascript - 无法在 dojo/request/iframe post 上读取未定义的属性 'value'

php - Laravel Blade : passing a variable to a child view

php - 从 sql 结果在 PHP 中为 json 创建一个关联数组

node.js - 如何将文件上传到Cloudinary中的某个文件夹? Node JS

使用新的 FileReader API 和 DataURL 的 Javascript 预览似乎效率低下

php - 将图像上传到服务器并存储在MySQL数据库中

swift - 在 iOS 中将多个图像上传到 FTP 服务器

php - 重复 if/elseif/else 直至完成