• PHP Video Tutorials

PHP - Function move_uploaded_file()



The move_uploaded_file() function can move an uploaded file to new location. If the filename is not a valid upload file, then no action can occur and return false. If the filename is a valid upload file but can't be moved for some reason, then no action can occur and return false. Additionally, a warning can be issued.

Syntax

bool move_uploaded_file ( string $filename , string $destination )

This function can check to ensure that the file designated by filename is a valid upload file, which means that it has uploaded via PHP's HTTP POST upload mechanism. If the file is valid, it can be moved to the filename given by the destination.

This sort of check is especially used if there is any chance that anything done with uploaded files can reveal their contents to the user, or even to other users on the same system.

Example

<?php
   $uploads_dir = "/PhpProject/uploads";
   foreach($_FILES["pictures"]["error"] as $key => $error) {
      if($error == UPLOAD_ERR_OK) {
         $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
         $name = basename($_FILES["pictures"]["name"][$key]);
         move_uploaded_file($tmp_name, "$uploads_dir/$name");
      }
   }
?>
php_function_reference.htm
Advertisements