2

I'm using PHP $_FILE procedure to upload an image to a specific folder, But nothing happens, I've tried using some snippets aswell but nothing seems to Work, I'm using PHP and JavaScript to do this,

Here's the PHP code:

<?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>";
        }
    }

?>

The JavaScript code:

<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>

the HTML code:

 <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>
3
  • 3
    if (isset($_POST['postImage'])) that should be $_FILES; your conditional is based on that. Plus, you may need a valid enctype in your form. Commented Dec 29, 2014 at 11:58
  • You mean the enctype="multipart/form-data" right! Commented Dec 29, 2014 at 12:00
  • I've tried that already But doesnot work Commented Dec 29, 2014 at 12:01

2 Answers 2

2

Change one line:

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

You missed

enctype="multipart/form-data" 

for file upload.

Sign up to request clarification or add additional context in comments.

8 Comments

Check image details using print_r($_FILES ). whether data coming or not.
that means your $_FILES doesn't have any value.
Sooo, how do I add value to It??? I've tried submiting It, BTW I'm using a live server.
put <input type="submit"/> code inside your <form> </form>. I think you miss that line.
@arqetech ........ $_POST & $_FILES only work when you submit form using submit buton or any other events.
|
0

Change this

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)) 

To

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))

2 Comments

Error is in if (isset($_FILE['postImage']))
please note your errors... write your code into the if (isset($_FILES['postImage'])){}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.