Change language

How can I move a file to a different folder on the server using PHP?

|
Syntax:
move_uploaded_file (string $Sourcefilename, string $destination): bool rename (string $oldname, string $newname [, resource $context]): bool 
  • The move_upload_file() method:this function checks if the source file or "$Sourcefilename" in syntax is a valid upload file (meaning it was uploaded using PHP - HTTP POST loading mechanism). If the file is valid, it will be moved to the file name given in the destination or ’ $destination ’ in the syntax. 
    Such verification is especially important if there is a possibility that something done with the downloaded files could disclose their contents to the user or even other users on the same system. Note that if the target file already exists, it will be overwritten. For this reason, you first need to check for the existence of the file and then perform only one action.
  • rename() method:This method tries to rename oldname to newname, moving it between directories if it is necessary. If newname exists, it will be overwritten. If a directory rename newname exists, then this function will issue a warning.
Example:This example is code that uploads a file to a directory named Uploads and then changes your path to another directory named New.Upload.html < html > < head > < title > Move a file into a different folder on the server < / title > < / head > < body > < form action = "upfile.php" method = " post " enctype = "multipart / form-data" > < input type = "file" name = "file" id = "file" > < br > < br > < input type = "submit" name = "submit" value = "Submit" > < / form > < / body > < / html > upfile.php  
// The target download directory is downloads $target_dir = "uploads /" ; $target_file = $target_dir . basename ( $_ FILES [ " file " ] [ " name " ]); $uOk = 1; if (isset ( $_ POST [ " submit " ])) { // Check if the file exists if ( file_exists ( $target_file )) { echo "file already exists.
"
; $uOk = 0; } // Check if $uOk is set to 0 if ( $uOk == 0) { echo "Your file was not uploaded.
"
; } // if uOk = 1, try uploading the file else {

// $_FILES [& quot; file & quot;] [& quot; tmp_name & quot;] implies the path to the repository // in the tmp directory that moves to downloads // directory using the move_uploaded_file() method if (move_uploaded_file ( $_ FILES [ " file " ] [ " tmp_name " ], $target_file )) { echo "The file" . basename ( $_ FILES [ " file " ] [ " name " ]) . "has been uploaded.
"
; // Move the file to a new directory if (rename ( $target_file , "New /" . basename ( $_ FILES [ "file " ] [ " name " ]))) { echo "File moving operation success
"
; } else { echo "File moving operation failed ..
"
; } } else { echo "Sorry, there was an error uploading your file.
"
; } } }  
?>
Note.Uploads and New ones already exist once, so you will have to create them if they are not available on the server.
The code works:

The code is executed using the rename method (switch to new)
Important Methods:

  • file_exists ($target_file):this method is used to check if a path exists ... If it exists, it returns true, otherwise it returns false.
  • basename ($_FILES ["file"] ["name"]):this method is used to get the name of the selected file and its peculiarity is that it operates on a user-supplied input string and is unaware of the actual filesystem and enforces the security feature provided by the browsers.

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method