Reputation: 103
I'm learning about the file uploading in php. For that I referred some links. In that they said inside the upload code we have to use:
move_uploaded_file(upload_file_name, 'path to move')
function for move the file to our desired directory.
But in move_uploaded_file
function they pass the temp name as a first argument.
Why they are using the temp name as a first argument? Why not they used original name?
move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$new_file_name);
Why we have to use this temp_name? Is there any reasons available for that?
Upvotes: 0
Views: 178
Reputation: 582
I will try to explain php file uploads as i'm understand this process.
When you upload file in php (using POST request) php should fill $_FILES global variable with file data. To get file data (such size, type, etc.) this file should be available in filesystem. So, using upload_tmp_dir directive from php.ini php save this file to temporary folder and then fill $_FILES global variable.
After that you can working with this file using data which php provide for you.
Also php has some configuration directives for request data validation, such as: upload_max_filesize, post_max_size. So you can set global validation configuration and php make this validation for you.
Why they are using the temp name as a first argument?
Because this function processing only files from http request.
If you will try set any file which doesn't present in current http request as first argument of this function you will get false
as result.
If you interested in more details, you can read php source (this function process post request as i'm understand).
Why not they used original name?
Files uploaded by current http request doesn't exists in filesytem by original name (i think it's implemented for security reasons and maybe to avoid collisions with the same file names). That's why you can't using this original name.
P.S. And guys, please correct me if i'm wrong (it will be useful for all of us to know more about php internal processes)
Upvotes: 0
Reputation: 4335
When uploading a file, that file will be saved in your server`s temporary files directory, having a filename like /tmp/somefilename.
Using move_uploaded_file
you will move that file to any destiation you like / you are allowed to.
The original filename like it was on the users computer is stored in
$_FILES['userfile']['name']`.
Upvotes: 1