获取CodeIgniter 4上传文件的文件名的方法

Way to get filename of CodeIgniter 4 Uploaded file

默认情况下,CodeIgniter 4 的 move 函数 returns true 如果文件已移动。 但是如果一个文件已经存在,它会在文件名中添加一个带有计数器的分隔符,因为我只有 true 我没有新的文件名,我想将其存储在数据库中。

我可以扩展 UploadedFile class(老实说这对我来说很难),但我问的是我的感觉,想知道文件名很直观,所以大多数是一种更简单的方法。

一个微小的更正:不是 move function

File 个实例应该是 move method

根据你想获取的名字,有以下三种方法:

getName()

You can retrieve the original filename provided by the client with the getName() method. This will typically be the filename sent by the client, and should not be trusted. If the file has been moved, this will return the final name of the moved file:

$name = $file->getName();


getClientName()

Always returns the original name of the uploaded file as sent by the client, even if the file has been moved:

$originalName = $file->getClientName();


getTempName()

To get the full path of the temp file that was created during the upload, you can use the getTempName() method:

$tempfile = $file->getTempName();

查看 CI4 Documentation 了解更多信息。