PHP上传图像不起作用

时间:2016-12-20 10:29:24

标签: php html image upload xampp

我有一个新问题。我想将我的网页上的图像上传到服务器上的文件夹(我使用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的例子。

Variable $文本只是在url中看到发生了什么。 该文件的输入字段称为:thumbnail

我想我不会从页面上获取图像,因为当我写这篇文章时:

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

我之间什么都没得到:file:&文本

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

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

以下是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>

1 个答案:

答案 0 :(得分:0)

您错过了&#34; enctype =&#34; multipart / form-data &#34;。

请在此表格中使用此内容。

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

不使用此功能,您无法检索文件数据。

希望这对你有用。