PHP上传图片不同的目录

时间:2015-08-05 06:35:35

标签: php

我将图片上传到不同目录时遇到问题。

$path = "../uploads/";
$path2 = "../uploads2/";
$imagename = $_FILES['photoimg']['name'];
$actual_image_name = $imagename;
$uploadedfile = $_FILES['photoimg']['tmp_name'];


$widthArray = array(600,240); //resize width.
foreach($widthArray as $newwidth)
{
  $filename = $uploadedfile,$path,$actual_image_name,$newwidth;

//Original Image
if(move_uploaded_file($uploadedfile, $path.$actual_image_name))
{}

if(move_uploaded_file($uploadedfile, $path2.$actual_image_name))
{}

我想将图片上传到uploads和uploads2文件夹中吗?

例如宽度宽度= 600px到上传,宽度= 240px到文件夹upload2。

我的代码出了什么问题?

2 个答案:

答案 0 :(得分:2)

使用move_uploaded_file移动文件后,$uploadedfile中存储的位置无法使用该文件。对于第二个文件,您必须使用copy函数。

请尝试以下方法:

if(move_uploaded_file($uploadedfile, $path.$actual_image_name))
{}

if(copy($path.$actual_image_name, $path2.$actual_image_name))
{}

答案 1 :(得分:0)

删除此行。我不知道它的目的是什么。

$filename = $uploadedfile,$path,$actual_image_name,$newwidth;

要调整上传图像的大小,请使用任何库调整大小,然后传递它。 但这里是在不同目录中上传的完整代码。但是你必须调整这两个$file1& $file2到您预期的调整大小文件,并将其替换为$file1& $file2 要调整大小,您可以使用任何建议的代码here

    $path1 = "../uploads/";
    $path2 = "../uploads2/";
    $file1= $_FILES['photoimg'];
    $file2= $_FILES['photoimg'];

    $file1_imagename = $file1['name'];
    $file2_imagename = $file2['name'];

    $file1_actual_image_name = $file1_imagename;
    $file2_actual_image_name = $file2_imagename;

    $file1_uploadedfile = $file1['tmp_name'];
    $file2_uploadedfile = $file2['tmp_name'];


    $widthArray = array(600, 240); //resize width.

    if (move_uploaded_file($file1_uploadedfile, $path1 . $file1_actual_image_name)) {
        echo "Uploaded Successfully!";
    }

    if (move_uploaded_file($file2_uploadedfile, $path2 . $file2_actual_image_name)) {
        echo "Uploaded Successfully!";
    }