A function to get the remote file (both Linux and Windows apply)

Source: Internet
Author: User
Tags create directory return split linux
window| function to get the remote file, wrote a small function of the following, basic aspects are considered, we look at it:
function Getpic ($url, $dir, $name) {
Gets the URL file and then stores it in the Dir directory, with name as the filename.
If the default file name, the original file name is retained.
$tmp =split ("/", $url);
$fname = $tmp [Count ($tmp) -1];//gets the file name
if (empty ($name)) {
$name = $fname;//Set the filename.
}else{//determines if the file's extension has been changed, and if so, the file's own extension.
$tmp 1=split ("\.", $fname);
$tmp 2=split ("\.", $name);
if (strcmp ($tmp 1[1], $tmp 2[1])!=0) $name = $tmp 2[0]. "." $tmp 1[1];
}
if (Strstr ($dir, "\ \")) {//description is a directory in Windows environment
if (substr ($dir, -2) = = "\") $file = $dir. $name; else $file = $dir. " \ ". $name;
}else{
if (substr ($dir, "/")) {//description is a directory in the Linux environment
if (substr ($dir, -1) = = "/") $file = $dir. $name; else $file = $dir. " /". $fname;
}
else{//gave an illegal directory, the function exited
echo "Error directory!";
return;
}
}
if (!) ( File_exists ("$dir") && is_dir ("$dir")) {//If directory does not exist, create directory
mkdir ($dir, 0777);
}
if (file_exists ($file)) return;//If the file exists, abort 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); The file tries to open up to three times, and then 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.