0

I am trying to write a little script in which the user can upload a file but for some reason,I am having issues. For some reason when the submit button is clicked, the _$FILES array is coming back NULL (tested this with var_dump() and var_export()) so of course everything else is skipped and I get the "Error: No file uploaded" I cannot determine why this is. Below is the snippet of HTML for the form as well as the PHP script.

The HTML

<form class='admin_form' action='' method='POST' enctype='multipart/form-data'>
    <fieldset>
       <label for='comic_page'>Filename</label>
       <input type='file' name='uploaded_file' id='uploaded_file'> 
       <input type='submit' name='comic_page_btn' value='Upload'>

       <span id='comic_page_upl_err_msg'>".$err_msg."</span>
    </fieldset>
</form>

The PHP

if(isset($_POST['comic_page_btn'])){

    echo var_dump($_FILES['uploaded_file']['name']).'<br>';
    echo var_export($_FILES["uploaded_file"]);
    if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
        //Check if the file is JPEG image and it's size is less than 350Kb
        $filename = basename($_FILES['uploaded_file']['name']);
        $ext = substr($filename, strrpos($filename, '.') + 1);
        if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && 
          ($_FILES["uploaded_file"]["size"] < 350000)) {
          //Determine the path to which we want to save this file
            $newname = dirname(__FILE__).'/uploads/'.$filename;
            //Check if the file with the same name is already exists on the server
            if (!file_exists($newname)) {
              //Attempt to move the uploaded file to it's new place
              if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
                $err_msg = "The file has been saved as: ".$newname;
              } else {
                 $err_msg = "Error: A problem occurred during file upload!";
              }
            } else {
               $err_msg = "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
            }
        } else {
           $err_msg =  "Error: Only .jpg images under 350Kb are accepted for upload";
        }
      } else {
            $err_msg = "Error: No file uploaded";

      }
}
WeVie
  • 568
  • 2
  • 9
  • 25
  • `echo var_dump($_FILES['uploaded_file']['name']).'
    ';`? Does that work?
    – Popnoodles Jul 13 '14 at 20:07
  • It does echo to the page but both that one as well as `var_export()` are returning null. – WeVie Jul 13 '14 at 20:25
  • The code given looks ok. Did you copy and paste from the code you have, or write (some of) the code in the question while posting? – Popnoodles Jul 13 '14 at 20:31
  • 1
    You form is ok. The error is elsewhere check this answer http://stackoverflow.com/a/3587158/1548376 – David Jacquel Jul 13 '14 at 20:35
  • I suspect the problems lies within that link some where @DavidJacquel. I will look through it and see if I need to make any changes and post what I have or have not found. – WeVie Jul 13 '14 at 21:03
  • I am still stumped on this. After looking through the page that @DavidJacquel posted, I have still not solved the problem. Looking at phpinfo(), file_uploads is set to On, and upload_max_filesize and post_max_size is set to 50M. I have a tmp folder and an uploads folder and their permissions are 777. – WeVie Jul 14 '14 at 00:29
  • You have identify if the problem is browser or server side. Can you have a look in a chrome debuger to see if your form is correct and what are the transmitted file names ? – David Jacquel Jul 14 '14 at 08:06
  • I am not aware of a Chrome PHP debugger. I checked and did see anything about PHP on the console. The whole problem is that the $_FILES array is coming back empty so as far as I can tell, there are no files that are transmitted. – WeVie Jul 15 '14 at 03:23
  • So I finally figured it out. I also had a log out button on the page and even after reading the link that @DavidJacquel posted and then carefully looking over the code, I missed a closing form tag on that log out button form. Closing that up solved the problem. Thanks to DavidJacquel for the link and helping me talk through this. – WeVie Jul 15 '14 at 10:20

0 Answers0