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.

10
  • Did you try looking at the code in $_FILES['file']['error'] if its not Zero then it will point you to an error message Commented Aug 26, 2016 at 13:50
  • I cannot echo anything after the first IF statement. Is there another way to check $file['error']? Commented Aug 26, 2016 at 13:51
  • Why can you not echo anything... Commented Aug 26, 2016 at 13:52
  • Check your PHP upload_max_filesize secure.php.net/manual/en/ini.core.php#ini.upload-max-filesize Commented Aug 26, 2016 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 Commented Aug 26, 2016 at 13:54

1 Answer 1

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.

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

2 Comments

This is a comment and not really an answer
@HoodCoderMan have you restarted your web server?

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.