PHP | move_uploaded_file() Function

PHP move_uploaded_file() Function


Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the move_uploaded_file() Function in PHP.

The built-in PHP move_uploaded_file() function moves an uploaded file to a new location.

Syntax

Following below is the syntax to use this function -

bool move_uploaded_file ( string $filename , string $destination )


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

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

Return Value

If the filename is not a valid upload file, then no action is taken and it returns false. If the filename is a valid uploaded file but can not be moved for some reasons, then no action occurs and returns false. In addition, a warning can be issued.

Example

Try out the below 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");
      }
   }
?>


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the parse_ini_file() Function in PHP.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.