【问题标题】:Way to get filename of CodeIgniter 4 Uploaded file获取 CodeIgniter 4 上传文件的文件名的方法
【发布时间】:2022-01-22 19:46:31
【问题描述】:

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

我可以扩展 UploadedFile 类(老实说,这对我来说非常困难),但我想知道文件名是非常直观的,所以大多数情况下都有更简单的方法。

【问题讨论】:

  • 它在返回 true 之前将 $destination 的基本名称存储在 $this->name 中,所以 - 之后只需读取该属性的值...?

标签: php codeigniter codeigniter-4


【解决方案1】:

一个小小的修正:不是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 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 2012-07-09
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 2013-05-21
    相关资源
    最近更新 更多