从PHP导入/导出文件 [英] Import/Export file from PHP

查看:110
本文介绍了从PHP导入/导出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我问我的问题(问题)时,我觉得我的解释不清楚。
我有一个项目PHP,我没有本地环境,我在线工作:我通过Netbeans更改我的文件,我部署它,而不是在本地进行测试。我的服务器是生产的。我的数据库(MySQL - phpMyadmin)在服务器也。
我将数据从我的磁盘导入项目。然后PHP利用它通过HighChart。用户可以根据许多选择(日期,城市,客户,..)查看这些数据,最后他可以将此最终结果下载到文件csv中。
这里的问题我无法下载所有的数据,例如:在图表14万亿的营业额显示,但在文件csv它显示较少的营业额。



这是我的代码:

  class Export {
private $ _dataIN = [];
private $ _author;
private $ _filepath;

public function __construct(array $ dataIN){
$ this-> _dataIN = $ dataIN;
$ this-> setFilepath();
$ this-> prepareFile();
}
public function setFilepath(){
$ date = new \DateTime();
$ this-> _filepath ='.. / DATA / ExportFiles / ExportData'$ date-> getTimestamp();
}
public function prepareFile(){
$ file = fopen($ this-> _filepath,'w');
fputcsv($ file,array('Piece','Client','Libelle Client','Facturation','OTP','Typcde','Cdecli','Cdesap','Article' , '开发', 'Montant_fac_dev_ht', 'Montant_fac_eur', '艾维', '自然', 'Repsite', '区域', 'LRU', '合同'), ';');
foreach($ this-> _dataIN as $ key => $ statement){
fputcsv($ file,array($ statement-> getId(),$ statement-> getCustomerId ,$ statement-> getCustomerName(),$ statement-> getDate(),$ statement-> getOTP(),$ statement-> getCdetype(),$ statement-> getCdecustomer(),$ statement-> ; getSam(),$ statement-> getAmount(),$ statement-> getAmount(),$ statement-> getCount() $ statement-> getAircraft(),$ statement-> getNature(),$ statement-> getRepsite(),$ statement-> getZone(),$ statement-> getLru(),$ statement-> getContract()), ';');
}
}

public function download(){

header('Content-Description:File Transfer');
header('Content-Type:application / vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // octet-stream'
header('Content-Disposition:attachment; filename ='。basename $这 - > _filepath) 'CSV');
header('Expires:0');
header('Cache-Control:must-revalidate');
header('Pragma:public');
header('Content-Length:'。filesize($ this-> _filepath)。'Giga'); // Kilo
readfile($ this-> _filepath);
exit();

我希望这次我的解释是干净的,你可以帮助?

解决方案

更好地使用这种不存储:

  public function __construct($ dataIN = array()){
$ this-> _dataIN = $ dataIN;
$ this-> prepareFile();
}

public function prepareFile(){

$ header [] = array('Piece','Client','Libelle Client','Facturation' 'OTP', 'Typcde', 'Cdecli', 'Cdesap', '文章', '指定', '开发', 'Montant_fac_dev_ht', 'Montant_fac_eur', '阿维翁', '自然', 'Repsite',区, 'LRU', '合同');

$ data = array();

foreach($ this-> _dataIN as $ key => $ statement){
$ data [] = array($ statement-> getId(),$ statement-> ; getCustomerId(),$ statement-> getCustomerName(),$ statement-> getDate(),$ statement-> getOTP(),$ statement-> getCdetype(),$ statement-> getCdecustomer() $ statement-> getSAPcode(),$ statement-> getArticle(),$ statement-> getDesigniation(),$ statement-> getCurrency(),$ statement-> getAmount(),$ statement-> getAmountEur(),$ statement-> getAircraft(),$ statement-> getNature(),$ statement-> getRepsite(),$ statement-> getZone(),$ statement-> getLru(),$陈述式> getContract());
}

$ final_data = array_merge($ header,$ data);

$ this-> array_to_csv($ final_data,'report.csv');
}


函数array_to_csv($ array,$ download =)
{
if($ download!=)
{
header('Content-Description:File Transfer');
header(Content-type:application / vnd.ms-excel);
header('Content-Disposition:attachement; filename ='。$ download。'');
header('Content-Transfer-Encoding:binary');
}

ob_start();
$ f = fopen('php:// output','w')或show_error(Can not open php:// output);
$ n = 0;
foreach($ array as $ line)
{
$ n ++;
if(!fputcsv($ f,$ line))
{
show_error(Can not write line $ n:$ line);
}
}
fclose($ f)或show_error(无法关闭php://输出);
$ str = ob_get_contents();
ob_end_clean();

if($ download ==)
{
return $ str;
}
else
{
打印\xEF\xBB\xBF; // UTF-8 BOM
print $ str;
}
}


I think I wasn't clean in my explanation when I asked my question (the problem). I have a project PHP, I don't have a local environnement, I'm working on-line: I change my file via Netbeans and I deploy it without tested it locally. My server is of production.My Database (MySQL - phpMyadmin ) in the sever also. I import the data from my disk into the project. Then PHP exploit it via HighChart. The user can see these data per many choices (Date, city, Customer,..) and finally he can download this final result into file csv. Here the problem I can't download all the data, for example: in The Turnover display in the Chart 14Million but in the file csv it display a smaller number of turnover.

this following my code:

class Export{
    private $_dataIN=[];
    private $_author;
    private $_filepath;

    public function __construct(array $dataIN) {
        $this->_dataIN = $dataIN;
        $this->setFilepath();
        $this->prepareFile();
    }
    public function setFilepath() {
        $date = new \DateTime();
        $this->_filepath='../DATA/ExportFiles/ExportData'.$date->getTimestamp();
    }
    public function prepareFile(){
        $file = fopen($this->_filepath, 'w');
        fputcsv($file, array('Piece', 'Client', 'Libelle Client', 'Facturation','OTP','Typcde','Cdecli','Cdesap','Article','Designation','Dev','Montant_fac_dev_ht','Montant_fac_eur','Avion','Nature','Repsite','Zone','LRU','Contract'),';');        
        foreach ($this->_dataIN as $key => $statement) {
            fputcsv($file, array($statement->getId(), $statement->getCustomerId(), $statement->getCustomerName(), $statement->getDate(), $statement->getOTP(), $statement->getCdetype(), $statement->getCdecustomer(), $statement->getSAPcode(), $statement->getArticle(), $statement->getDesigniation(), $statement->getCurrency(), $statement->getAmount(), $statement->getAmountEur(), $statement->getAircraft(), $statement->getNature(), $statement->getRepsite(), $statement->getZone(), $statement->getLru(),$statement->getContract()),';');
        }
    }

    public  function download() {        

        header('Content-Description: File Transfer');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//octet-stream' 
        header('Content-Disposition: attachment; filename='.basename($this->_filepath).'.csv'); 
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($this->_filepath) . 'Giga'); //Kilo
        readfile($this->_filepath);
        exit();

I hope that this time my explanation is clean and you can help?

解决方案

Better to use like this without storing:

public function __construct($dataIN = array()) {
    $this->_dataIN = $dataIN;
    $this->prepareFile();
}

public function prepareFile(){

    $header[] = array('Piece', 'Client', 'Libelle Client', 'Facturation','OTP','Typcde','Cdecli','Cdesap','Article','Designation','Dev','Montant_fac_dev_ht','Montant_fac_eur','Avion','Nature','Repsite','Zone','LRU','Contract');        

    $data = array();

    foreach ($this->_dataIN as $key => $statement) {
        $data[] = array($statement->getId(), $statement->getCustomerId(), $statement->getCustomerName(), $statement->getDate(), $statement->getOTP(), $statement->getCdetype(), $statement->getCdecustomer(), $statement->getSAPcode(), $statement->getArticle(), $statement->getDesigniation(), $statement->getCurrency(), $statement->getAmount(), $statement->getAmountEur(), $statement->getAircraft(), $statement->getNature(), $statement->getRepsite(), $statement->getZone(), $statement->getLru(),$statement->getContract());
    }

    $final_data = array_merge($header, $data);

    $this->array_to_csv($final_data, 'report.csv');
}


function array_to_csv($array, $download = "")
{   
    if ($download != "")
    {    
        header('Content-Description: File Transfer');
        header("Content-type: application/vnd.ms-excel");
        header('Content-Disposition: attachement; filename="' . $download . '"');
        header('Content-Transfer-Encoding: binary');
    }        

    ob_start();
    $f = fopen('php://output', 'w') or show_error("Can't open php://output");
    $n = 0;        
    foreach ($array as $line)
    {
        $n++;
        if ( ! fputcsv($f, $line))
        {
            show_error("Can't write line $n: $line");
        }
    }
    fclose($f) or show_error("Can't close php://output");
    $str = ob_get_contents();
    ob_end_clean();

    if ($download == "")
    {
        return $str;    
    }
    else
    {    
        print "\xEF\xBB\xBF"; // UTF-8 BOM
        print $str;
    }        
}

这篇关于从PHP导入/导出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