php explode文件扩展名为null

时间:2016-02-08 05:36:29

标签: php

因此,当我上传文件时,我想检查它是哪个文件扩展名:

$files      = $_FILES['files'];

enter image description here

$files_name      =  $files['name'];     

显示:`" img.jpg"'

$files_name_explode = end(explode('.', $files_name));

获取null

我做错了什么?

由于

3 个答案:

答案 0 :(得分:2)

使用以下代码:

$path = $_FILES['files']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);

希望它会对你有所帮助:)。

答案 1 :(得分:1)

而不是explode,您可以使用pathinfo

使用

$files_name_explode = pathinfo($filename, PATHINFO_EXTENSION);

pathinfo()可以为您提供其他信息,例如规范路径,具体取决于您传递给它的常量。

答案 2 :(得分:1)

试试这个:使用in_array验证的完整代码($ file_extension,$ validextensions)

if(isset($_FILES["file"]["type"])) 
{
    $validextensions = array("jpeg", "jpg", "png","JPG");
    $temporary = explode(".", $_FILES["file"]["name"]);
    $file_extension = end($temporary);
    if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg")
    ) && ($_FILES["file"]["size"] < 1000000)//Approx. 100kb files can be uploaded.
    && in_array($file_extension, $validextensions)) {
        if ($_FILES["file"]["error"] > 0)
        {
            //echo "Return Code: " . $_FILES["file"]["error"] . "<br/><br/>";
        }
        else
        {


    $file_name=$_FILES["file"]["name"];
    $rand_file_name=rand()."_".$file_name;

                $sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable


                $targetPath = "../upload/". $rand_file_name; // Target path where file is to be stored
                move_uploaded_file($sourcePath,$targetPath); // Moving Uploaded file                

        }
    }
    else
    {
        //echo "<span id='invalid'>***Invalid file Size or Type***<span>";
    }
}