[英]How to copy image from DB to new path
我正在尝试使用存储在我的数据库中的路径复制图像。 我试过这个,但它抛出“数组到字符串转换”错误。 我以前从来没有处理过这个。 这是我的代码。
$record = AboutPageIntro::find($id);
$img_name = $record->image;
$ext = explode('.', $img_name);
Storage::disk('local')->copy(public_path($record->image), public_path('uploads/about_page/intro/' . uniqid() . '.' . $ext));
您正在尝试从explode('.', $img_name);
到最后一行的字符串。
$record = AboutPageIntro::find($id);
$img_name = $record->image;
$ext = explode('.', $img_name); // This is an array like ['image_name', 'ext]
Storage::disk('local')->copy(
public_path($record->image),
public_path('uploads/about_page/intro/' . uniqid() . '.' . $ext[count($ext)-1])
); // You'll need to address the last item in the array by its index like this
也就是说, pathinfo(...)
是这里更安全的选择。
$record = AboutPageIntro::find($id);
$img_name = $record->image;
$ext = pathinfo($img_name, PATHINFO_EXTENSION);
Storage::disk('local')->copy(
public_path($record->image),
public_path('uploads/about_page/intro/' . uniqid() . '.' . $ext
);
我认为这是一个使用explode 的简单问题,因为它会根据除法参数返回一个字符串数组。
我对 OOP 不太了解,但我可以建议使用 str_replace 或 trim 删除 $img_name 变量中的 '.'。
$record = AboutPageIntro::find($id);
$img_name = $record->image;
$ext = str_replace('.',"",$img_name);
Storage::disk('local')->copy(public_path($record->image), public_path('uploads/about_page/intro/' . uniqid() . '.' . $ext));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.