首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >10月CMS扩展系统/模型/文件

10月CMS扩展系统/模型/文件
EN

Stack Overflow用户
提问于 2018-06-27 07:34:41
回答 1查看 558关注 0票数 1

在使用System/model/ file时,我试图保留原始文件名,我得到了以下代码来扩展该模型:

代码语言:javascript
运行
复制
    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;

它与文件本身一起工作,它保留原始名称,但问题是连接到附加文件的链接仍在生成中。我心烦意乱,但做不到这份工作。有人能详细说明如何修复它吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-30 20:34:39

哦,我看到了,它似乎试图使用disk_name来生成URL

所以你的形象保存得很好

代码语言:javascript
运行
复制
//$this->putFile($realPath, $this->disk_name);
$this->putFile($realPath, $this->file_name);

但你只需要换一句..。只需撤消您的更改并进行此更改即可。

代码语言:javascript
运行
复制
 $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.phpmodules\system\models\File.php

代码语言:javascript
运行
复制
/**
 * 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_namefile_name相同,所以当文件保存在磁盘上时,它将使用original name,生成链接时也使用disk_name,这是原始的file_name

现在,您的链接和文件名是同步的,并将始终相同。

如有任何疑问,请评论。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51057024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档