Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - Differences between copy, rename and move_uploaded_file

Are there differences when I use that functions? Why should I use one instead of the other one...

like image 655
thomas Avatar asked Oct 13 '10 13:10

thomas


People also ask

What is move_uploaded_file in PHP?

The move_uploaded_file() function moves an uploaded file to a new destination. Note: This function only works on files uploaded via PHP's HTTP POST upload mechanism. Note: If the destination file already exists, it will be overwritten.

How do I copy a file from one directory to another in PHP?

The copy() function in PHP is used to copy a file from source to target or destination directory. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure.

How can change upload file name in PHP?

You can simply change the name of the file by changing the name of the file in the second parameter of move_uploaded_file . $temp = explode(".", $_FILES["file"]["name"]); $newfilename = round(microtime(true)) . '.

How do I move a file into permanent directory?

PHP stores uploaded files in a temporary directory with temporary file names. You must move uploaded files to a permanent directory, if you want to keep them permanently. PHP offers the move_uploaded_file() to help you moving uploaded files. The example script, processing_uploaded_files.


1 Answers

  • copy() copies the file - you now have 2 files, and for large files, this can take very long
  • rename() changes the file's name, which can mean moving it between directories.
  • move_uploaded_file() is basically the same as rename(), but it will only work on files that have been uploaded via PHP's upload mechanism. This is a security feature that prevents users from tricking your script into showing them security-relevant data.

In the future, I suggest looking up such information in the PHP Manual yourself.

like image 64
Michael Borgwardt Avatar answered Sep 18 '22 18:09

Michael Borgwardt