Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between copy and move_uploaded_file

Tags:

php

what is difference between copy() and move_uploaded_file()

I think both the functions perform same operations then whats the difference?

copy ( $_FILES['file']['tmp_name'], 
     "C:/Apache/htdocs/" . $_FILES['file']['name'] ) 


move_uploaded_file($_FILES['file']['tmp_name'], 
     "C:/Apache/htdocs/" . $_FILES['file']['name'])
like image 840
Vijayan Avatar asked Feb 04 '12 09:02

Vijayan


3 Answers

As to complement @Muhammad Hasan Khan's answer.

According to this comment https://www.php.net/manual/en/function.is-uploaded-file.php#113766, move_uploaded_file does not bring anymore security than is_uploaded_file, and therefore the two followings snippets are strictly equivalent in terms of security:

$tmp = $_FILES['file']['tmp_name'];
if(false === is_uploaded_file($tmp)){
    return false;
}
copy($tmp, $dst);
$tmp = $_FILES['file']['tmp_name'];
move_uploaded_file($tmp, $dst);
like image 78
ling Avatar answered Oct 11 '22 00:10

ling


Copy will copy the file source to destination whereas move will move it.

When a file is copied, a duplicate is made means temporary buffers(source) is not cleaned.

When you move a file, it is deleted from the original location means in temporary buffer (source: $_FILES) and move the file in destinations.

like image 44
Pankaj Chauhan Avatar answered Oct 11 '22 01:10

Pankaj Chauhan


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.

http://php.net/manual/en/function.move-uploaded-file.php

If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.

like image 13
Muhammad Hasan Khan Avatar answered Oct 11 '22 01:10

Muhammad Hasan Khan