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;
- }
- ?>
新聞熱點
疑難解答