无法使用$ _FILES过程在PHP中上传图像

时间:2014-12-29 11:57:10

标签: javascript php file-upload

我正在使用PHP $ _FILE过程将图像上传到特定文件夹,但没有任何反应,我尝试过使用一些片段,但似乎没有任何工作,我正在使用PHP和JavaScript来做到这一点,< / p>

这是PHP代码:

<?php

if (isset($_POST['postImage']))
    {
    $validextensions = array(
        "jpeg",
        "jpg",
        "png",
        "gif"
    );
    $temporary = explode(".", $_FILES["postImage"]["name"]);
    $file_extension = end($temporary);
    if ((($_FILES["postImage"]["type"] == "../news/img/.png") || ($_FILES["postImage"]["type"] == "../news/img/.jpg") || ($_FILES["postImage"]["type"] == "../news/img/.jpeg") || ($_FILES["postImage"]["type"] == "../news/img/.gif")) && in_array($file_extension, $validextensions))
        {
        if ($_FILES["postImage"]["error"] > 0)
            {
            echo "Return Code: " . $_FILES["postImage"]["error"] . "<br/><br/>";
            }
          else
            {
            echo "<span>Your File Uploaded Succesfully...!!</span><br/>";
            echo "<br/><b>File Name:</b> " . $_FILES["postImage"]["name"] . "<br />";
            echo "<b>Type:</b> " . $_FILES["postImage"]["type"] . "<br />";
            echo "<b>Temp file:</b> " . $_FILES["postImage"]["tmp_name"] . "<br />";
            if (file_exists("../news/img/" . $_FILES["postImage"]["name"]))
                {
                echo $_FILES["postImage"]["name"] . " <b>already exists.</b> ";
                }
              else
                {
                move_uploaded_file($_FILES["postImage"]["tmp_name"], "../news/img/" . $_FILES["postImage"]["name"]);
                echo "<b>Stored in:</b> " . "../news/img/" . $_FILES["postImage"]["name"];
                }
            }
        }
      else
        {
        echo "<span>***Invalid file Size or Type***<span>";
        }
    }

?>

JavaScript代码:

<script>
        $(document).ready(function() {
        // Function for Preview Image.
        $(function() {
        $(":file").change(function() {
        if (this.files && this.files[0]) {
        var reader = new FileReader();
        reader.onload = imageIsLoaded;
        reader.readAsDataURL(this.files[0]);
        }
        });
        });
        function imageIsLoaded(e) {
        $('#message').css("display", "none");
        $('#preview').css("display", "block");
        $('#previewimg').attr('src', e.target.result);
        };
        // Function for Deleting Preview Image.
        $("#deleteimg").click(function() {
        $('#preview').css("display", "none");
        $('#file').val("");
        });
        // Function for Displaying Details of Uploaded Image.
        $("#submit").click(function() {
        $('#preview').css("display", "none");
        $('#message').css("display", "block");
        });
        });
    </script>

HTML代码:

 <form action="" method="post">  
   <div id="message"></div>

   <input id="postImage" type="file" name="postImage" placeholder="Upload Image" value="">

   <input type="submit">

   <br>
   <br>

   <div id="preview">
      <img id="previewimg" src="">
   </div>
 </form>

2 个答案:

答案 0 :(得分:2)

更改一行:

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

你错过了

enctype="multipart/form-data" 

用于文件上传。

答案 1 :(得分:0)

更改此

if (isset($_POST['postImage']))...
if ((($_FILES["postImage"]["type"] == "../news/img/.png") || ($_FILES["postImage"]["type"] == "../news/img/.jpg") || ($_FILES["postImage"]["type"] == "../news/img/.jpeg") || ($_FILES["postImage"]["type"] == "../news/img/.gif")) && in_array($file_extension, $validextensions)) 

if (isset($_FILE['postImage']))
if ((($_FILES["postImage"]["type"] == "png") || ($_FILES["postImage"]["type"] == "jpg") || ($_FILES["postImage"]["type"] == "jpeg") || ($_FILES["postImage"]["type"] == ".gif")) && in_array($file_extension, $validextensions))