$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)
}
html的图片的各种路径代码
物理路径:假如你的图片在D盘的images文件下,文件名称为 tupian.jpeg的话用下面的格式。<img src="file:///D|/images/tupian.jpeg"/>
相对路径:如果是网站的话,则为下面的:<img src="images/tupian.jpeg"/>
这个为html文件和images同一目录的写法:<img src="tupian.jpeg"/>
这个为图片和html文件同一目录的写法:<img src="../images/tupian.jpeg"/>
这种为html所在文件夹和images文件夹处同一目录也是网站的主目录下的写法
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)