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.

Bing
  • 3,071
  • 6
  • 42
  • 81
  • 2
    It works fine for me! Please post what you get as output and what you expect to get – Rizier123 Nov 11 '14 at 02: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? – Rizier123 Nov 11 '14 at 02: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. – Barmar Nov 11 '14 at 02:13
  • Maybe your tmp file partition is too small! See http://stackoverflow.com/q/3586919/3933332 Try your skript with a very small file (Do you run your skript on a server?) – Rizier123 Nov 11 '14 at 02:17
  • This works. Try it in another server. Could be some configuration in the one you are running now. –  Nov 11 '14 at 02:17
  • 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. – Barmar Nov 11 '14 at 02:19
  • @Rizier123 Also notice the `MAX_FILE_SIZE=30000` parameter. That will prevent trying to upload huge files. – Barmar Nov 11 '14 at 02:20
  • @Barmar Agreed! But it's a iteresting problem. Also can't reproduce the error. – Rizier123 Nov 11 '14 at 02:21
  • None of us can, there's obviously something he's not showing us. – Barmar Nov 11 '14 at 02:22
  • @Bing Is this the entire code of your skript or do you have other forms? (Maybe with the same names or something in this direction)? – Rizier123 Nov 11 '14 at 02:24
  • @Bing do you get any error's at anytime? or do you just get the empty array? – Rizier123 Nov 11 '14 at 02:32
  • @Bing paste these lines at the top of the skript: `ini_set('post_max_size', '64M'); ini_set('upload_max_filesize', '64M');` – Rizier123 Nov 11 '14 at 02:37
  • There are no errors (nor warnings) and the file is 1-2MB, tops, a simple image. The code is copied in it's entirety. I now suspect it's NOT as obvious as I'd thought (given how much response I've gotten) and it's a WAMP config I missed. – Bing Nov 11 '14 at 02:43
  • 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 – Rizier123 Nov 11 '14 at 02:45

3 Answers3

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
Phil
  • 157,677
  • 23
  • 242
  • 245
  • 2
    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. –  Nov 11 '14 at 02:39
  • @ADASein could be. I've not run in to that error before but I have seen the empty super-globals thing – Phil Nov 11 '14 at 02:45
  • 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 Nov 11 '14 at 02:46
  • @Bing What does your configuration look like for the properties mentioned in my answer? – Phil Nov 11 '14 at 02:48
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.

Community
  • 1
  • 1
IgorAlves
  • 5,086
  • 10
  • 52
  • 83
  • 4
    He's not using AJAX, he's using normal form submission. Don't let the name `ajax.php` confuse you. – Barmar Nov 11 '14 at 02:13
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.

Rajinder Chandel
  • 264
  • 2
  • 11
  • 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... – Bing Nov 11 '14 at 02:29
  • I have also checked this on locahost(WAMP server) and it working. have you tested it with different files having different size? – Rajinder Chandel Nov 11 '14 at 02:32
  • Does anything actually use the `MAX_FILE_SIZE` value? I don't think I've ever seen it apply in practice. – Phil Nov 11 '14 at 02:49
  • 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? – Rajinder Chandel Nov 11 '14 at 04:06