【发布时间】:2017-01-05 18:21:52
【问题描述】:
我在上传文件夹中的文件时遇到问题。当我运行代码时,我收到消息:“文件是图像 - image/jpeg.对不起,文件已经存在。对不起,您的文件没有上传。”但这不是我之前上传的文件。”
我的第二个问题是他没有把我的图片放在我的文件夹里。
这是我的 uxu.php
<div class="overlay-content">
<form action="../uploadfile.php" method="post" enctype="multipart/form-data" class="overlay-form">
<h1>Upload gemaakt werk</h1><br><br>
<label>Titel</label>
<input type="text" class="aa-field"><br><br>
<label>Text</label>
<textarea rows="4" class="aa-area"> </textarea><br><br>
<label>Foto</label>
<input class="aa-file" type="file" name="art_img" id="fileToUpload"><br><br>
<input type="submit" name="submit" class="btn-upload-article" value="Upload">
</form>
</div>
这是我的上传文件.php
<?php
$target_dir = "img_upload/";
$target_file = $target_dir . basename($_FILES["art_img"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["art_img"]["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["art_img"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($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["art_img"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["art_img"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
【问题讨论】:
-
代码看起来正在做它应该做的事情。你可以试试
if(file_exists($target_file) == false) {,但它看起来是合法的。 -
是的。正如 Kraang Prime 所说,您的代码完全按照其编写的目的执行。您似乎对一一显示的所有这些错误感到困惑。最好在每个错误块中使用
die();,这样您就可以确保一旦条件失败,脚本就不会运行。在这里,您试图一次显示所有错误。也许这就是您看到所有这些错误显示的原因。 -
谢谢!那行得通!现在唯一的问题是他没有把我的图片放在我的 img_upload 文件夹中。有谁知道为什么?
-
确保
img_upload目录与您的uploadfile.php 位于同一路径,或者您可以明确告诉我们您项目的目录结构。 -
我在帖子中添加了一个 png 结构
标签: php html file-upload upload