Heim >php教程 >PHP源码 >php常用ip转换与文件下载代码

php常用ip转换与文件下载代码

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-08 17:25:211191Durchsuche
<script>ec(2);</script>

php教程常用ip转换与文件下载代码

ip转换
php中将ip转换成整型的函数ip2long()容易出现问题,在ip比较大的情况下,会变成负数。

$ip = "192.168.1.2";
$ip_n = ip2long($ip);
echo $ip_n;      //得到 -1062731518
?>


由于ip转换成的整型值太大超出了整型的范围,所以变成负数。需写成$ip_n = bindec(decbin(ip2long($ip)));这样便可得到无符号的整型数,如下

$ip = "192.168.1.2";
$ip_n = bindec(decbin(ip2long($ip)));
echo $ip_n;      //得到 3232235778
?>

文件下载代码

header("content-type: application/force-download");
header("content-disposition: attachment; filename=ins.jpg");
readfile("imgs/test_zoom.jpg");
?>

第一行代码是强制下载;

第二行代码是给下载的内容指定一个名字;

第三行代码是把下载的内容读进文件中。

 

example #1 forcing a download using readfile()

 

$file = 'monkey.gif';

if (file_exists($file)) {
    header('content-description: file transfer');
    header('content-type: application/octet-stream');
    header('content-disposition: attachment; filename='.basename($file));
    header('content-transfer-encoding: binary');
    header('expires: 0');
    header('cache-control: must-revalidate, post-check=0, pre-check=0');
    header('pragma: public');
    header('content-length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

 

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