我制作了一个PHP应用程序,可以将图片上传到服务器。大多数时候它工作正常,但它不会上传一些图片。
在这里,您可以下载所有文件,包括一张有用的图片和一张没有的图片:https://drive.google.com/file/d/0BwoIBS8cNsz4Rjl0eERMb2FndUk/view?usp=sharing
这是我的PHP代码(文件名:upload.php):
<?php
if(isset($_POST["submit"])) {
$target_dir = "slike/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "JPG" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
else{
echo "Sorry, there was an error uploading your file.";
}
?>
这是我的HTML代码(文件名:index.php):
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload" required>
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
我在Windows 8上使用Wamp服务器,但我没有更改任何配置。
(英语不是我的母语,我希望得到更多关于代码的答案作为语法修正。)
答案 0 :(得分:0)
Upload ImageFile is an image - image/jpeg.Sorry, your file is too large.Sorry, your file was not uploaded.
- (5000000/1024 * 2)限制为4.7 MB
这一个不工作.jpg:错误=&gt; $_FILES["fileToUpload"]["size"] > 5000000
图片大小约为8 Mb
将{{1}}更改为更高的
答案 1 :(得分:-1)
我知道这个回应有点过期了。但是对于其他可能遇到类似错误的人。我最近遇到了类似的问题。所以我将5000000(十进制)更改为5242880(二进制),它的作用就像MAGIC。