gpt4 book ai didi

php - 在laravel中上传图片并将名称插入数据库

转载 作者:行者123 更新时间:2023-11-29 21:00:39 25 4
gpt4 key购买 nike

我正在尝试上传文件夹中的图像并将其名称插入数据库中。像这样的 C:\xampp\tmp\phpAA38.tmp 存储在数据库中,而不是图像名称,并且没有上传任何内容。

 $imageName = $request->file('image');
if($imageName!==null)
{
$imageName->getClientOriginalExtension();
$imageName->move(
base_path() . '/public/images/new', $imageName);
}
$store= Result::insert(['Name'=>$name,Image'=>$imageName]);

我使用enctype="multipart/form-data"确保表单接受文件

最佳答案

您的代码中有一些错误。例如

  • 您没有保存或使用文件扩展名
  • 您没有正确移动文件

看看这是否效果更好:

$imageName = $request->file('image');
if($imageName!==null)
{
// get the extension
$extension = $imageName->getClientOriginalExtension();
// create a new file name
$new_name = date( 'Y-m-d' ) . '-' . str_random( 10 ) . '.' . $extension;
// move file to public/images/new and use $new_name
$imageName->move( public_path('images/new'), $imageName);
}
$store= Result::insert(['Name'=>$name,Image'=>$new_name]);

关于php - 在laravel中上传图片并将名称插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37254654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com