why tmp_name is used in this example

怎甘沉沦 提交于 2020-01-23 12:51:11

问题


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;
    }
}

回答1:


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).




回答2:


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.



来源:https://stackoverflow.com/questions/28528566/why-tmp-name-is-used-in-this-example

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!