5

I can't believe I have to ask this, but for some reason my file isn't working. It's called ajax.php (though don't mind the name), and here is the exact code:

<?php
error_reporting(-1);

print_r($_POST);
print_r($_FILES);
?>

<form action="ajax.php" method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <input type="text" name="first" value="Bob" />
    <input type="text" name="middle" value="James" />
    <input type="text" name="last" value="Smith" />
    <input type="file" name="something" />
    <input type="submit" value="Submit" />
</form>

When I submit without attaching a file it prints data in the arrays. When I submit WITH a file no arrays populate.

What obvious thing am I missing???

Without file

Array ( [MAX_FILE_SIZE] => 30000 [first] => Bob [middle] => James [last] => Smith )
Array ( [something] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) )

With File

Array ( )
Array ( )

EXPECTED with File

Array ( [MAX_FILE_SIZE] => 30000 [first] => Bob [middle] => James [last] => Smith )
Array ( [something] => Array ( [name] => sample.jpg [type] => image/jpg [tmp_name] => whatever.jpg [error] => 0 [size] => 1248 ) )

UPDATE

It appears to be working on another server, it's DEFINITELY some configuration with my WAMP, meaning my question was incorrectly asked and therefore I'm closing it. Apologies to anyone who wasted time on my stupidity.

14
  • 2
    It works fine for me! Please post what you get as output and what you expect to get Commented Nov 11, 2014 at 2:02
  • thats really strange! Because it should/ must work! Just for clarity if you don't select a file and submit the form you get arrays and if you select a file and submit the form you get empty arrays? Commented Nov 11, 2014 at 2:10
  • Are you sure you're seeing that AFTER you submit? That's what you would see when you first display the form before submitting. Commented Nov 11, 2014 at 2:13
  • 1
    @Rizier123 That wouldn't cause the parameter variables to be empty. The $_POST parameters should still appear, and there should be something in $_FILES['something']['error'] saying that the file couldn't be uploaded. Commented Nov 11, 2014 at 2:19
  • 1
    @Bing Would recommend you to make an answer and explain what you missed! Because i think another person with the same problem would be happe if he find this answer fast :D Commented Nov 11, 2014 at 2:45

3 Answers 3

2

This appears to be a configuration problem. I'd say the post_max_size is too small. This would explain why the $_POST superglobal is empty when a file is uploaded. From the manual...

If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty.

You need to set this value greater than upload_max_filesize. For example, one of my servers has...

file_uploads=On
upload_max_filesize=12M
post_max_size=20M
Sign up to request clarification or add additional context in comments.

4 Comments

It should display the error besides the empty arrays: Warning: POST Content-Length of 84706071 bytes exceeds the limit of 8388608 bytes in Unknown on line 0.... Maybe that feature is disabled in his server.
@ADASein could be. I've not run in to that error before but I have seen the empty super-globals thing
Indeed it MUST be a config problem (since I am not having this issue on an AWS LAMP server, just my local WAMP one), but I have error reporting turned to the max and it is giving me nothing.
@Bing What does your configuration look like for the properties mentioned in my answer?
0

If you are trying to send uploaded file trough Ajax, you can check my question and answer. Here you can find all the javascript code. How to send data to server while upload file?

You can send your data in 2 times. First upload your file, then inside upload complete, Second put your ajax to send your data, without file upload.

1 Comment

He's not using AJAX, he's using normal form submission. Don't let the name ajax.php confuse you.
0

The issue is with MAX_FILE_SIZE

 <input type="hidden" name="MAX_FILE_SIZE" value="30000" />

The MAX_FILE_SIZE hidden field measured in bytes, so either replace it with some higher value or remove it.

Try it with this

      <form action="ajax.php" method="post" enctype="multipart/form-data">
       <input type="hidden" name="MAX_FILE_SIZE" value="2597152" />
       <input type="text" name="first" value="Bob" />
       <input type="text" name="middle" value="James" />
       <input type="text" name="last" value="Smith" />
       <input type="file" name="something" />
        <input type="submit" value="Submit" />
     </form>

OUTPUT:

  Array
   (
          [MAX_FILE_SIZE] => 2597152
          [first] => Bob
          [middle] => James
          [last] => Smith
   )
 Array
 (
    [something] => Array
      (
           [name] => Desert.jpg
           [type] => image/jpeg
           [tmp_name] => D:\wamp\tmp\php3191.tmp
           [error] => 0
           [size] => 845941
     )

  )

I will suggest you to remove MAX_FILE_SIZE, because it not good practice to interest on user for input. better if you use SERVER side validation for this.

4 Comments

I didn't have it originally, and removing it didn't change anything. I am using localhost (WAMP server)... I wonder if that's it. File uploads are working in other places...
I have also checked this on locahost(WAMP server) and it working. have you tested it with different files having different size?
Does anything actually use the MAX_FILE_SIZE value? I don't think I've ever seen it apply in practice.
Yes, MAX_FILE_SIZE affect file upload, if file size greater than MAX_FILE_SIZE, then it return with error 2. Is this something, you are asking?

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.