PHP common IP conversion and file download code

Source: Internet
Author: User
Tags php tutorial readfile

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.