PHP 8.3.4 Released!

Subida con el método POST

Esta característica permite la subida de ficheros de texto y binarios. Con la autenticación de PHP y las funciones de manipulación de ficheros se tiene control completo sobre quién está autorizado a realizar una subida y qué hay que hacer con el fichero una vez subido.

PHP es capaz de recibir subidas de ficheros de cualquier navegador compatible con el RFC-1867.

Nota: Nota sobre configuraciones relacionadas

Véase también las directivas file_uploads, upload_max_filesize, upload_tmp_dir, post_max_size y max_input_time de php.ini

PHP también admite el método PUT para subir ficheros tal y como lo utilizan los clientes Netscape Composer y Amaya del W3C. Véase el Soporte para el método PUT para más detalles.

Ejemplo #1 Formulario para la subida de ficheros

Se puede construir una página de subida de ficheros creando un formulario especial parecido a este:

<!-- El tipo de codificación de datos, enctype, DEBE especificarse como sigue -->
<form enctype="multipart/form-data" action="__URL__" method="POST">
    <!-- MAX_FILE_SIZE debe preceder al campo de entrada del fichero -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- El nombre del elemento de entrada determina el nombre en el array $_FILES -->
    Enviar este fichero: <input name="fichero_usuario" type="file" />
    <input type="submit" value="Enviar fichero" />
</form>

El __URL__ del ejemplo anterior se debe sustituir y debe apuntar a un fichero de PHP.

El campo oculto MAX_FILE_SIZE (medido en bytes) debe preceder al campo de entrada del fichero, siendo su valor el tamaño de fichero máximo aceptado por PHP. Se debe utilizar siempre este elemento del formulario, ya que evita a los usuarios la molestia de esperar a que un fichero grande sea transferido sólo para descubrir que falló la transferencia porque era demasiado grande. Hay que tener en cuenta que engañar a esta configuración en el lado del navegador es muy fácil; nunca dependa de que los ficheros que tengan un tamaño mayor sean bloqueados por esta característica. Es simplemente una característica conventiene para los usuarios en el lado cliente de la aplicación. No obstante, la configuración de PHP (en el lado del servidor) para un tamaño máximo no puede ser engañada.

Nota:

Asegúrese de que el formulario de subida de ficheros tiene el atributo enctype="multipart/form-data" o de lo contrario la subida de ficheros no funcionará.

El array global $_FILES contendrá toda la información de los los ficheros subidos. Su contenido en el formulario del ejemplo es el siguiente. Observe que se asume el empleo del nombre fichero_usuario para el fichero subido, tal como se utiliza en el script de ejemplo anterior. Este puede ser cualquier nombre.

$_FILES['fichero_usuario']['name']

El nombre original del fichero en la máquina del cliente.

$_FILES['fichero_usuario']['type']

El tipo MIME del fichero, si el navegador proporcionó esta información. Un ejemplo sería "image/gif". Este tipo MIME, sin embargo, no se comprueba en el lado de PHP y por lo tanto no se garantiza su valor.

$_FILES['fichero_usuario']['size']

El tamaño, en bytes, del fichero subido.

$_FILES['fichero_usuario']['tmp_name']

El nombre temporal del fichero en el cual se almacena el fichero subido en el servidor.

$_FILES['fichero_usuario']['error']

El código de error asociado a esta subida.

Por omisión, los ficheros se almacenan en el directorio temporal predeterminado del servidor, a menos que se haya indicado otra ubicaicón con la directiva upload_tmp_dir en php.ini. Se puede cambiar el directorio predeterminado del servidor estableciendo la variable de entorno TMPDIR en el entorno en que se ejecuta PHP. Configurarlo usando putenv() desde un script de PHP no funcionará. Esta variable de entorno también se puede utilizar para asegurarse de que las demás operaciones están trabajando sobre los ficheros subidos.

Ejemplo #2 Validación de la subida de ficheros

Véanse también las entradas de las funciones is_uploaded_file() y move_uploaded_file() para más información. El siguiente ejemplo procesará la subida de fichero desde un formulario.

<?php
$dir_subida
= '/var/www/uploads/';
$fichero_subido = $dir_subida . basename($_FILES['fichero_usuario']['name']);

echo
'<pre>';
if (
move_uploaded_file($_FILES['fichero_usuario']['tmp_name'], $fichero_subido)) {
echo
"El fichero es válido y se subió con éxito.\n";
} else {
echo
"¡Posible ataque de subida de ficheros!\n";
}

echo
'Más información de depuración:';
print_r($_FILES);

print
"</pre>";

?>

