3

I am trying to figure out why this upload script is failing.

Starting with the HTML FORM:

<form role="form" action="api/upload.php" method="post" enctype="multipart/form-data">
  <input id="file" name="file" type="file" />
  <input class="btn btn-primary" type="submit" value="Upload" />
</form>

Here is the PHP script:

<?php
if(isset($_FILES['file'])){
  $file = $_FILES['file'];
  $target_file = basename($_FILES["file"]["name"]);

  $file_name = $file['name'];
  $file_tmp = $file['tmp_name'];
  $file_size = $file['size'];
  $file_error = $file['error'];

  $file_ext = explode('.',$file_name);
  $file_ext = strtolower(end($file_ext));

  $allowed = array('txt', 'jpg', 'xlsx', 'pptx', 'docx', 'doc', 'xls', 'pdf');

  if(in_array($file_ext, $allowed)){
    if($file_error === 0){
      if($file_size <= 99600000){ // this was set to 600000
        $file_name_new = uniqid('', true) . '.' . $file_ext;
        $file_destination = '../files/' . $file_name;

        if(move_uploaded_file($file_tmp, $file_destination)){
          header("Location: ../index.php");
          die();
        }
        else{
          echo "There was a problem uploading the file";
        }
      }
      else{
        echo "The file is too large";
      }
    }
    else{
      echo "There was an error uploading the file";
    }
  }
  else{
    echo "The file type is not allowed";
  }
}
?>

Please forgive the nested IF statement. I was going off of this video on youtube: https://www.youtube.com/watch?v=PRCobMXhnyw

The code above works. I can upload the files, and when an error occurs, I get the appropriate error message.

However, I have come across a file that will not upload. It is an allowed file, a word document, that happens to be 14MB. Not sure if that's too large. But even still, files that I tried to upload that were too large wouldn't get past the file_size check, and I would get the corresponding error message.

In this case, all I get is a blank screen. I can echo 'hello' before and after the initial IF statement, but it fails right after the first IF.

halfer
  • 19,824
  • 17
  • 99
  • 186
John Beasley
  • 2,577
  • 9
  • 43
  • 89
  • Did you try looking at the code in `$_FILES['file']['error']` if its not Zero then it will point you to an error message – RiggsFolly Aug 26 '16 at 13:50
  • I cannot echo anything after the first IF statement. Is there another way to check $file['error']? – John Beasley Aug 26 '16 at 13:51
  • Why can you not echo anything... – RiggsFolly Aug 26 '16 at 13:52
  • Check your PHP upload_max_filesize https://secure.php.net/manual/en/ini.core.php#ini.upload-max-filesize – pr0gramist Aug 26 '16 at 13:53
  • The first TEST on any file upload shoudl be to look at `$_FILES['file']['error']` See the [first user contribution in the Manual Page for an example](http://php.net/manual/en/features.file-upload.php) – RiggsFolly Aug 26 '16 at 13:54
  • Please, check the `post_max_size` value in you php.ini too – yceruto Aug 26 '16 at 13:54
  • I cannot echo anything because something is happening here: if(isset($_FILES['file'])){ – John Beasley Aug 26 '16 at 13:54
  • I just updated the post_max_size to 32M. I am trying 50M. – John Beasley Aug 26 '16 at 13:56
  • No success on updating to 50M. – John Beasley Aug 26 '16 at 13:58
  • A blank page (or a "500 Internal Server Error" status code) means that your script is throwing an error but you haven't configured PHP to display error messages. That's something you need to address before you go further because it's hard to code properly without the aid of error messages. The error reporting thumb rule is to show in development and log in production. As a starting point, I suggest you edit the system-wide `php.ini` file in the computer where you develop and tweak the `error_reporting` and `display_errors` directives ([details here](http://stackoverflow.com/a/5680885/13508)). – Álvaro González Aug 29 '16 at 06:59

1 Answers1

5

You should simply increase the value of upload_max_filesize (by default it's 2M) in your php.ini file (its location depends on your operating system), and restart your web server.

Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34