一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

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

时间:2011-04-02 编辑:简简单单 来源:一聚教程网

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;
}
?>

 

热门栏目