El script de PHP que recibe el fichero subido debería implementar cualquier lógica necesaria para determinar qué se debe hacer con el fichero subido. Se puede, por ejemplo, utilizar la variable $_FILES['fichero_usuario']['size'] para descartar cualquier fichero que sea demasiado pequeño o demasiado grande. Se podría utilizar la variable $_FILES['fichero_usuario']['type'] para descartar cualquier fichero que no corresponda con un cierto criterio de tipo, aunque esto se debe emplear solo como la primera de una serie de comprobaciones debido a que este valor está completamente bajo el control del cliente y no se comprueba en el lado de PHP. También se puede usar $_FILES['fichero_usuario']['error'] y planear la lógica de acuerdo con los códigos de error. Cualquiera que sea la lógica, se debería borrar el fichero del directorio temporal o moverlo a otra ubicación.

Si no se selecciona ningún fichero en el formulario para realizar la subida, PHP devolverá $_FILES['fichero_usuario']['size'] como 0, y $_FILES['fichero_usuario']['tmp_name'] como ninguno.

El fichero será borrado del directorio temporal al final de la solicitud si este no ha sido movido o renombrado.

Ejemplo #3 Subir un array de ficheros

PHP admite la funcionalidad de array en HTML incluso con ficheros.

<form action="" method="post" enctype="multipart/form-data">
<p>Imágenes:
<input type="file" name="imágenes[]" />
<input type="file" name="imágenes[]" />
<input type="file" name="imágenes[]" />
<input type="submit" value="Enviar" />
</p>
</form>
<?php
foreach ($_FILES["imágenes"]["error"] as $clave => $error) {
if (
$error == UPLOAD_ERR_OK) {
$nombre_tmp = $_FILES["imágenes"]["tmp_name"][$clave];
// basename() puede evitar ataques de denegació del sistema de ficheros;
// podría ser apropiado más validación/saneamiento del nombre de fichero
$nombre = basename($_FILES["imágenes"]["name"][$clave]);
move_uploaded_file($nombre_tmp, "datos/$nombre");
}
}
?>

Se puede implementar una barra de progreso de subida de ficheros con el Progreso de subida en sesiones.

add a note

User Contributed Notes 12 notes

up
77
daevid at daevid dot com
14 years ago
I think the way an array of attachments works is kind of cumbersome. Usually the PHP guys are right on the money, but this is just counter-intuitive. It should have been more like:

Array
(
[0] => Array
(
[name] => facepalm.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpn3FmFr
[error] => 0
[size] => 15476
)

[1] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] =>
)
)

and not this
Array
(
[name] => Array
(
[0] => facepalm.jpg
[1] =>
)

[type] => Array
(
[0] => image/jpeg
[1] =>
)

[tmp_name] => Array
(
[0] => /tmp/phpn3FmFr
[1] =>
)

[error] => Array
(
[0] => 0
[1] => 4
)

[size] => Array
(
[0] => 15476
[1] => 0
)
)

Anyways, here is a fuller example than the sparce one in the documentation above:

<?php
foreach ($_FILES["attachment"]["error"] as $key => $error)
{
$tmp_name = $_FILES["attachment"]["tmp_name"][$key];
if (!
$tmp_name) continue;

$name = basename($_FILES["attachment"]["name"][$key]);

if (
$error == UPLOAD_ERR_OK)
{
if (
move_uploaded_file($tmp_name, "/tmp/".$name) )
$uploaded_array[] .= "Uploaded file '".$name."'.<br/>\n";
else
$errormsg .= "Could not move uploaded file '".$tmp_name."' to '".$name."'<br/>\n";
}
else
$errormsg .= "Upload error. [".$error."] on file '".$name."'<br/>\n";
}
?>
up
48
mpyw
7 years ago
Do not use Coreywelch or Daevid's way, because their methods can handle only within two-dimensional structure. $_FILES can consist of any hierarchy, such as 3d or 4d structure.

The following example form breaks their codes:

<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="files[x][y][z]">
<input type="submit">
</form>

As the solution, you should use PSR-7 based zendframework/zend-diactoros.

GitHub:

https://github.com/zendframework/zend-diactoros

Example:

<?php

use Psr\Http\Message\UploadedFileInterface;
use
Zend\Diactoros\ServerRequestFactory;

$request = ServerRequestFactory::fromGlobals();

if (
$request->getMethod() !== 'POST') {
http_response_code(405);
exit(
'Use POST method.');
}

$uploaded_files = $request->getUploadedFiles();

if (
!isset(
$uploaded_files['files']['x']['y']['z']) ||
!
$uploaded_files['files']['x']['y']['z'] instanceof UploadedFileInterface
) {
http_response_code(400);
exit(
'Invalid request body.');
}

$file = $uploaded_files['files']['x']['y']['z'];

if (
$file->getError() !== UPLOAD_ERR_OK) {
http_response_code(400);
exit(
'File uploading failed.');
}

