代码之家  ›  专栏  ›  技术社区  ›  PHPFan

为什么getimagesize()函数对于传递的不同参数[来自$\u文件超全局数组的索引]表现不同?

  •  -1
  • PHPFan  · 技术社区  · 7 年前

    在获得用户上传到服务器的图像大小之前,我还有一个用PHP编写的不完整代码。

    HTML表单代码:

    <!DOCTYPE html>
    <html>
    <body>
    
    <form action="upload.php" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
    </form>
    
    </body>
    </html>
    

    <?php
    $target_dir = "C:/xampp/htdocs/php_playground/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"]);
        echo "Check Value with tmp_name parameter : \n";
        echo "<pre>";
        print_r($check); 
        echo "</pre>";
        die;
    }
    ?>
    

    PHP代码1的输出如下:

    Check Value with tmp_name parameter :
    
    Array
    (
        [0] => 1536
        [1] => 2048
        [2] => 2
        [3] => width="1536" height="2048"
        [bits] => 8
        [channels] => 3
        [mime] => image/jpeg
    )
    

    PHP代码2:

     <?php
        $target_dir = "C:/xampp/htdocs/php_playground/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"]["name"]);
            echo "Check Value with name parameter : \n";
            echo "<pre>";
            print_r($check); 
            echo "</pre>";
            die;
        }
        ?>
    

    PHP代码2的输出如下:

    :getimagesize(demo\u image.jpg):无法打开流:中没有此类文件或目录 C: \xampp\htdocs\php\u playfield\uploads\upload。php 8.

    使用名称参数检查值:

    getimagesize() $_FILES 大堆

    请在这方面指导我。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Tejashwi Kalp Taru    7 年前

    因为在您使用“name”的第二个代码中,函数 getimagesize

    原因如下:

    “name”变量来自浏览器,因此在服务器端,函数无法定位图像,因此无法提供信息并生成警告。 而“tmp_名称”是服务器端位置的临时路径,上传文件临时存储在这里。因此,当您使用“temp_name”时,该函数能够读取文件并为您提供信息。

    裁判: http://php.net/manual/en/reserved.variables.files.php