move_uploaded_file
(PHP 4 >= 4.0.3, PHP 5, PHP 7)
move_uploaded_file — Moves an uploaded file to a new location
Description
$filename
, string $destination
)
This function checks to ensure that the file designated by
filename
is a valid upload file (meaning
that it was uploaded via PHP's HTTP POST upload mechanism). If
the file is valid, it will be moved to the filename given by
destination
.
This sort of check is especially important if there is any chance that anything done with uploaded files could reveal their contents to the user, or even to other users on the same system.
Parameters
-
filename
-
The filename of the uploaded file.
-
destination
-
The destination of the moved file.
Return Values
Returns TRUE
on success.
If filename
is not a valid upload file,
then no action will occur, and
move_uploaded_file() will return
FALSE
.
If filename
is a valid upload file, but
cannot be moved for some reason, no action will occur, and
move_uploaded_file() will return
FALSE
. Additionally, a warning will be issued.
Examples
Example #1 Uploading multiple files
<?php
$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
// basename() may prevent filesystem traversal attacks;
// further validation/sanitation of the filename may be appropriate
$name = basename($_FILES["pictures"]["name"][$key]);
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}
?>
Notes
Note:
move_uploaded_file() is both safe mode and open_basedir aware. However, restrictions are placed only on the
destination
path as to allow the moving of uploaded files in whichfilename
may conflict with such restrictions. move_uploaded_file() ensures the safety of this operation by allowing only those files uploaded through PHP to be moved.
If the destination file already exists, it will be overwritten.
See Also
- is_uploaded_file() - Tells whether the file was uploaded via HTTP POST
- rename() - Renames a file or directory
- See Handling file uploads for a simple usage example
Version en cache
05/11/2024 10:26:14 Cette version de la page est en cache (à la date du 05/11/2024 10:26:14) afin d'accélérer le traitement. Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la dernère version de la page.Document créé le 30/01/2003, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/php-rf-move-uploaded-file.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.