A function for obtaining remote files (applicable to both Linux and Windows)

Source: Internet
Author: User
To obtain the remote file, write the following small function, which is considered in all aspects. let's take a look: functiongetpic ($ url, $ dir, $ name) {get the url file, and store it in the dir directory, with name as the file name. If the default file name is used, the original file name is retained. $ Tmpsplit (, $ url); $ fname $ tmp [count ($ tmp)-1]; get to get the remote file, write the following small function, we have considered all aspects. let's take a look:
Function getpic ($ url, $ dir, $ name ){
// Obtain the url file and store it in the dir directory, with name as the file name.
// If the default file name is used, the original file name is retained.
$ Tmp = split ("/", $ url );
$ Fname = $ tmp [count ($ tmp)-1]; // get the file name
If (empty ($ name )){
$ Name = $ fname; // Set the file name
} Else {// Determine whether the file extension has been changed. if so, change it to the file extension.
$ Tmp1 = split ("\.", $ fname );
$ Tmp2 = split ("\.", $ name );
If (strcmp ($ tmp1 [1], $ tmp2 [1])! = 0) $ name = $ tmp2 [0]. ".". $ tmp1 [1];
}
If (strstr ($ dir, "\") {// specifies the Directory in Windows.
If (substr ($ dir,-2) = "\") $ file = $ dir. $ name; else $ file = $ dir. "\\". $ name;
} Else {
If (substr ($ dir, "/") {// specifies the Directory in Linux.
If (substr ($ dir,-1) = "/") $ file = $ dir. $ name; else $ file = $ dir. "/". $ fname;
}
Else {// an invalid directory is given and the function exits.
Echo "error directory! ";
Return;
}
}
If (! (File_exists ("$ dir") & is_dir ("$ dir") {// create a directory if the directory does not exist
Mkdir ($ dir, 0777 );
}
If (file_exists ($ file) return; // if the object exists, discard the operation.
$ Fpo = fopen ($ file, "wb ");
If (! $ Fpo ){
Echo "Sorry, create file: $ file failure! Check your right! ";
Return;
}
$ Num = 0;
Do {
$ Num ++;
$ Fpi = fopen ($ url, "r ");
If (! $ Fpi) echo "open remote file falure... Retry! \ R \ n ";
} While (! $ Fpi & $ num <3); // you can open a file three times at most. if the file is exceeded, it exits.
If (! Fpi ){
Echo "The network is buzy or Bad URL! ";
Return;
} Else echo "Open file OK! Now transfer ....";
While (! Feof ($ fpi )){
$ Cont = fread ($ fpi, 128 );
Fwrite ($ fpo, $ cont, 128 );
}
Fclose ($ fpi );
Fclose ($ fpo );
Echo "Success! ";
}
Related Article

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.