我正在尝试编写一个脚本来从Excel工作表上传包含产品名称,价格和图像的批量产品。图像是Dropbox图像共享链接。如何从Dropbox网址下载这些图片,将其保存在我的服务器上,然后将图片网址上传到我的数据库?
Excel表格阅读:Maatwebsite/Laravel-Excel
通用上传代码:
public function productUpload(Request $request){
if($request->hasFile('products')){
$path= $request->file('products')->getRealPath();
$data = Excel::load($path)->get();
if($data->count()){
foreach ($data as $key => $value) {
//download the image create thumbain and store to /images/product/thumbnail folder and get the link,
$thumbnail = //here will be the path for the thumbnail
//Original image
$original = ;
$data['original']= $original;
$data['thumbnail']=$thumbnail;
$data['name']=$value->name;
$data['price']= $value->price;
Product::create($data);
}
return redirect()->back()->with('success','Product has been uploaded');
}
}
}
excel表格中的图片网址与此类似https://www.dropbox.com/s/x2tbsy49sraywvv/Ruby12.jpg?dl=0此文件已被删除。
答案 0 :(得分:0)
您可以通过将dl = 1添加到保管箱链接Source
来直接下载图片您可以使用file_get_contents php命令将映像下载到服务器。 Source并使用Storage Facade将其存储在服务器中
$url = "yoururl"; //check if has the dl=0 and change it to 1 or add dl=1
$contents = file_get_contents($url);
$name = 'image.png';//the name of the image to store on disk.
\Storage::disk('yourdisk')->put($name, $contents);
此外,您将获得这样的图像的完整路径。
$thumbnail = storage_path('yourdir/'.$name)
使用我在Dropbox上的文件测试它并且没有问题。 在下载图像时,您仍需要处理错误和延迟等问题。