$file->moveTo('/path/to/new/file');

?>
up
23
coreywelch+phpnet at gmail dot com
8 years ago
The documentation doesn't have any details about how the HTML array feature formats the $_FILES array.

Example $_FILES array:

For single file -

Array
(
[document] => Array
(
[name] => sample-file.doc
[type] => application/msword
[tmp_name] => /tmp/path/phpVGCDAJ
[error] => 0
[size] => 0
)
)

Multi-files with HTML array feature -

Array
(
[documents] => Array
(
[name] => Array
(
[0] => sample-file.doc
[1] => sample-file.doc
)

[type] => Array
(
[0] => application/msword
[1] => application/msword
)

[tmp_name] => Array
(
[0] => /tmp/path/phpVGCDAJ
[1] => /tmp/path/phpVGCDAJ
)

[error] => Array
(
[0] => 0
[1] => 0
)

[size] => Array
(
[0] => 0
[1] => 0
)

)

)

The problem occurs when you have a form that uses both single file and HTML array feature. The array isn't normalized and tends to make coding for it really sloppy. I have included a nice method to normalize the $_FILES array.

<?php

function normalize_files_array($files = []) {

$normalized_array = [];

foreach(
$files as $index => $file) {

if (!
is_array($file['name'])) {
$normalized_array[$index][] = $file;
continue;
}

foreach(
$file['name'] as $idx => $name) {
$normalized_array[$index][$idx] = [
'name' => $name,
'type' => $file['type'][$idx],
'tmp_name' => $file['tmp_name'][$idx],
'error' => $file['error'][$idx],
'size' => $file['size'][$idx]
];
}

}

return
$normalized_array;

}

?>

The following is the output from the above method.

Array
(
[document] => Array
(
[0] => Array
(
[name] => sample-file.doc
[type] => application/msword
[tmp_name] => /tmp/path/phpVGCDAJ
[error] => 0
[size] => 0
)

)

[documents] => Array
(
[0] => Array
(
[name] => sample-file.doc
[type] => application/msword
[tmp_name] => /tmp/path/phpVGCDAJ
[error] => 0
[size] => 0
)

[1] => Array
(
[name] => sample-file.doc
[type] => application/msword
[tmp_name] => /tmp/path/phpVGCDAJ
[error] => 0
[size] => 0
)

)

)
up
17
anon
8 years ago
For clarity; the reason you would NOT want to replace the example script with
$uploaddir = './';
is because if you have no coded file constraints a nerd could upload a php script with the same name of one of your scripts in the scripts directory.

Given the right settings and permissions php-cgi is capable of replacing even php files.

Imagine if it replaced the upload post processor file itself. The next "upload" could lead to some easy exploits.

Even when replacements are not possible; uploading an .htaccess file could cause some problems, especially if it is sent after the nerd throws in a devious script to use htaccess to redirect to his upload.

There are probably more ways of exploiting it. Don't let the nerds get you.

More sensible to use a fresh directory for uploads with some form of unique naming algorithm; maybe even a cron job for sanitizing the directory so older files do not linger for too long.
up
7
fravadona at gmail dot com
3 years ago
mpyw is right, PSR-7 is awesome but a little overkill for simple projects (in my opinion).

Here's an example of function that returns the file upload metadata in a (PSR-7 *like*) normalized tree. This function deals with whatever dimension of upload metadata.

I kept the code extremely simple, it doesn't validate anything in $_FILES, etc... AND MOST IMPORTANTLY, it calls array_walk_recursive in an *undefined behaviour* way!!!

