PHP多图像上传,检查大小和类型(PHP multiple image upload with checking size and type)

编程入门 行业动态 更新时间:2024-10-24 12:20:33
PHP多图像上传,检查大小和类型(PHP multiple image upload with checking size and type)

我设计了一个表单来使用PHP上传多个图像,我想检查图像的类型和大小。 但是我的代码无法成功运行。 我该如何更正代码?

HTML输入文件(文件上传的一部分):

<input type="file" class="test" name="file_array[]"> <input type="file" class="test" name="file_array[]">

PHP代码

<?php if (isset($_FILES['file_array'])) { $name_array = $_FILES['file_array']['name']; $tmp_name_array = $_FILES['file_array']['tmp_name']; $type_array = $_FILES['file_array']['type']; $size_array = $_FILES['file_array']['size']; $allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF); $detectedType = exif_imagetype($tmp_name_array); $checktype = in_array($detectedType, $allowedTypes); } if ($checktype == false) { echo "type error"; echo "</br>"; } elseif ($size_array > 2097152) { echo "type error"; echo "</br>"; } else { for ($i = 0; $i < count($tmp_name_array); $i++) { if (move_uploaded_file( $tmp_name_array[$i], "upload/" . $name_array[$i] ) ) { echo $name_array[$i] . " upload is complete<br>"; } else { echo "function failed for " . $name_array[$i] . "<br>"; } } } ?>

I design a form to upload multiple image using PHP and i want to check the type and size of image. But my code won't work successfully. How can I correct the code?

HTML input file(part of file upload):

<input type="file" class="test" name="file_array[]"> <input type="file" class="test" name="file_array[]">

PHP code

<?php if (isset($_FILES['file_array'])) { $name_array = $_FILES['file_array']['name']; $tmp_name_array = $_FILES['file_array']['tmp_name']; $type_array = $_FILES['file_array']['type']; $size_array = $_FILES['file_array']['size']; $allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF); $detectedType = exif_imagetype($tmp_name_array); $checktype = in_array($detectedType, $allowedTypes); } if ($checktype == false) { echo "type error"; echo "</br>"; } elseif ($size_array > 2097152) { echo "type error"; echo "</br>"; } else { for ($i = 0; $i < count($tmp_name_array); $i++) { if (move_uploaded_file( $tmp_name_array[$i], "upload/" . $name_array[$i] ) ) { echo $name_array[$i] . " upload is complete<br>"; } else { echo "function failed for " . $name_array[$i] . "<br>"; } } } ?>

最满意答案

您正在将数组传递给exif_imagetype() ,它应该是一个带有文件名的字符串,对于您的示例,您应该遍历数组或使用以下内容:

// Get type from first image $detectedTypeImage1 = exif_imagetype($tmp_name_array[0]); $detectedTypeImage2 = exif_imagetype($tmp_name_array[1]);

然后你错误地将array与int进行比较

$size_array > 2097152

你可以像我给的第一个例子那样做,或者你可以在数组中使用循环,如:

foreach($size_array as $imageSize){ if($imageSize > 2097152){ echo "type error SIZE"; echo "<br>"; } }

完整的例子

/** * Reorder the uploaded files array to simply the use of foreach * * @param array $file_post * @return array */ function reArrayFiles(&$file_post) { $file_ary = array(); $file_count = count($file_post['name']); $file_keys = array_keys($file_post); for ($i=0; $i<$file_count; $i++) { foreach ($file_keys as $key) { $file_ary[$i][$key] = $file_post[$key][$i]; } } return $file_ary; } // Array reArranged $file_ary = reArrayFiles($_FILES['file_array']); $allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF); // For each file... foreach($file_ary as $key => $file){ static $i = 1; if($file['error'] == 4){ echo 'No file uploaded on field '.$i++.'.<br>'; continue; } $errors = false; // Check type, if not allowed tell's you wich one have the problem if(!in_array(exif_imagetype($file['tmp_name']), $allowedTypes)){ echo '<span style="color:red">Filename '.$file['name'].' <b>type</b> is not allowed.</span><br>'; $errors = true; } // Check size, if exceed allowed size tell's you wich one have the problem if($file['size'] > 2097152){ echo '<span style="color:red">Filename '.$file['name'].' exceeds maximum allowed <b>size</b></span>.<br>'; $errors = true; } // If we don't have errors let's upload files to the server if(!$errors){ if (move_uploaded_file( $file['tmp_name'], "../upload/" . $file['name']) ){ echo $file['name'] . " upload is complete<br>"; }else{ echo "Uploaded failed for " . $file['name'] . "<br>"; } } $i++; }

别忘了使用is_uploaded_file() 。

You are passing an array to exif_imagetype(), it should be a string with filename, for your example, you should iterate through your array or use something like:

// Get type from first image $detectedTypeImage1 = exif_imagetype($tmp_name_array[0]); $detectedTypeImage2 = exif_imagetype($tmp_name_array[1]);

Then you are wrongly compare an array with an int

$size_array > 2097152

you could do like the first example I gave or you could use a loop through the array, like:

foreach($size_array as $imageSize){ if($imageSize > 2097152){ echo "type error SIZE"; echo "<br>"; } }

Full example

/** * Reorder the uploaded files array to simply the use of foreach * * @param array $file_post * @return array */ function reArrayFiles(&$file_post) { $file_ary = array(); $file_count = count($file_post['name']); $file_keys = array_keys($file_post); for ($i=0; $i<$file_count; $i++) { foreach ($file_keys as $key) { $file_ary[$i][$key] = $file_post[$key][$i]; } } return $file_ary; } // Array reArranged $file_ary = reArrayFiles($_FILES['file_array']); $allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF); // For each file... foreach($file_ary as $key => $file){ static $i = 1; if($file['error'] == 4){ echo 'No file uploaded on field '.$i++.'.<br>'; continue; } $errors = false; // Check type, if not allowed tell's you wich one have the problem if(!in_array(exif_imagetype($file['tmp_name']), $allowedTypes)){ echo '<span style="color:red">Filename '.$file['name'].' <b>type</b> is not allowed.</span><br>'; $errors = true; } // Check size, if exceed allowed size tell's you wich one have the problem if($file['size'] > 2097152){ echo '<span style="color:red">Filename '.$file['name'].' exceeds maximum allowed <b>size</b></span>.<br>'; $errors = true; } // If we don't have errors let's upload files to the server if(!$errors){ if (move_uploaded_file( $file['tmp_name'], "../upload/" . $file['name']) ){ echo $file['name'] . " upload is complete<br>"; }else{ echo "Uploaded failed for " . $file['name'] . "<br>"; } } $i++; }

Don't forget to use is_uploaded_file() too.

更多推荐

PHP,type,upload,code,电脑培训,计算机培训,IT培训"/> <meta name="descript

本文发布于:2023-07-15 19:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117708.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多图   大小   类型   上传   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!