Reputation: 9066
I am quite new to php.I was going through an example on file uploading.Here inside getimagesize()
function $_FILES['file']['temp_name']
is used.and when i echoed $_FILES['file']['temp_name'] it shows the following output:
C:\xampp\tmp\phpDE4B.tmp
my questions are:
1.why use tmp_name instead of original name inside getimagesize()
function.
2.For what reason this tmp_name is created ?
3.How it is generated?
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
Upvotes: 3
Views: 3776
Reputation: 353
When some file is uploaded to the server, it is placed in temporary directory to work with it. In order to not have collisions in names temporary name is assigned.
1) You need to pass the path to file to getimagesize()
, because you can want to work not only with saved files.
2) Tmp_name is created to avoid collisions, but you can get original name if you need.
3) Just some random generated name inside of a temp directory.
Upvotes: 1
Reputation: 212422
The temporary file is generated as part of the file upload process, handled directly between PHP and the web server.
A temp file is used so that userland code can move the file to its final destination folder on the server - the final destination folder isn't information that can be passed in the http request, or handled by the web server, because it depends entirely on your application. If the file isn't moved to its final destination folder, then the temporary file will be deleted automatically at the end of the request
In this case, the code is part of a validation process to ensure that the file is what it claims to be before moving it to its final destination folder (assuming that it is a valid file).
Upvotes: 3