// Other variables
$MAX_FILENAME_LENGTH = 260;
$file_name = $_FILES[$upload_name]['name'];
//echo "testing-".$file_name."<br>";
//$file_name = strtolower($file_name);
$file_extension = end(explode('.', $file_name)); //ERROR ON THIS LINE
$uploadErrors = array(
    0=>'There is no error, the file uploaded with success',
    1=>'The uploaded file exceeds the upload max filesize allowed.',
    2=>'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
    3=>'The uploaded file was only partially uploaded',
    4=>'No file was uploaded',
    6=>'Missing a temporary folder'
);

什么好主意吗?过了2天,还是卡住了。


当前回答

其他所有人都已经给出了出错的原因,但这里是完成您想要做的事情的最佳方式: $file_extension = pathinfo($file_name, PATHINFO_EXTENSION);

其他回答

首先,您必须像这样将值存储在变量中

$value = explode("/", $string);

然后你可以使用end函数从这样的数组中获取最后一个索引

echo end($value);

我希望这对你有用。

其他所有人都已经给出了出错的原因,但这里是完成您想要做的事情的最佳方式: $file_extension = pathinfo($file_name, PATHINFO_EXTENSION);

将爆炸的结果赋值给一个变量,并将该变量传递给end:

$tmp = explode('.', $file_name);
$file_extension = end($tmp);

问题是,这个结束需要一个引用,因为它修改了数组的内部表示(即它使当前元素指针指向最后一个元素)。

爆炸的结果('。`, $file_name)不能转换为引用。这是PHP语言中的一个限制,可能是出于简单的原因。

Php 7兼容的正确用法:

$fileName      = 'long.file.name.jpg';
$tmp           = explode('.', $fileName);
$fileExtension = end($tmp);

echo $fileExtension;
// jpg

PHP官方手册: 结束()

参数

array

数组。这个数组是通过引用传递的,因为它是由函数修改的。这意味着您必须传递给它一个实际变量,而不是返回数组的函数,因为只有实际变量可以通过引用传递。