php常用ip转换与文件下载代码
ip转换:php中将ip转换成整型的函数ip2long()容易出现问题,在ip比较大的情况下,会变成负数,代码如下:
<?php $ip = "192.168.1.2"; $ip_n = ip2long($ip); echo $ip_n; //得到 -1062731518
由于ip转换成的整型值太大超出了整型的范围,所以变成负数,需写成$ip_n = bindec(decbin(ip2long($ip)));这样便可得到无符号的整型数,如下代码:
<?php $ip = "192.168.1.2"; $ip_n = bindec(decbin(ip2long($ip))); echo $ip_n; //得到 3232235778
文件下载代码如下:
<?php 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()
<?php $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; }
本文地址:http://www.phprm.com/scxz/fs4178.html
转载随意,但请附上文章地址:-)