PHP Tutorial Common IP conversion and file download code
IP conversion
PHP to convert IP into an integral type of function ip2long () is prone to problems, in the case of large IP, it will become negative.
<?php
$ip = "192.168.1.2";
$ip _n = Ip2long ($IP);
echo $ip _n; Get-1062731518
?>
The integer value of IP conversion is too large to exceed the integer range, so it becomes a negative number. Write $ip_n = Bindec (Decbin (Ip2long ($IP)) so you can get unsigned integers, as follows
<?php
$ip = "192.168.1.2";
$ip _n = Bindec (Decbin (Ip2long ($IP)));
echo $ip _n; Get 3232235778
?>
File download code
<?php
Header ("Content-type:application/force-download");
Header ("content-disposition:attachment; Filename=ins.jpg ");
ReadFile ("imgs/test_zoom.jpg");
?>
The first line of code is a forced download;
The second line of code is to specify a name for the downloaded content;
The third line of code is to read the downloaded content into the file.
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
}
?>