Having url rewrite patterns in .htaccess file which modify your urls can affect $_FILES sometimes. Even though the php page loads and works fine, this variable may not work because of it. Therefore if you rewrite 'www.example.com' to 'example.com', make sure you use the latter one when sending POST to the php page. I'm still not sure why this happens, but its worth noting here so others don't spend time chasing ghosts.
$_FILES
$HTTP_POST_FILES [obsoleta]
$_FILES -- $HTTP_POST_FILES [obsoleta] — Variables de Carga de Archivos HTTP
Descripción
Una variable tipo array asociativo de elementos cargados al script actual a través del método POST.
$HTTP_POST_FILES contiene la misma información inicial, pero no es una superglobal. (Note que $HTTP_POST_FILES y $_FILES son variables diferentes y que PHP las trata como tal)
Historial de cambios
| Versión | Descripción |
|---|---|
| 4.1.0 | Se introdujo $_FILES, haciendo $HTTP_POST_FILES obsoleta. |
Notas
Note:
Esta es una 'superglobal' o una variable automatic global. Significa simplemente que es una variable que está disponible en cualquier parte del script. No hace falta hacer global $variable; para acceder a la misma desde funciones o métodos.
Ver también
- move_uploaded_file() - Mueve un archivo subido a una nueva ubicación
- Gestión de Carga de Archivos
Anonymous
26-Apr-2011 09:48
Anonymous
18-Apr-2011 01:23
As mentioned , you should check the error index of the upload.
Example below suggests you have a file field named 'image'.
<?php
if($_FILES['image']['error'] == 0){
// success - move uploaded file and process stuff here
}else{
// 'there was an error uploading file' stuff here....
}
?>
John
04-Feb-2011 02:14
In the past you could unconditionally call $_FILES['profile_pic'] without ever having to worry about PHP spitting an "Undefined index: profile_pic" error (so long as the page posting had a file input on it (e.g. <input type="file" name="profile_pic" />)). This was the case regardless of whether or not the end user actually uploaded a file. These days, with so many people browsing the web via iPads, you have to explicitly check to see if the input isset($_FILES['profile_pic']) before calling into it, else you'll get the aforementioned error message. This is because iOS devices running Safari disable file inputs thereby causing them to be treated as if they don't exist. Time to update your scripts!
-john
mwgamera at gmail dot com
13-Aug-2009 12:40
To determine whether upload was successful you should check for error being UPLOAD_ERR_OK instead of checking the file size. When nothing is chosen to be uploaded, the key in $_FILES will still be there, but it should have error equal UPLOAD_ERR_NO_FILE.
calurion at gmail dot com
29-Jun-2009 11:51
For some reason when I tried to check if $_FILES['myVarName'] was empty() or !isset() or array_key_exists(), it always came back that the file was indeed in the superglobal, even when nothing was uploaded.
I wonder if this is a result of enctype="multipart/form-data".
Anyways, I solved my issue by checking to make sure that $_FILES['myVarName']['size'] > 0
Sam
22-May-2009 02:08
This is REQUIRED by the xhtml specs.
dewi at dewimorgan dot com
18-Mar-2009 06:35
The format of this array is (assuming your form has two input type=file fields named "file1", "file2", etc):
Array
(
[file1] => Array
(
[name] => MyFile.txt (comes from the browser, so treat as tainted)
[type] => text/plain (not sure where it gets this from - assume the browser, so treat as tainted)
[tmp_name] => /tmp/php/php1h4j1o (could be anywhere on your system, depending on your config settings, but the user has no control, so this isn't tainted)
[error] => UPLOAD_ERR_OK (= 0)
[size] => 123 (the size in bytes)
)
[file2] => Array
(
[name] => MyFile.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php/php6hst32
[error] => UPLOAD_ERR_OK
[size] => 98174
)
)
Last I checked (a while ago now admittedly), if you use array parameters in your forms (that is, form names ending in square brackets, like several file fields called "download[file1]", "download[file2]" etc), then the array format becomes... interesting.
Array
(
[download] => Array
(
[name] => Array
(
[file1] => MyFile.txt
[file2] => MyFile.jpg
)
[type] => Array
(
[file1] => text/plain
[file2] => image/jpeg
)
[tmp_name] => Array
(
[file1] => /tmp/php/php1h4j1o
[file2] => /tmp/php/php6hst32
)
[error] => Array
(
[file1] => UPLOAD_ERR_OK
[file2] => UPLOAD_ERR_OK
)
[size] => Array
(
[file1] => 123
[file2] => 98174
)
)
)
So you'd need to access the error param of file1 as, eg $_Files['download']['error']['file1']
andrewpunch at bigfoot dot com
17-Jan-2009 08:16
If $_FILES is empty, even when uploading, try adding enctype="multipart/form-data" to the form tag and make sure you have file uploads turned on.
