我的图片上传存在问题。我已经检查了有关此问题的stackoverflow上的所有帖子,并尝试了人们在评论中推荐的所有内容,但没有任何效果。
大多数图片文件都能正常上传,但有些文件无法正常工作。例如,一些jpg文件工作,一些jpg文件不工作。对于png和所有其他文件扩展名也是如此。
首先我认为这是关于大小,但我增加到500000000的大小,它仍然无法正常工作...所以我想也许它是php.ini中的上传限制。
我试图提高限制并取消注释限制并重新启动apache但它仍然无法正常工作。那么问题是什么?
这是我的上传脚本
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png", "GIF", "JPEG", "JPG", "PNG");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 500000000) &&
in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}else{
$fileName = $temp[0].".".$temp[1];
$temp[0] = rand(0, 3000);
$fileName;
if (file_exists("../../uploads/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
}else{
$newfilename = round(microtime(true)) . '.' . end($temp);
if(move_uploaded_file($_FILES["file"]["tmp_name"], "../../uploads/" . $newfilename)) {
include('../config/dbconf.php');
$title = $_POST['title'];
$content = $_POST['content'];
$sticky = $_POST['sticky'];
$stmt = $conn->prepare("INSERT INTO gallery (img_name, title, content, sticky) VALUES (?, ?, ?, ?)");
$stmt->bind_param("sssi", $newfilename, $title, $content, $sticky);
if($stmt->execute()) {
echo "The file ". basename($_FILES["file"]["name"]). " has been uploaded. You will be redirected back to upload page !";
header('refresh:2; ../upload');
}else{
echo $stmt->error;
}
}
}
}
}else{
echo "Invalid File : " . $_FILES["file"]["name"] . $_FILES["file"]["type"];
}
?>