Remote File Download Code

Source: Internet
Author: User

Remote File Download Code: here we provide you with a remote file download code. We can use php to download remote files to a specified local directory, the following is a code class for downloading remote server files.

Remote File Download Code
Here we provide you with a remote file download code. We can use the php tutorial to download remote files to a specified local directory. Below is a code class for downloading remote server files.

Class download
{
Var $ url; // remote file address

Var $ file_name = "hdwiki.zip"; // The Name Of The downloaded file.

Var $ save_path = "./www. bKjia. c0m"; // download to local file path

Var $ localfile; // The path and name of the downloaded local file

Var $ warning; // warning information

Var $ redown = 0; // whether to download again


/* Initialize */
Function seturl ($ url)
{
If (! Empty ($ url) $ this-> url = $ url;
}

Function setfilename ($ file_name)
{
If (! Empty ($ file_name) $ this-> file_name = $ file_name;
}

Function setsavepath ($ save_path)
{
If (! Empty ($ save_path) $ this-> save_path = $ save_path;
}

Function setredown ($ redown)
{
If (! Empty ($ redown) $ this-> redown = $ redown;
}

Function download ($ url, $ redown = 0, $ save_path = 0, $ file_name = 0)
{
$ This-> seturl ($ url );
$ This-> setfilename ($ file_name );
$ This-> setsavepath ($ save_path );
$ This-> setredown ($ redown );
If (! File_exists ($ this-> save_path ))
{
$ Dir = explode ("/", $ this-> save_path );
Foreach ($ dir as $ p)
Mkdir ($ p );
}
}

/* Url validity check function */
Function checkurl (){
Return preg_match ("/^ (http | ftp) (: //) ([a-za-z0-9-_] + [. /] + [w-_/] +. *) + $/I ", $ this-> url );
}

// Download the object to your local device

Function downloadfile ()
{
// Check Variables

$ This-> localfile = $ this-> save_path. "/". $ this-> file_name;
If ($ this-> url = "" | $ this-> localfile = ""){
$ This-> warning = "error: variable setting error .";
Return $ this-> warning;
}

If (! $ This-> checkurl ()){
$ This-> warning = "error: url". $ this-> url. "invalid .";
Return $ this-> warning;
}

If (file_exists ($ this-> localfile )){
If ($ this-> redown)
{
Unlink ($ this-> localfile );
}
Else
{
$ This-> warning = "warning: Upgrade File". $ this-> localfile. "already exists! <A href = '? Action = download & redown = 1 'target = '_ self'> download again </a> ";
Return $ this-> warning;
// Exit ("error: local file". $ this-> localfile. "already exists. Please delete or rename the name and run the program again .");

}
}

// Open a Remote File

$ Fp = fopen ($ this-> url, "rb ");
If (! $ Fp ){
$ This-> warning = "error: open Remote File". $ this-> url. "failed .";
Return $ this-> warning;
}

// Open a local file

$ Sp = fopen ($ this-> localfile, "wb ");
If (! $ Sp ){
$ This-> warning = "error: open local file". $ this-> localfile. "failed .";
Return $ this-> warning;
}

// Download a Remote File

// Echo "the remote file is being downloaded. Please wait ";

While (! Feof ($ fp )){
$ Tmpfile. = fread ($ fp, 1024 );
// Echo strlen ($ tmpfile );

}
// Save the file to your local device

Fwrite ($ sp, $ tmpfile );
Fclose ($ fp );
Fclose ($ sp );

If ($ this-> redown)
$ This-> warning = "success: download the file again". $ this-> file_name. "successful ";
Else
$ This-> warning = "success: Download file". $ this-> file_name. "successful ";

Return $ this-> warning;
}
}

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.