Since your $_FILES
is empty, here’s a Checklist for upload of files with PHP
On the server:
Check in your php.ini
whether the following Directives have these values:
file_uploads = On
post_max_size = 100M
upload_max_filesize = 100M
Beware of common errors while setting up the size drive. Note above that it is 100M
and not 100MB
- Make sure you are editing the
php.ini
right. After saving the options and restarting the server check through a phpinfo() if the defined values are those you defined
You may need a file .htaccess
or .user.ini
if you were to use a shared server on which you would not have access to `php.ini
- Ensure that your temporary file directory, defined in the directive
upload_tmp_dir
of php.ini
, and the directory in which you will write the uploaded files have the proper read and write permissions defined
Such directories cannot have spaces like /tmp/my uploads
- It is very rare, especially in your case with small files, but a comment, now extinct, in the PHP Manual, directed to make sure that there is enough disk space for the operation
As for the HTML:
Make sure your form has the attribute enctype="multipart/form-data"
.
GET requests do not support uploads with multipart/form-data
, then your forms should be defined with method="POST"
Such attribute must be in (s) tag(s) <form>
and wrapped in quotes and not backticks that some editors end up accidentally converting.
Make sure you don’t have two or more <input type="file" />
of the same value for the attribute name
. If you need multiple upload, add a pair of brackets at the end of the name:
<input type="file" name="files[]">
<input type="file" name="files[]">
Though obvious, the attribute name
must be present in all fields that must be processed
Remember to close all tags <form>
from your page with the counterparty </form>
Make sure you’re not nesting forms:
<form><form></form></form>
And neither overlapping tags:
<div><form></div></form>
Last but not least, make sure there is no Javascript by disabling the form submission or removing the(s) element(s) <input type="file">
Regarding the end user:
Make sure that the file being sent does not have non-alphinical characters in it
If "asynchronous" upload, Javascript must obviously be enabled
What about the programmer?
Well, you’re getting an E_NOTICE undefined index so you should check if such an index exists.
Give a var_dump() / print_r() in $_FILES and see the structure of the superglobal. It is REALLY one-dimensional?
Quite common in multiple uploads, suddenly what you use as $_FILES['arquivo']['name']
may have an additional numerical index
Resolved the case, to get the file extension you can do with pathinfo(), as it is already doing, but add the predefined constant PATHINFO_EXTENSION as already suggested.
Just keep in mind path to be analyzed end with a point (which for all intents and purposes is a file without a valid extension), pathinfo() return an empty string, but if the file has no extension it will return NULL.
So be careful ;)
Another option would be simple string manipulation:
$ext = ltrim( substr( __FILE__, strrpos( __FILE__, '.' ) ), '.' );
If there is a point in the string, returns the value after it, backwards.
If no, returns the string itself, since strrpos() will fail and the cast automatic PHP will make replace() consider this "failure" as zero and this makes the substring start from the beginning of it.
You can put an output display of 'echo $arquivo_up'
– bruno
It is not an error per se. You are reporting that the variable is null, or $FILES[] contains nothing. Usually appears at pre-checkout times. The form may not send the file if the enctype is not in Multipart/form-data
– Henrique Chaves Domingues