【问题标题】:PHP Upload an Image does not workPHP上传图片不起作用
【发布时间】:2017-05-05 12:59:18
【问题描述】:

我有一个新问题。我想将网页中的图片上传到服务器上的文件夹中(我正在使用 XAMPP)。

我尝试了很多来自 php.net 和 w3schools.com 的例子,但它不起作用,我不知道为什么。 -_-

这是我现在使用的代码:

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

这是来自 w3schools 的示例。

变量 $text 仅用于在 url 中查看发生了什么。 文件的输入字段称为:缩略图

我想我没有从页面中获得图像,因为当我写这个时:

$text = $text . "Sorry, there was an error uploading your file: ". basename( $_FILES["thumbnail"]["name"]). " Text ";

我什么都没有:文件:和文本

我希望有人可以帮助我解决我的问题。

====================== 编辑======================

这是 HTML 内容:

<form id="postform" name="postform" action="?track=7" method="POST">
    <label for="thumbnail" class="forinput">Thumbnail <span class="info">Das Bild wir auf 250px : 250px skaliert</span></label>
    <input class="addMovieFormFile" id="thumbnail" type="file" name="thumbnail">
    <input type="submit" value="FILM HINZUFÜGEN" class="button">
</form>

【问题讨论】:

  • 向我们展示您的 html 内容
  • 发布您的 html 表单
  • @Blueblazer172 我在编辑部分做了这个:-)

标签: php html image upload xampp


【解决方案1】:

您缺少“enctype="multipart/form-data”。

请像这样在您的表单中使用它。

<form id="postform" name="postform" action="?track=7" method="POST" enctype="multipart/form-data">

如果不使用它,您将无法检索文件数据。

希望这对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    • 2013-06-23
    • 2014-06-21
    • 1970-01-01
    相关资源
    最近更新 更多