Remote File download code _php tutorial

Source: Internet
Author: User
Tags save file
Remote File download code here to provide you with a remote file download code, we can put the remote file in PHP to download the local directory, the following is a download remote server file code class.

Remote File download code
Here to provide you with a remote file download code, we can take the remote file with PHP tutorial download to the local designated directory, below is a download remote server file code class.

Class Download
{
var $url;//Remote file address

var $file _name = "hdwiki.zip";//File name downloaded

var $save _path = "./www.bkjia.c0m";//download to local file path

var $localfile;//the path and name of the downloaded to local file

var $warning;//warning message

var $redown =0;//whether to re-download


/* 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);
}
}

/* Check URL legitimacy function */
function Checkurl () {
Return Preg_match ("/^ (HTTP|FTP) (://) ([a-za-z0-9-_]+[./]+[w-_/]+.*) +$/i", $this->url);
}

Download files to Local

function DownloadFile ()
{
Detection variables

$this->localfile = $this->save_path. " /". $this->file_name;
if ($this->url = = "" | | $this->localfile = = "") {
$this->warning = "ERROR: variable is set incorrectly.";
return $this->warning;
}

if (! $this->checkurl ()) {
$this->warning = "Error:url". $this->url. "Illegal.";
return $this->warning;
}

if (file_exists ($this->localfile)) {
if ($this->redown)
{
Unlink ($this->localfile);
}
Else
{
$this->warning = "Warning: Upgrade file". $this->localfile. "Already exists! Re-download ";
return $this->warning;
Exit ("Error: Local file".) $this->localfile. "Already exists, please remove or rename after re-run this program.");

}
}

Open a remote file

$fp = fopen ($this->url, "RB");
if (! $fp) {
$this->warning = "Error: Open remote file". $this->url. "Failure.";
return $this->warning;
}

Open Local File

$SP = fopen ($this->localfile, "WB");
if (! $sp) {
$this->warning = "Error: Open local file". $this->localfile. "Failure.";
return $this->warning;
}

Download remote Files

echo "Downloading remote files, please wait";

while (!feof ($fp)) {
$tmpfile. = Fread ($fp, 1024);
echo strlen ($tmpfile);

}
Save file to Local

Fwrite ($SP, $tmpfile);
Fclose ($FP);
Fclose ($SP);

if ($this->redown)
$this->warning = "Success: Download file again". $this->file_name. "Success";
Else
$this->warning = "Success: Download File". $this->file_name. "Success";

return $this->warning;
}
}

http://www.bkjia.com/PHPjc/631992.html www.bkjia.com true http://www.bkjia.com/PHPjc/631992.html techarticle Remote File download code here to provide you with a remote file download code, we can put the remote file in PHP to download the local directory, the following is a download remote services ...

  • 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.