为什么在这个例子中使用tmp_name [英] why tmp_name is used in this example

查看:290
本文介绍了为什么在这个例子中使用tmp_name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP很新,我正在通过文件上传的例子。 getimagesize()函数 $ _ FILES ['file '] ['temp_name'] 被使用。当我回显$ _FILES ['file'] ['temp_name']时,显示如下输出:


C:\xampp\tmp\phpDE4B.tmp

我的问题是:
$ b $ 1.为什么要在 getimagesize()函数中使用tmp_name而不是原始名称。



2.是什么原因创建了tmp_name?



3.它是如何生成的?

  $ target_dir =uploads /; 
$ target_file = $ target_dir。基名($ _ FILES [ fileToUpload] [ 名称]);
$ uploadOk = 1;
$ imageFileType = pathinfo($ target_file,PATHINFO_EXTENSION);
//检查图像文件是实际图像还是假图像
if(isset($ _ POST [submit])){
$ check = getimagesize($ _ FILES [fileToUpload ] [ tmp_name的值]);
if($ check!== false){
echoFile is a image - 。 $ check [mime]。 ;
$ uploadOk = 1;
} else {
echo文件不是图像。
$ uploadOk = 0;


$ / code $ / pre

解决方案

临时文件作为文件上传过程的一部分生成,直接在PHP和Web服务器之间处理。

使用临时文件,以便用户级代码可以将文件移动到它在服务器上的最终目标文件夹 - 最终的目标文件夹不是可以在http请求中传递或由Web服务器处理的信息,因为它完全取决于您的应用程序。如果文件没有移动到最终的目标文件夹,那么临时文件将在请求结束时被自动删除。

在这种情况下,代码是的确认过程,以确保该文件是在 之前声明为 的文件,并将其移动到其最终的目标文件夹(假定它是有效的文件)。 >

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

解决方案

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

这篇关于为什么在这个例子中使用tmp_name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