You can test it against the examples of the PSR-7 spec ( https://www.php-fig.org/psr/psr-7/#16-uploaded-files ) and try to add your own checks that will detect the error in the last example ^^

<?php
/**
* THIS CODE IS ABSOLUTELY NOT MEANT FOR PRODUCTION !!! MAY ITS INSIGHTS HELP YOU !!!
*/
function getNormalizedFiles()
{
$normalized = array();

if ( isset(
$_FILES) ) {

foreach (
$_FILES as $field => $metadata ) {

$normalized[$field] = array(); // needs initialization for array_replace_recursive

foreach ( $metadata as $meta => $data ) { // $meta is 'tmp_name', 'error', etc...

if ( is_array($data) ) {

// insert the current meta just before each leaf !!! WRONG USE OF ARRAY_WALK_RECURSIVE !!!
array_walk_recursive($data, function (&$v,$k) use ($meta) { $v = array( $meta => $v ); });

// fuse the current metadata with the previous ones
$normalized[$field] = array_replace_recursive($normalized[$field], $data);

} else {
$normalized[$field][$meta] = $data;
}
}
}
}
return
$normalized;
}
?>
up
13
eslindsey at gmail dot com
14 years ago
Also note that since MAX_FILE_SIZE hidden field is supplied by the browser doing the submitting, it is easily overridden from the clients' side. You should always perform your own examination and error checking of the file after it reaches you, instead of relying on information submitted by the client. This includes checks for file size (always check the length of the actual data versus the reported file size) as well as file type (the MIME type submitted by the browser can be inaccurate at best, and intentionally set to an incorrect value at worst).
up
5
Mark
13 years ago
$_FILES will be empty if a user attempts to upload a file greater than post_max_size in your php.ini

post_max_size should be >= upload_max_filesize in your php.ini.
up
5
claude dot pache at gmail dot com
14 years ago
Note that the MAX_FILE_SIZE hidden field is only used by the PHP script which receives the request, as an instruction to reject files larger than the given bound. This field has no significance for the browser, it does not provide a client-side check of the file-size, and it has nothing to do with web standards or browser features.
up
-1
Anonymous
7 years ago
I have found it useful to re-order the multidimensional $_FILES array into a more intuitive format, as proposed by many other developers already.

Unfortunately, most of the proposed functions are not able to re-order the $_FILES array when it has more than 1 additional dimension.

Therefore, I would like to contribute the function below, which is capable of meeting the aforementioned requirement:

<?php
function get_fixed_files() {
$function = function($files, $fixed_files = array(), $path = array()) use (&$function) {
foreach (
$files as $key => $value) {
$temp = $path;
$temp[] = $key;

if (
is_array($value)) {
$fixed_files = $function($value, $fixed_files, $temp);
} else {
$next = array_splice($temp, 1, 1);
$temp = array_merge($temp, $next);

$new = &$fixed_files;

foreach (
$temp as $key) {
$new = &$new[$key];
}

$new = $value;
}
}

return
$fixed_files;
};

return
$function($_FILES);
}
?>

Side note: the unnamed function within the function is used to avoid confusion regarding the arguments necessary for the recursion within the function, for example when viewing the function in an IDE.
up
-4
Age Bosma
12 years ago
"If no file is selected for upload in your form, PHP will return $_FILES['userfile']['size'] as 0, and $_FILES['userfile']['tmp_name'] as none."

Note that the situation above is the same when a file exceeding the MAX_FILE_SIZE hidden field is being uploaded. In this case $_FILES['userfile']['size'] is also set to 0, and $_FILES['userfile']['tmp_name'] is also empty. The difference would only be the error code.
Simply checking for these two conditions and assuming no file upload has been attempted is incorrect.

Instead, check if $_FILES['userfile']['name'] is set or not. If it is, a file upload has at least been attempted (a failed attempt or not). If it is not set, no attempt has been made.
up
-13
bimal at sanjaal dot com
9 years ago
Some suggestions here:

1. It is always better to check for your error status. If MAX_FILE_SIZE is active and the uploaded file crossed the limit, it will set the error. So, only when error is zero (0), move the file.

2. If possible, never allow your script to upload in the path where file can be downloaded. Point your upload path to outside of public_html area or prevent direct browsing (using .htaccess restrictions). Think, if someone uploads malicious code, specially php codes, they will be executed on the server.

3. Do not use the file name sent by the client. Regenerate a new name for newly uploaded file. This prevents overwriting your old files.

4. Regularly track the disk space consumed, if you are running out of storage.
up
-17
katrinaelaine6 at gmail dot com
6 years ago
Here's a complete example of the $_FILES array with nested and non-nested names. Let's say we have this html form:

<form action="test.php" method="post">

<input type="file" name="single" id="single">

<input type="file" name="nested[]" id="nested_one">
<input type="file" name="nested[root]" id="nested_root">
<input type="file" name="nested[][]" id="nested_two">
<input type="file" name="nested[][parent]" id="nested_parent">
<input type="file" name="nested[][][]" id="nested_three">
<input type="file" name="nested[][][child]" id="nested_child">

<input type="submit" value="Submit">

</form>

In the test.php file:

<?php

print_r
($_FILES);
exit;

?>

If we upload a text file with the same name as the input id for each input and click submit, test.php will output this:

<?php

Array
(

[
single] => Array
(
[
name] => single.txt
[type] => text/plain
[tmp_name] => /tmp/phpApO28i
[error] => 0
[size] => 3441
)

[
nested] => Array
(
[
name] => Array
(
[
0] => nested_one.txt
[root] => nested_root.txt
[1] => Array
(
[
0] => nested_two.txt
)

[
2] => Array
(
[
parent] => nested_parent.txt
)

[
3] => Array
(
[
0] => Array
(
[
0] => nested_three.txt
)

)

[
4] => Array
(
[
0] => Array
(
[
child] => nested_child.txt
)

)

)

// type, tmp_name, size, and error will have the same structure.
)

)

?>
To Top