在使用System/model/ file时,我试图保留原始文件名,我得到了以下代码来扩展该模型:
namespace System\Models;
class NewFile extends File { public function fromPost($uploadedFile) { if ($uploadedFile === null) { return; }
$this->file_name = $uploadedFile->getClientOriginalName();
$this->file_size = $uploadedFile->getClientSize();
$this->content_type = $uploadedFile->getMimeType();
$this->disk_name = $this->getDiskName();
/*
* getRealPath() can be empty for some environments (IIS)
*/
$realPath = empty(trim($uploadedFile->getRealPath()))
? $uploadedFile->getPath() . DIRECTORY_SEPARATOR . $uploadedFile->getFileName()
: $uploadedFile->getRealPath();
//$this->putFile($realPath, $this->disk_name);
$this->putFile($realPath, $this->file_name);
return $this;它与文件本身一起工作,它保留原始名称,但问题是连接到附加文件的链接仍在生成中。我心烦意乱,但做不到这份工作。有人能详细说明如何修复它吗?
发布于 2018-06-30 20:34:39
哦,我看到了,它似乎试图使用disk_name来生成URL
所以你的形象保存得很好
//$this->putFile($realPath, $this->disk_name);
$this->putFile($realPath, $this->file_name);但你只需要换一句..。只需撤消您的更改并进行此更改即可。
$this->file_name = $uploadedFile->getClientOriginalName();
$this->file_size = $uploadedFile->getClientSize();
$this->content_type = $uploadedFile->getMimeType();
// $this->disk_name = $this->getDiskName();
$this->disk_name = $this->file_name;
// use same file_name for disk ^ HERE链接逻辑(用于仅引用)
vendor\october\rain\src\Database\Attach\File.php和modules\system\models\File.php
/**
* Returns the public address to access the file.
*/
public function getPath()
{
return $this->getPublicPath() . $this->getPartitionDirectory() . $this->disk_name;
}
/**
* Define the public address for the storage path.
*/
public function getPublicPath()
{
$uploadsPath = Config::get('cms.storage.uploads.path', '/storage/app/uploads');
if ($this->isPublic()) {
$uploadsPath .= '/public';
}
else {
$uploadsPath .= '/protected';
}
return Url::asset($uploadsPath) . '/';
}只需使disk_name与file_name相同,所以当文件保存在磁盘上时,它将使用original name,生成链接时也使用disk_name,这是原始的file_name
现在,您的链接和文件名是同步的,并将始终相同。
如有任何疑问,请评论。
https://stackoverflow.com/questions/51057024
复制相似问题