只有变量应通过引用传递

php
2022-08-30 06:06:04
// 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天后仍然卡住。


答案 1

将 的结果赋给变量,并将该变量传递给 :explodeend

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

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

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


答案 2

其他人已经给出了你遇到错误的原因,但这是做你想做的事情的最好方法:$file_extension = pathinfo($file_name, PATHINFO_EXTENSION);


推荐