批量上传php图片只上传2或3张图片

时间:2016-09-11 15:56:54

标签: php regex

以下我在另一个例子中使用的代码,当执行它时运行并且没有错误成功,如果我从iPhone(safari浏览器)上的相机胶卷中选择6或8张照片,它的鞋子会进展并上传所有6或8张图像。在uploads目录中,它似乎只有2或3个图像。

我原本以为随机时间线的运行速度超过了毫秒,也许加密产生了完全相同的字符串,但现在我不确定???不幸的是,在Camera Roll中从iPhone获取的所有图像上的文件名似乎是相同的名称??? " Image.jpeg"

修订结果:此错误仅发生在移动iPhone浏览器上?当在桌面上运行时,文件名被附加到图像名称的前面,实际的图像名称出现在html结果中,在iPhone浏览器上,html页面中的图像名称都是image.jpeg而不是实际的图像名称???

<?php
//If directory doesnot exists create it.
$output_dir = "uploads/";

if(isset($_FILES["myfile"]))
{
$ret = array();

$error =$_FILES["myfile"]["error"];
{

    if(!is_array($_FILES["myfile"]['name'])) //single file
    {
        $RandomNum   = md5(date('Y-m-d H:i:s:u'));

        $ImageName      = str_replace(' ','-',strtolower($_FILES['myfile']['name']));
        $ImageType      = $_FILES['myfile']['type']; //"image/png", image/jpeg etc.

        $ImageExt = substr($ImageName, strrpos($ImageName, '.'));
        $ImageExt       = str_replace('.','',$ImageExt);
        $ImageName      = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
        //$NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;
        $NewImageName = $RandomNum.'.'.$ImageExt;

        move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $NewImageName);
        echo "<br> Error: ".$_FILES["myfile"]["error"];

             $ret[$fileName]= $output_dir.$NewImageName;
    }
    else
    {
        $fileCount = count($_FILES["myfile"]['name']);
        for($i=0; $i < $fileCount; $i++)
        {
            $RandomNum   = md5(date('Y-m-d H:i:s:u'));

            $ImageName      = str_replace(' ','-',strtolower($_FILES['myfile']['name'][$i]));
            $ImageType      = $_FILES['myfile']['type'][$i]; //"image/png", image/jpeg etc.

            $ImageExt = substr($ImageName, strrpos($ImageName, '.'));
            $ImageExt       = str_replace('.','',$ImageExt);
            $ImageName      = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
            //$NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;
            $NewImageName = $RandomNum.'.'.$ImageExt;

            $ret[$NewImageName]= $output_dir.$NewImageName;
            move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$NewImageName );
        }
    }
}
echo json_encode($ret);

}

?> 

HTML:

<script>

$(document).ready(function()
{

var settings = {
  url: "upload.php",
  method: "POST",
  allowedTypes:"jpg,png,gif,doc,pdf,zip,jpeg",
  fileName: "myfile",
  multiple: true,
  onSuccess:function(files,data,xhr)
  {
    $("#status").html("<font color='green'>Upload is success</font>");

  },
afterUploadAll:function()
  {
    alert("all images uploaded!!");
  },
onError: function(files,status,errMsg)
  {     
    $("#status").html("<font color='red'>Upload is Failed</font>");
  }
}
$("#mulitplefileuploader").uploadFile(settings);

});
</script>

0 个答案:

没有答案