PHP Curl mimics ftp File upload Code _php tutorial

Source: Internet
Author: User
Tags ftp file
PHP Tutorial Curl mimics ftp file upload code




if (Isset ($_post[' submit ')) {
if (!empty ($_files[' upload ' [' name '])) {
$ch = Curl_init ();
$localfile = $_files[' upload ' [' tmp_name '];
$fp = fopen ($localfile, ' R ');
curl_setopt ($ch, Curlopt_url, ' ftp://ftp_login:password@ftp.domain.com/'. $_files[' upload ' [' name ']);
curl_setopt ($ch, curlopt_upload, 1);
curl_setopt ($ch, Curlopt_infile, $fp);
curl_setopt ($ch, Curlopt_infilesize, FileSize ($localfile));
Curl_exec ($ch);
$error _no = Curl_errno ($ch);
Curl_close ($ch);
if ($error _no = = 0) {
$error = ' file uploaded succesfully. ';
} else {
$error = ' File upload error. ';
}
} else {
$error = ' Please select a file. ';
}
}
?>

Okay, it's closed down into classes.

Class Curl_ftp
{

Private $ftpname; FTP User Name
Private $ftppaw; FTP password
Private $urlftp; FTP address
Private $filename; Filename

Public __construct ($name, $password, $ftp)
{
$this->ftpname = $name;
$this->ftppaw = $password;
$this->urlftp = $ftp;
$this->filename = $filename;
}

Public Function getftp ()
{
if (Isset ($_post[' submit '))
{
if (!empty ($_files[' upload ' [' name ']))
{
$ch = Curl_init ();
$this->filename = $_files[' upload ' [' tmp_name '];
$fp = fopen ($this->filename, ' R ');
curl_setopt ($ch, Curlopt_url, $this->ftp $this->filename);
curl_setopt ($ch, Curlopt_userpwd, "$name:p assword");
curl_setopt ($ch, curlopt_upload, 1);
curl_setopt ($ch, Curlopt_infile, $fp);
curl_setopt ($ch, Curlopt_infilesize, FileSize ($this->filename));
Curl_exec ($ch);
$error _no = Curl_errno ($ch);
Curl_close ($ch);
if ($error _no = = 0)
{
$error = ' File upload succeeded ';
}
Else
{
$error = ' File upload failed ';
}
}
Else
{
$error = ' no file selected ';
}
}
}
}


http://www.bkjia.com/PHPjc/444823.html www.bkjia.com true http://www.bkjia.com/PHPjc/444823.html techarticle PHP Tutorial Curl mimics ftp File upload code body form action=curlupload.php method=post enctype=multipart/form-data Div label for= Uploadselect File/label input name=upload type=file ...

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