PHP 当文件太大时,我如何获取文件名


PHP How can I get the file's name when it is too large?

当文件太大时,如何获取文件名?

我可以用$_SERVER['CONTENT_LENGTH']来获得它的长度,但它的名字呢?

// check that post_max_size has not been reached
// convert_to_bytes is the function turn `5M` to bytes because $_SERVER['CONTENT_LENGTH'] is in bytes.
if (isset($_SERVER['CONTENT_LENGTH']) 
    && (int) $_SERVER['CONTENT_LENGTH'] > convert_to_bytes(ini_get('post_max_size'))) 
{
  // ... with your logic
  throw new Exception('File too large!');
}

例如,如果我上传一个mp3,

very-large.mp3 (size 11MB)

我想得到它的名字,这是very-large.mp3可能吗?还是我必须使用javascript?

注意:

你会得到

Array()

当文件太大时$_FILES

所以你不能得到$_FILES[file]["name"]

因此,

如果您有<input type="file" name="userFile"/>,则使用它的名称从全局变量中获取名称$_FILES echo "File {$_FILES['userFile']['name']} is too big.";