Php多个图片上传处理错误

时间:2018-06-16 23:38:17

标签: php

在下面的表单和处理文件中,我尝试上传3种尺寸的多张图片上传,并且上传3种尺寸完全正常。

但是它会在所选最后一张图像的所有尺寸上上传相同的图像。

更新

我观察到必须使用$src播放的内容我在下面尝试过,当我这样做时,图像保存为黑色。 $src不接受[$Kv]

foreach($_FILES['file']['tmp_name'] as $src[$Kv]) {

if($extension[$Kv]=="jpg" || $extension[$Kv]=="jpeg" ){
$uploadedfile[$Kv] = $_FILES['file']['tmp_name'][$Kv];
$src[$Kv] = imagecreatefromjpeg($uploadedfile[$Kv]);
}

list($width,$height)=getimagesize($uploadedfile[$Kv]);

////// 1st Size of Image
$newwidth=350;
$newheight=350;
$tmp[$Kv]=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp[$Kv],$src[$Kv],0,0,0,0,$newwidth,$newheight,$width,$height);

}

Suppose I selected 3 images to upload.
Image A
Image B
Image C

它正在转换和上传所有图像的所有尺寸(3x3),但所有选定图像尺寸的图像显示是图像C 的图像。

你可以帮我解决这个问题,我错了吗?

感谢。

form.php的

<form action="process.php" method="post" enctype="multipart/form-data">
<div class="col-lg-12 col-md-9 col-sm-12" id="thumb-output">
<div class="m-dropzone dropzone m-dropzone--primary" id="m-dropzone-two">
    <h3 class="m-dropzone__msg-title">
        Drop files here or click to upload.
    </h3>
<input id="files" class="" type="file" name="file[]" multiple>
</div>
</div>
</form>

process.php

$change="";
$abc="";

    if(count($_FILES['file']['name']) > 0){
        $Kv = 0;

define ("MAX_SIZE","12000");
function getExtension($str) {
        $i = strrpos($str,".");
        if (!$i) { return ""; }
        $l = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);
        return $ext;
}

$errors=0;

if($_SERVER["REQUEST_METHOD"] == "POST"){

    $image =$_FILES["file"]["name"][$Kv];

if ($image) {
    foreach($_FILES['file']['name'] as $filename) {
        $filename = stripslashes($_FILES['file']['name'][$Kv]);
        $extension = getExtension($filename);
        $extension = strtolower($extension);
    }

    foreach($_FILES['file']['size'] as $size) {
    $size=filesize($_FILES['file']['tmp_name'][$Kv]);
    }

if ($size > MAX_SIZE*1024){
    $change='<div class="msgdiv">You have exceeded the size limit!</div> ';
    $errors=1;
}


if($extension=="jpg" || $extension=="jpeg" ){
$uploadedfile = $_FILES['file']['tmp_name'][$Kv];
$src = imagecreatefromjpeg($uploadedfile);

}else if($extension=="png"){
$uploadedfile = $_FILES['file']['tmp_name'][$Kv];
$src = imagecreatefrompng($uploadedfile);

}else {
$src = imagecreatefromgif($uploadedfile);
}
//echo $scr;

list($width,$height)=getimagesize($uploadedfile);
////// 1st Size of Image
$newwidth=350;
$newheight=350;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

////// 2nd Size of Image
$newwidths=400;
$newheights=400;
$tmps=imagecreatetruecolor($newwidths,$newheights);
imagecopyresampled($tmps,$src,0,0,0,0,$newwidths,$newheights,$width,$height);

////// 3rd Size of Image
$newwidthsz=92;
$newheightsz=92;
$tmpsz=imagecreatetruecolor($newwidthsz,$newheightsz);
imagecopyresampled($tmpsz,$src,0,0,0,0,$newwidthsz,$newheightsz,$width,$height);
/////////////////////////////////////////////////////

foreach($_FILES['file']['name'] as $name) {
$name = $_FILES["file"]["name"][$Kv];
$ext = end((explode(".", $name))); # extra () to prevent notice
}

$folderPath = "../images/combo2_images";
if (file_exists($folderPath)){
}else{
mkdir("$folderPath");
}
//mkdir($folderPath);



if ($execute == true) {

    if ($type == 'm') {
        foreach($_FILES['file']['name'] as $filenames) {
        $filenames = "../images/combo2_mimages/m".$idz.'_'.$Kv++.'.'.$ext;
        imagejpeg($tmps,$filenames,100);
        }
        //$savefilenames    =   'sc1_'.$id.'.'.$ext;

        foreach($_FILES['file']['name'] as $filenamesz) {
        $filenamesz = "../images/combo2_mimages/small/m".$idz.'_'.$Kv++.'.'.$ext;
        imagejpeg($tmpsz,$filenamesz,100);
        }
        //$savefilenamesz   =   'sc1_'.$id.'.'.$ext;
    }


$filename = "../images/combo2_images/".$idz.'.'.$ext;
imagejpeg($tmp,$filename,100);
$savefilename   =   $idz.'.'.$ext;


if ($_FILES['file']['size'] !== 0 && $_FILES['file']['error'] == 0) {

    if ($type == 'm') {
        $querys =   "insert into items_images
        (combo_type, combo_id, item_id, filename, status) 
        values (2, '$idz', '$mitem', '$savefilename', '$status')
        ";
        $executes   =   $dba->query($querys);
    }


$queryu =   "update items_combobox2
set filename = '$savefilename'
where id = '$idz'
";
$executeu   =   $dba->query($queryu);

imagedestroy($src);
imagedestroy($tmp); ////// 1st Size of Image
imagedestroy($tmps); ////// 2nd Size of Image
imagedestroy($tmpsz); ////// 3rd Size of Image
}
}


}


header("location: all/items/");
echo "<script>parent.document.location.href = 'all/items/';</script>";
exit();
}
}

