猿问

上传多张图片时如何随机生成

我正在尝试在上传多个图像时生成随机名称。我在上传单个图像时已经这样做了,并且图像上传后图像大小也被压缩。我在上传多张图片时尝试做同样的事情。


这是我上传多张图片但名称相同的代码,


$birtimage=$_FILES['birtimage']['name'];     

$birtimage = implode(",",$birtimage);

$pic = "upload/"; 

for($i=0;$i<count($_FILES['birtimage']['name']);$i++){

$target_file = $pic.basename($_FILES["birtimage"]["name"][$i]);

$imageFiletype= pathinfo($target_file,PATHINFO_EXTENSION);

if(move_uploaded_file($_FILES["birtimage"]["tmp_name"][$i], $target_file))

  {

    $msg="The file has been successfully uploaded"; 

  } 

  else{

    $msg="not found image";

  } 

}

这是我的代码,非常适合单个图像,


$extension = pathinfo($_FILES['bday_banner'] ['name'], PATHINFO_EXTENSION);

    $bday_banner = rand(10000,99999) . '.' . $extension;

    $location = "assets/birthday/banner/".$bday_banner;


    if(in_array(strtolower($extension), [ 'png', 'jpeg', 'jpg', ])){

        compressImage($_FILES['bday_banner']['tmp_name'],$location,60);

    }else{

        echo "Invalid file type.";

    }

这里还有在上传单个图像时压缩我的图像的功能,


 function compressImage($source, $destination, $quality) {


            $info = getimagesize($source);


            if ($info['mime'] == 'image/jpeg') 

                $image = imagecreatefromjpeg($source);


            elseif ($info['mime'] == 'image/gif') 

                $image = imagecreatefromgif($source);


            elseif ($info['mime'] == 'image/png') 

                $image = imagecreatefrompng($source);


            imagejpeg($image, $destination, $quality);


        }

我试过这个:


    $extension = pathinfo($_FILES['birtimage'] ['name'], PATHINFO_EXTENSION);

    $birtimage = rand(10000,99999) . '.' . $extension;

    $birtimage = implode(",",$birtimage);

    $location = "upload/".$birtimage;

    for($i=0;$i<count($_FILES['birtimage']['name']);$i++){


    if(in_array(strtolower($extension), [ 'png', 'jpeg', 'jpg', ])){

        compressImage($_FILES['birtimage']['tmp_name'][$i],$location,60);

    }else{

        echo "Invalid file type.";

    }

}


隔江千里
浏览 279回答 3
3回答

慕少森

我想问题也出在html中。在 html 中你应该有这样的东西:<form method="POST"&nbsp; enctype="multipart/form-data">&nbsp; &nbsp; <input type="file" name="birtimage[]" multiple><br>&nbsp; &nbsp; <input type="file" name="birtimage[]" multiple><br>&nbsp; &nbsp; <input type="file" name="birtimage[]" multiple><br>&nbsp; &nbsp; <input type="submit" value="UPLOAD"></form>然后在 PHP 文件中,您可以执行以下操作:if (isset($_FILES) && isset($_FILES['birtimage'])) {&nbsp; &nbsp; $birtimages = $_FILES['birtimage'];&nbsp; &nbsp; $pic = "upload/";&nbsp; &nbsp; for ($i = 0; $i < count($birtimages['name']); $i++) {&nbsp; &nbsp; &nbsp; &nbsp; $extension = pathinfo($birtimages['name'][$i], PATHINFO_EXTENSION);&nbsp; &nbsp; &nbsp; &nbsp; $bday_banner = rand(10000,99999) . '.' . $extension;&nbsp; &nbsp; &nbsp; &nbsp; $location = "assets/birthday/banner/".$bday_banner;&nbsp; &nbsp; &nbsp; &nbsp; if (in_array(strtolower($extension), [ 'png', 'jpeg', 'jpg', ])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; compressImage($birtimages['tmp_name'][$i], $location, 60);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //echo "Invalid file type.";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do what you want to trap error with or without exiting the loop&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}我试过了,这段代码对我有用,没有任何错误。

开心每一天1111

生成随机文件名的一种更安全的方法是使用 PHP&nbsp;tempnam()函数。在指定目录中创建一个具有唯一文件名、访问权限设置为 0600 的文件。如果该目录不存在或不可写,tempnam() 可能会在系统的临时目录中生成一个文件,并返回该文件的完整路径,包括其名称。例子:$tmpfname&nbsp;=&nbsp;tempnam("/path_to_file");

繁星点点滴滴

为此,请在 foreach 循环中使用此代码$temp = explode(".", $_FILES["file"]["name"]);$newfilename = round(microtime(true)) . '.' . end($temp);move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);这里 microtime(true) 将创建随机数。我们将首先获取文件扩展名,无论是 jpg 还是 png 等。然后我们将通过将其加入随机数来重命名该文件,即使您可以将原始文件名与随机数连接起来生成名称以防止重复是个好主意。
随时随地看视频慕课网APP
我要回答