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

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

WBOY
WBOYOriginal
2016-06-08 17:25:211146browse
<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;
}
?>

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn