我试图将图片上传到Laravel中的模型中,所以我决定使用Czim / Paperclip库。这是我的配置文件:
在我的迁移文件中,添加了以下内容:
$table->string('image_file_name')->nullable();
$table->integer('image_file_size')->nullable();
$table->string('image_content_type')->nullable();
$table->timestamp('image_updated_at')->nullable();
$table->string('image_variants', 255)->nullable();
在我的app / config / filesystems.php中,我添加了回形针:
'paperclip' => [
'driver' => 'local',
'root' => public_path('storage'),
'url' => env('APP_URL') . '/storage',
'visibility' => 'public',
],
我的带有回形针参考的模型:
public function __construct(array $attributes = [])
{
$this->hasAttachedFile('image', [
'variants' => [
'medium' => [
'auto-orient' => [],
'resize' => ['dimensions' => '300x300'],
],
'thumb' => '100x100',
],
'attributes' => [
'variants' => true,
],
]);
parent::__construct($attributes);
}
问题是这样的:如果我上传png或jpg图片,则文件将存储在此路径public\storage\App\User\000\000\007
中,但不会以这种格式存储。我在那里有3个文件夹(原始,中,拇指),其中包含a7b7a025f3630860.txt
文件。在txt中,其为上传图片的名称。
我最终在表单中添加了enctype="multipart/form-data"
,但是添加了此enctype后,我的图像为空(它们没有上传)。如果我从表单中删除enctype,它们将在该路径中上传,但以.txts格式上传。
这是我的存储方法中的dd($request->image)
:
"image" => UploadedFile {#295 ▼
-test: false
-originalName: "5.png"
-mimeType: "image/png"
-error: 0
#hashName: null
path: "C:\xampp\tmp"
filename: "php6E88.tmp"
basename: "php6E88.tmp"
pathname: "C:\xampp\tmp\php6E88.tmp"
extension: "tmp"
realPath: "C:\xampp\tmp\php6E88.tmp"
aTime: 2019-06-27 10:40:36
mTime: 2019-06-27 10:40:36
cTime: 2019-06-27 10:40:36
inode: 0
size: 9486
perms: 0100666
owner: 0
group: 0
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
linkTarget: "C:\xampp\tmp\php6E88.tmp"
}
控制器方法如下:
$receipe = new Receipt();
$receipe->user_id = Auth::user()->id;
$receipe->name = $request->get('name');
$receipe->body = $request->get('body');
$receipe->url = $request->get('url');
$receipe->cooking_time = $request->get('cooking_time');
$receipe->image = $request->get('image');
$receipe->save();
我在做什么错? 谢谢
答案 0 :(得分:0)
通过从以下位置更改控制器中的存储方法来修复
$receipe->image = $request->get('image');
至$receipe->image = $request->file('image');