1 个答案:

答案 0 :(得分:0)

最后,我得到了我想要的工作。

if(count($_FILES['file']['name']) > 0){

define ("MAX_SIZE","12000"); 
function getExtension($str)
{
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}

$errors=0;

$i=1; // auto increment number

  $uploaddir = "../images/combo2_images"; //a directory inside
    foreach ($_FILES['file']['name'] as $name => $value) {

        $filename = stripslashes($_FILES['file']['name'][$name]);
     //get the extension of the file in a lower case format
         $extension = getExtension($filename);
         $extension = strtolower($extension);
        echo "\n This is the extension: ",$extension;
         if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
        //print error message
        ?>
           <h4>Unknown extension!</h4>
          <?php
            $errors=1;
} else {
            $size=filesize($_FILES['file']['tmp_name'][$name]);
            if ($size > MAX_SIZE*1024) {
            ?>
               <h4>You have exceeded the size limit!</h4>
          <?php
            $errors=1;
            }

if($extension=="jpg" || $extension=="jpeg" && $extension!=="png"){
$uploadedfile = $_FILES['file']['tmp_name'][$name];
$src = imagecreatefromjpeg($uploadedfile);
}else if($extension=="jpg" || $extension=="jpeg" && $extension=="png"){
$uploadedfile = $_FILES['file']['tmp_name'][$name];
$src = imagecreatefromjpeg($uploadedfile);
}else if($extension!=="jpg" || $extension!=="jpeg" && $extension=="png"){
$uploadedfile = $_FILES['file']['tmp_name'][$name];
$src = imagecreatefrompng($uploadedfile);
}


list($width,$height)=getimagesize($uploadedfile);

$newwidth=350;
$newheight=350;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

$newwidths=400;
$newheights=400;
$tmps=imagecreatetruecolor($newwidths,$newheights);
imagecopyresampled($tmps,$src,0,0,0,0,$newwidths,$newheights,$width,$height);

$newwidthsz=92;
$newheightsz=92;
$tmpsz=imagecreatetruecolor($newwidthsz,$newheightsz);
imagecopyresampled($tmpsz,$src,0,0,0,0,$newwidthsz,$newheightsz,$width,$height);


            //$image_name=($i++).$filename.'.'.$extension;
            //$newname="files/".$image_name;
            //$copied = copy($_FILES['file']['tmp_name'][$name], $newname);

if ($execute == true) {

if ($type == 'm') {
$filenames = "../images/combo2_mimages/m".$idz.'.'.$extension;
//imagejpeg($tmps,$filenames,100);
$copied1 = copy(imagejpeg($tmps,$filenames,100));

$filenamesz = "../images/combo2_mimages/small/m".$idz.'_'.($i++).'.'.$extension;
//imagejpeg($tmpsz,$filenamesz,100);
$copied2 = copy(imagejpeg($tmpsz,$filenamesz,100));
}


$filename = "../images/combo2_images/".$idz.'.'.$extension;
imagejpeg($tmp,$filename,100);
$savefilename=$idz.'.'.$extension;

if ($type == 'm') {
$querys="insert into items_images
(combo_type, combo_id, item_id, filename, status) 
values (2, '$idz', '$mitem', '$savefilename', '$status')
";
$executes=$dba->query($querys);
}

$queryu="update items_combobox2
set filename = '$savefilename'
where id = '$idz'
";
$executeu=$dba->query($queryu);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmps);
imagedestroy($tmpsz);
}


            if (!$copied) {
                ?>
                <h4>Copy unsuccessfull!</h4>
                <?php
                $errors=1;
            }
        }
    }

header("location: all/items/");
echo "<script>parent.document.location.href = 'all/items/';</script>";
exit();
}