简体   繁体   English

PHP映像上传未将映像上传到目标目录

[英]PHP Image Upload Is Not Uploading Image to target directory

Problem: 问题:

I have created a upload.php file, which contains a HTML form and the PHP code needed to upload images and I use XAMPP localhost: 我创建了一个upload.php文件,其中包含HTML表单和上传图像所需的PHP代码,并且我使用XAMPP本地主机:

htdocs/web2/assets/upload.php

I want to upload images to the folder upload in the same directory: 我想将图像upload到同一目录中的文件夹upload中:

htdocs/web2/assets/uploads/

Script: 脚本:

    <?php 

        <form action="?" method="post" enctype="multipart/form-data">   

            <!--wrap input button as around pre-existing image -->   
            <?php
                echo '<label class="profile_gallery_image_in"><input type="file" name="fileToUpload" id="fileToUpload" onchange="form.submit()"/><p class="label"></p><img class="myImg" src='.$image.' height="100%" width="100%" /></label>';
            ?>

        </form>

    <!-- Commence Photo Upload -->

    <?php
        $target_dir = "uploads/";
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $uploadOk = 1;
        $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
        // Check if image file is a actual image or fake image
        if(isset($_POST["submit"])) {
            $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;
            }
        }
    ?>

The form is set to auto-submit upon input type change. 表单设置为在输入类型更改时自动提交。 This appears to be working fine and the form is submitting. 这似乎工作正常,正在提交表单。

However, I get no actual image uploaded and no error at all. 但是,我没有上传任何实际图像,也没有任何错误。

How do I solve this problem? 我该如何解决这个问题?

There seems to be an error in the code. 代码中似乎有错误。 You can use the below code for image uploading. 您可以使用以下代码上传图片。

 <?php if(isset($_FILES['image'])){ $errors= array(); $dir = "images/"; $file_name = $_FILES['image']['name']; $file_name = $dir. $file_name; $file_size = $_FILES['image']['size']; $file_tmp = $_FILES['image']['tmp_name']; $file_type = $_FILES['image']['type']; $tmp = explode('.',$_FILES['image']['name']); $file_ext=strtolower(end($tmp)); $extensions= array("jpeg","jpg","png","gif"); if(in_array($file_ext,$extensions)=== false){ $errors[]="extension not allowed, please choose a GIF, JPEG or PNG file."; } if($file_size > 2097152) { $errors[]='File size must be excately 2 MB'; } if(empty($errors)==true) { move_uploaded_file($file_tmp, $file_name); echo "Success"; }else{ print_r($errors); } } ?> <html> <body> <form action = "" method = "POST" enctype = "multipart/form-data"> <input type = "file" name = "image" /> <input type = "submit"/> </form> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM