$data = file_get_contents("/path/to/photo.jpg")// 读文件内容
$name = 'myphoto.jpg'
force_download($name, $data)
function force_download($filename = '', $data = '')
{
if ($filename == '' OR $data == '')
{
return FALSE
}
// Try to determine if the filename includes a file extension.
// We need it in order to set the MIME type
if (FALSE === strpos($filename, '.'))
{
return FALSE
}
// Grab the file extension
$x = explode('.', $filename)
$extension = end($x)
$mimes = array( 'bmp' =>array('image/bmp','application/octet-stream'),
'gif' =>array('image/gif','application/octet-stream'),
'jpeg' => array('image/jpeg', 'image/pjpeg', 'application/octet-stream'),
'jpg' => array('image/jpeg', 'image/pjpeg', 'application/octet-stream'),
'jpe' => array('image/jpeg', 'image/pjpeg', 'application/octet-stream'),
'png' => array('image/png', 'image/x-png', 'application/octet-stream')
)
// Set a default mime if we can't find it
if ( ! isset($mimes[$extension]))
{
$mime = 'application/octet-stream'
}
else
{
$mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension]
}
// Generate the server headers
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
header('Content-Type: "'.$mime.'"')
header('Content-Disposition: attachmentfilename="'.$filename.'"')
header('Expires: 0')
header('Cache-Control: must-revalidate, post-check=0, pre-check=0')
header("Content-Transfer-Encoding: binary")
header('Pragma: public')
header("Content-Length: ".strlen($data))
}
else
{
header('Content-Type: "'.$mime.'"')
header('Content-Disposition: attachmentfilename="'.$filename.'"')
header("Content-Transfer-Encoding: binary")
header('Expires: 0')
header('Pragma: no-cache')
header("Content-Length: ".strlen($data))
}
exit($data)
}
直接“右键”→点击“图片另存为”→选择保存目录→点击“保存”。如果网站禁用了右键功能,而不能保存图片,可以采取如下方法:
点击浏览器顶部的“查看”→点击“启用编辑模式”→再选中要保存的图片→点击“右键”→点击“图片另存为”→选中保存目录→点击“保存”。
希望这个方法对你有用吧!
<script>function savepic(o){
pic=window.open(o.src,"demo")
setTimeout('pic.document.execCommand("saveas")',0)
}
</script>
<iframe name=demo style="display:none"></iframe>
<img src="图片" style="cursor:pointer" alt="点击下载" border="0" onclick="savepic(this)/>