How can I make this code such that it allows only PNG files to be uploaded? Could someone explain it to me?
if (isset($_FILES["userfile"]) && !empty($_FILES["userfile"])) {
$image = $_FILES['userfile']['tmp_name'];
$imageName = $_FILES['userfile']['name'];
$imageSize = $_FILES['userfile']['size'];
$imageType = $_FILES['userfile']['type'];
$len = count($image);
$path = "uploads/furnipack/images/";
for ($i = 0; $i < $len; $i++) {
if (isset($imageName[$i]) && $imageName[$i] !== NULL) {
if(move_uploaded_file($image[$i], $path.$imageName[$i])) {
$result = $mysqli->query("INSERT INTO imageTable (imageName, imageCategory, imageSize, imageType) VALUES ('$imageName[$i]', '$imageCategory', '$imageSize[$i]' , '$imageType[$i]' )");
$melding = "Item is succesvol geupload!";
}
}
}
}
from the name of the image, get the extension and try to check the image uploaded based on the extension,
$imageName = $_FILES['userfile']['name'];
$str=strpos($imageName,'.');
$ext=substr($imageName,$str+1,strlen($imageName)-$str-1);
if($ext=='png')
{
if(move_uploaded_file($image[$i], $path.$imageName[$i])) {
$result = $mysqli->query("INSERT INTO imageTable (imageName, imageCategory, imageSize, imageType) VALUES ('$imageName[$i]', '$imageCategory', '$imageSize[$i]' , '$imageType[$i]' )");
$melding = "Item is succesvol geupload!";
}
}
you can use this i guess
$ext = pathinfo($imageName, PATHINFO_EXTENSION);
if($ext == "png"){
if(move_uploaded_file($image[$i], $path.$imageName[$i])) {
$result = $mysqli->query("INSERT INTO imageTable (imageName, imageCategory, imageSize, imageType) VALUES ('$imageName[$i]', '$imageCategory', '$imageSize[$i]' , '$imageType[$i]' )");
$melding = "Item is succesvol geupload!";
}
}
else {
echo 'please upload .png images only';
}
我相信这个问题已经回答了有关堆栈溢出了无数次,只需检查图像的延伸和更安全的网站,你可能还需要检查这些页面下面讨论的EXIF数据仅允许只jpg和.png文件 如何我只能在php中上传某些文件类型吗?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.