图片远程保存,浏览器直接下载
???? $url = '遠程圖片';
?? ??? ?$url ?? ? = preg_replace( '/(?:^[\'"]+|[\'"\/]+$)/', '',$url] ); //url過慮
?? ??? ?$fn ?? ??? ?= "Dowload/voluntary.jpg"; //保存路徑
?? ??? ?header('Content-type:text/html;charset=utf-8');?? ?
?? ??? ?$image_info = getimagesize($url);?? ??? ?
?? ??? ?$base64_image_content = "data:{$image_info['mime']};base64," . chunk_split(base64_encode(file_get_contents($url))); //編碼
?? ??? ?if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){??? //保存本地
?? ??? ??? ?@unlink($fn);
?? ??? ??? ?$type = $result[2];
?? ??? ??? ?$a = file_put_contents($fn, base64_decode(str_replace($result[1], '', $base64_image_content))); //保存
?? ??? ??? ?//文件下載
?? ??? ??? ?header('Content-type: application/x-jpg');
?? ??? ??? ?//下載顯示的名字
?? ??? ??? ?header('Content-Disposition: attachment; filename="voluntary.jpg"');
?? ??? ??? ?ob_clean(); ?
? ?? ??? ??? ?flush(); ?
? ?? ??? ??? ?readfile($fn); //瀏覽器直接下載
?? ??? ??? ?exit();
?? ??? ?}
?
轉載于:https://www.cnblogs.com/myJuly/p/10077826.html
總結
以上是生活随笔為你收集整理的图片远程保存,浏览器直接下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java工程师之SpringBoot系列
- 下一篇: 称为的国之重器弓弩到底有什么不同?