Heim  >  Artikel  >  Backend-Entwicklung  >  php下载图片

php下载图片

WBOY
WBOYOriginal
2016-06-23 13:40:031037Durchsuche

现在我在页面上做一个按钮,只要用户一点,我在后台用php下载指定的图片到用户的电脑上,怎么实现这个功能,不要下载到服务器上,而是直接将我直接下载图片下载到用户电脑上


回复讨论(解决方案)

<a href="download.php">下??片</a>


download.php
<?php  $file = '1.jpg';  if(file_exists($file)){      header('content-type:application/octet-stream');      header('content-disposition:attachment; filename='.basename($file));      header('content-length:'.filesize($file));      readfile($file);  }  ?> 

<a href="download.php">下??片</a>


download.php
<?php  $file = '1.jpg';  if(file_exists($file)){      header('content-type:application/octet-stream');      header('content-disposition:attachment; filename='.basename($file));      header('content-length:'.filesize($file));      readfile($file);  }  ?> 


再追问一下,我这个服务器要去另外一个图片服务器取图片,并且要重命名这个文件,然后再将这个文件直接传送到用户电脑上,我这边有图片服务器的url,这个要怎么处理,我不想将图片存本地服务器

<?php  $file = file_get_contents('http://photo.com/1.jpg');  // ?片服?器的?片地址$name = '2.jpg'; // 你想改的名字if($file){      header('content-type:application/octet-stream');      header('content-disposition:attachment; filename='.$name);      header('content-length:'.strlen($file));  	echo $file;}  ?> 

<?php  $file = file_get_contents('http://photo.com/1.jpg');  // ?片服?器的?片地址$name = '2.jpg'; // 你想改的名字if($file){      header('content-type:application/octet-stream');      header('content-disposition:attachment; filename='.$name);      header('content-length:'.strlen($file));  	echo $file;}  ?> 


这个方法发现两个问题:1、有些图片下载数据不完全,导致图片损坏,二、多图片下载,只会下载最后一张图片

你?片很大??
多?片下?,你的?片名?是一??

最初图片上传档名先用时间加乱数重新命名,这样不容易出错

你?片很大??
多?片下?,你的?片名?是一??


每张大概三四百k,一次要下四五百张,服务器上存的图片是时间戳,但是我现在要根据数据库的一个字段重新命名下载下来

用??按?,?下??百??片?用??

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn