【问题标题】:file upload in html and php not upload in folderhtml中的文件上传和php不上传到文件夹中
【发布时间】: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.";
    }
}
?>

This are my folders

【问题讨论】:

  • 代码看起来正在做它应该做的事情。你可以试试if(file_exists($target_file) == false) {,但它看起来是合法的。
  • 是的。正如 Kraang Prime 所说,您的代码完全按照其编写的目的执行。您似乎对一一显示的所有这些错误感到困惑。最好在每个错误块中使用die();,这样您就可以确保一旦条件失败,脚本就不会运行。在这里,您试图一次显示所有错误。也许这就是您看到所有这些错误显示的原因。
  • 谢谢!那行得通!现在唯一的问题是他没有把我的图片放在我的 img_upload 文件夹中。有谁知道为什么?
  • 确保img_upload 目录与您的uploadfile.php 位于同一路径,或者您可以明确告诉我们您项目的目录结构。
  • 我在帖子中添加了一个 png 结构

标签: php html file-upload upload


【解决方案1】:

您可以使用 html br 元素或 php exit() / die() 函数将您的错误消息一一分开来处理第一个错误。 File_exists、文件大小和文件格式验证在提交后条件之外执行,因此即使您的表单未提交,您也会看到错误消息。

对于你的第二个问题我认为你的PHP环境找不到路径,所以你需要指定绝对路径。

改变

$target_dir = "img_upload/";

$target_dir = __DIR__ . "/img_upload/";

还要检查上传目录的 chmod 写入权限。
您可以通过在 if(isset($_POST["submit"])) 条件中添加代码来使用 PHP 进行检查:

if (!is_writable(dirname($target_file))) {
    exit("Your upload path is incorrect or has no permissions");
}

无论如何,你可以使用这个最终的 PHP 代码:

<?php
if(isset($_POST["submit"])) {

    $target_dir = __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
    $check = getimagesize($_FILES["art_img"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".<br>";
        $uploadOk = 1;
    } else {
        exit("File is not an image.");
        $uploadOk = 0;
    } 
    // Check if file already exists
    if (file_exists($target_file)) {
        exit("Sorry, file already exists.");
        $uploadOk = 0;
    }
    // Check file size
    if ($_FILES["art_img"]["size"] > 500000) {
        exit("Sorry, your file is too large.");
        $uploadOk = 0;
    }
    // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif" ) {
        exit("Sorry, only JPG, JPEG, PNG & GIF files are allowed.");
        $uploadOk = 0;
    }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        exit("Sorry, your file was not uploaded.");
    // if everything is ok, try to upload file
    } else {

        if (is_writable(dirname($target_file))) {
            if (move_uploaded_file($_FILES["art_img"]["tmp_name"], $target_file)) {
                exit("The file ". basename( $_FILES["art_img"]["name"]). " has been uploaded.");
            } else {
                exit("Sorry, there was an error uploading your file.");
            }
        } else {
            exit("Your upload path is incorrect or has no permissions");
        }
    }

}

?>

【讨论】:

  • 谢谢!改变了它。现在我收到消息“文件是图像 - 图像/png。文件 wolk2.png 已上传。”。所以这很好,但他仍然没有把它放在我的文件夹中。
  • 最后代码有点麻烦,把target_dir ... img_uploasd改成img_upload然后查看目录/img_upload是否存在放置您的uploadfile.php 的目录。
  • 是的,我改变了。我用我的文件夹在帖子中添加了一个 png.. 我认为没关系?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-04
  • 1970-01-01
  • 2016-03-11
  • 1970-01-01
  • 2011-10-02
  • 2015-08-06
相关资源
最近更新 更多