Batch export files using PHP's Curl

Source: Internet
Author: User

Scene

1. You need to export a large number of files from the website, including the image format, document format and other attachment content.

2. Sub-folders to export different data

<?Phpheader ("content-type:text/html;charset= ' Utf-8 '"); include'csv.php'; $fileData=Array ('Csv/310-new.csv'=310,    'Csv/308-new.csv'=308,    'Csv/297-new.csv'=297,     'Csv/296-new.csv'=296,     'Csv/295-new.csv'=295,     'Csv/293-new.csv'=293,);foreach($fileData as$skey =$svalue) {echo"Export {$skey}\n"; if(!file_exists ("data/". $svalue)) {mkdir ("data/". $svalue); }    if(!file_exists ($skey)) {echo"file $skey is not exists \ n"; Continue; } $csvreader=NewCsvreader ($skey); $data= $csvreaderGet_data (); foreach($data as$key =$value) {$url= (Trim ($value [0])); $name= Trim ($value [1]); if(Empty ($url) or empty ($name)) {echo"url is empty \ n"; Continue; } $name= Str_replace ("/","-", $name);//string substitution, cannot have special characters in file name$ext = substr ($url, Strrpos ($url,'.')+1)///Gets the suffix downimage ($url, $svalue, $name of the original file name. "." .        $ext); Echo"export $url \ n"; }} function Downimage ($url, $sfolder, $filename) {//$filename = Iconv ("Utf-8", "GBK//IGNORE ", $filename);$folder = __dir__."/data/" ; $filename= $folder."{$sfolder}/". $filename; $ret=https_request ($url); SaveFile ($filename, $ret); //good file name after download} function SaveFile ($filename, $filecontent) {$local _file= fopen ($filename,'W'); if(false!==$local _file) {        if(false!==fwrite ($local _file, $filecontent))        {fclose ($local _file); }}}function https_request ($url) {$curl=Curl_init ();    curl_setopt ($curl, Curlopt_url, $url);    curl_setopt ($curl, Curlopt_ssl_verifypeer, FALSE);    curl_setopt ($curl, Curlopt_ssl_verifyhost, FALSE); curl_setopt ($curl, Curlopt_returntransfer,1); $output=curl_exec ($curl);    Curl_close ($curl); return$output;}

The class library for the csv.php package is

<?PHPclassCsvreader {Private$csv _file; Private$SPL _object =NULL; Private$error;  Publicfunction __construct ($csv _file ="') {        if($csv _file &&file_exists ($csv _file)) {            $ This->csv_file =$csv _file; }    }         Publicfunction Set_csv_file ($csv _file) {if(! $csv _file | |!file_exists ($csv _file)) {            $ This->error ='File Invalid'; return false; }        $ This->csv_file =$csv _file; $ This->spl_object =NULL; }         Publicfunction Get_csv_file () {return$ This-Csv_file; }        Privatefunction _file_valid ($file ="') {$file= $file? $file: $ This-Csv_file; if(! $file | |!file_exists ($file)) {            return false; }        if(!is_readable ($file)) {            return false; }        return true; }        Privatefunction _open_file () {if(!$ This-_file_valid ()) {            $ This->error ='File Invalid'; return false; }        if($ This->spl_object = =NULL) {            $ This->spl_object =NewSplfileobject ($ This->csv_file,'RB'); }        return true; }     Publicfunction Get_data ($length =0, $start =0) {        if(!$ This-_open_file ()) {            return false; } $length= $length? $length: $ This-Get_lines (); $start= $start-1; $start= ($start <0) ?0: $start; $data=Array (); $ This->spl_object->Seek ($start);  while($length--&&!$ This->spl_object->EOF ()) {$data []= $ This->spl_object->fgetcsv (); $ This->spl_object->next (); }        return$data; }         Publicfunction Get_lines () {if(!$ This-_open_file ()) {            return false; }        $ This->spl_object->seek (FileSize ($ This-csv_file)); return$ This->spl_object->key (); }         Publicfunction Get_error () {return$ This-error; }}

Batch export files using PHP's Curl

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.