PHP $ _FILES相同的图像名称

时间:2018-06-05 11:35:40

标签: php mysql php-imagine

我正在尝试将多个图像上传到php服务器,但由于某种原因,它使用所有上传图像的第一个图像名称,保存到相同的图像名称。我希望它保存到源文件夹中的文件名。像Banner1.jpg一样,应该保存Banner2.jpg和Banner3.jpg。但它节省了第一张图片三次。

$filesCount = count($_FILES['photos']['name']);
            $success = 0;
            for($i = 0; $i < $filesCount; $i++)
            {
                $uploadedfile = $_FILES['photos']['tmp_name'][$i];

                if($uploadedfile)
                {
                    $filename  = stripcslashes($_FILES['photos']['name'][$i]);
                    $extension = $this->getExtension($filename);
                    $extension = strtolower($extension);

                    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
                    {       
                        $change='<div class="msgdiv">Unknown Image extension </div>';
                    }
                    else
                    {
                        $size = filesize($_FILES['photos']['tmp_name'][$i]);
                    }
                    if($size > MAX_SIZE*1024)
                    {
                        $change='<div class="msgdiv">You have exceeded the size limit!</div> ';
                    }
                    if($extension=="jpg" || $extension=="jpeg" )
                    {
                        $uploadedfile = $_FILES['photos']['tmp_name'][$i];
                        $src = imagecreatefromjpeg($uploadedfile);
                    }
                    else if($extension=="png")
                    {
                        $uploadedfile = $_FILES['photos']['tmp_name'][$i];
                        $src = imagecreatefrompng($uploadedfile);
                    }
                    else 
                    {
                        $src = imagecreatefromgif($uploadedfile);
                    }

                    list($width,$height)=getimagesize($uploadedfile);   
                    $newwidth=1024;
                    $newheight=($height/$width)*$newwidth;
                    $tmp=imagecreatetruecolor($newwidth,$newheight);

                    $newwidth1=300;
                    $newheight1=($height/$width)*$newwidth1;
                    $tmp1=imagecreatetruecolor($newwidth1,$newheight1);

                    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
                    imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);

                    $filenameee = "server/php/rental/". $_FILES['photos']['name'][$i];
                    $filenameee1 = "server/php/rental/small/". $_FILES['photos']['name'][$i];

                    imagejpeg($tmp,$filenameee,100);
                    imagejpeg($tmp1,$filenameee1,100);
                    imagedestroy($src);
                    imagedestroy($tmp);
                    imagedestroy($tmp1);
                    if(mysql_query("INSERT INTO fc_rental_photos(product_image,product_id) VALUES('$filename','$prd_id')"))
                    {
                        $success++;
                    }
                }

这是我用来上传图片的输入字段。

<form id="imageform" method="post" enctype="multipart/form-data" action="/path-to-controller-method/">
<input type="file" name="photos[]" id="photoimg" multiple onchange="imageval();">
</form>

IMageVal

function imageval(){   /*Image size validation*/

      var fi = document.getElementById('photoimg');

       if (fi.files.length > 0) {      // FIRST CHECK IF ANY FILE IS SELECTED.

             for (var i = 0; i <= fi.files.length - 1; i++) {
                 var fileName, fileExtension, fileSize, fileType, dateModified;

                 fileName = fi.files.item(i).name;
                 fileExtension = fileName.replace(/^.*\./, '');
                 if (fileExtension == 'png' || fileExtension == 'jpg' || fileExtension == 'jpeg') {

                    var reader = new FileReader();

                    //Read the contents of Image File.

                    reader.readAsDataURL(fi.files.item(i));

                    reader.onload = function (e) {

                        //Initiate the JavaScript Image object.

                        var image = new Image();

                        //Set the Base64 string return from FileReader as source.

                        image.src = e.target.result;

                        //Validate the File Height and Width.

                        image.onload = function () {

                        var height = this.height;

                        var width = this.width;



                        if (width < 1450 || height < 500) {

                        alert("Image Height and Width should be Above 1450 * 500 px.");

                        return false;

                        }

                        <?php if($aws == 'Yes') echo "uploadImage_aws();";else echo "uploadImage();";?>

                    };
                }
             }
             else
             {
                 alert("Photo only allows file types of PNG, JPG, JPEG. ");
             }

       }


    }
}   

UploadImage

function uploadImage()

{

    $("#imageform").ajaxForm({target: '#preview', 

        beforeSubmit:function(){

            $("#imageloadstatus").show();

            $("#imageloadbutton").hide();

        }, 

        success:function(){

            $("#imageloadstatus").hide();

            $("#imageloadbutton").show();

        },

        error:function(){ 

            $("#imageloadstatus").hide();

            $("#imageloadbutton").show();

        }

    }).submit();

}

1 个答案:

答案 0 :(得分:0)

您必须检查数据是否已存在,如果不存在则重命名。

if(file_exists($new_path)) {
$id = 1;
do {
    $new_path = $upload_folder.$filename.'_'.$id.'.'.$extension;
    $id++;
} while(file_exists($new_path));

}

例如

$new_path = $upload_folder.$filename.'.'.$extension;