我正在尝试上传多个图像时生成随机名称。我在上传单个图像时已经做到了这一点,并且在图像上传后,图像尺寸也得到了压缩。我在上传多张图片时尝试做同样的事情。
这是我的代码,该代码正在上传多张图片,但名称相同,
$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.";
}
}
输出
Array
(
[name] => Array
(
[0] => Ammy.jpg
[1] => banner1.jpg
[2] => banner2.jpg
[3] => banner4.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
[2] => image/jpeg
[3] => image/jpeg
)
[tmp_name] => Array
(
[0] => D:\xampp\tmp\phpBAA2.tmp
[1] => D:\xampp\tmp\phpBAA3.tmp
[2] => D:\xampp\tmp\phpBAA4.tmp
[3] => D:\xampp\tmp\phpBAB4.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
[size] => Array
(
[0] => 19860
[1] => 553916
[2] => 476609
[3] => 515885
)
)
已更新:
$birtimages = $_FILES['birtimage'];
for ($i = 0; $i < count($birtimages); $i++) {
$extension = pathinfo($birtimages[$i]['name'], PATHINFO_EXTENSION);
$birtimages = rand(10000,99999) . '.' . $extension;
$location = "upload/".$birtimages;
if (in_array(strtolower($extension), [ 'png', 'jpeg', 'jpg', ])) {
compressImage($birtimages[$i]['tmp_name'], $location, 60);
} else {
//echo "Invalid file type.";
// do what you want to trap error with or without exiting the loop
}
答案 0 :(得分:0)
为此,请在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等。
然后,通过将文件加入随机数来重命名该文件 甚至您都可以用随机数来连接原始文件名
生成名称是一个好主意,以防止重复。
答案 1 :(得分:0)
一种更安全的生成随机文件名的方法是使用PHP tempnam()函数。
创建具有唯一文件名的文件,并将访问权限设置为 0600,在指定目录中。如果目录不存在或 是不可写的,tempnam()可能会在系统的 临时目录,然后返回该文件的完整路径,包括 它的名字。
$tmpfname = tempnam("/path_to_file");
答案 2 :(得分:0)
我想问题也出在html中。
在html中,您应该具有以下内容:
<form method="POST" enctype="multipart/form-data">
<input type="file" name="birtimage[]" multiple><br>
<input type="file" name="birtimage[]" multiple><br>
<input type="file" name="birtimage[]" multiple><br>
<input type="submit" value="UPLOAD">
</form>
然后在PHP文件中可以执行以下操作:
if (isset($_FILES) && isset($_FILES['birtimage'])) {
$birtimages = $_FILES['birtimage'];
$pic = "upload/";
for ($i = 0; $i < count($birtimages['name']); $i++) {
$extension = pathinfo($birtimages['name'][$i], PATHINFO_EXTENSION);
$bday_banner = rand(10000,99999) . '.' . $extension;
$location = "assets/birthday/banner/".$bday_banner;
if (in_array(strtolower($extension), [ 'png', 'jpeg', 'jpg', ])) {
compressImage($birtimages['tmp_name'][$i], $location, 60);
} else {
//echo "Invalid file type.";
// do what you want to trap error with or without exiting the loop
}
}
}
我尝试过,这段代码对我有用,没有任何错误。