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.
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;
}
}