2

その場でcsvにエクスポートする方法を探していて、これに出会いました。

    $list = array (
                   array('aaa', 'bbb', 'ccc', 'dddd'),
                   array('123', '456', '789'),
                   array('"aaa"', '"bbb"')
                   );
    $fp = fopen('path_to_file/file.csv', 'w');

    foreach ($list as $fields) {
        fputcsv($fp, $fields);
    }

    fclose($fp);

php.netページから取得した基本的なコードです。とにかく、自分のニーズに使用する前に試してみました。しかし、次のエラーが発生し続けます。

PHP Warning:  fopen(path_to_file): failed to open stream
fputcsv() expects parameter 1 to be resource, boolean given
fclose() expects parameter 1 to be resource, boolean given

とにかくその場で新しいファイルを作成したいので、なぜ fopen() エラーが表示されるのかわかりません。

これはファイル許可エラーであると想定しても安全ですか?

また、php.netページの正確なコードを使用しているため、fopen()の問題のために他のエラーが表示されていると想定しています。

編集。

もう少し情報を掲載した方が良さそうです。

関連するクラス。

 class transData{
var $connection;
var $date;
var $data;

function __construct($date){
    $this->date = $date;
    return;
}

function getData(){ //this method just pulls the results of the query and returns them as an array
    global $connection;
    $date=$this->date;

    //get num row for each date/fromfile combo, one with the most should be the most uptodate.
    // count(fromfile) for specific date, one with most will be the most up to date, if more than one then take the one with highest appended int
    $count=array();
    $x=0;
    $mysqli_result=$connection->query("select fromfile,count(fromfile) from transdata where soldtime like '%".$date."%' group by fromfile");
    while($row=$mysqli_result->fetch_row()){
        $count[$row[0]]=$row[1];
    }
    $mysqli_result->free();
    //got the data in array, now rearrange to get one with highest count
    arsort($count);
    $fromFile=key($count);


    $temp=array();
    $x=0;
    $mysqli_result=$connection->query("select * from transdata where soldtime like '%".$date."%' and fromFile='".$fromFile."' order by soldtime desc");
    while($row=$mysqli_result->fetch_row()){
        $temp[$x]['id']=$row[0];
        $temp[$x]['departure']=$row[1];
        $temp[$x]['type']=$row[2];
        $temp[$x]['typeText']=$row[3];
        $temp[$x]['price']=$row[4];
        $temp[$x]['soldTime']=$row[5];
        $temp[$x]['dateAdded']=$row[6];
        $temp[$x]['fromFile']=$row[7];
        $x++;

    }
    $this->data = $temp;
    $mysqli_result->free();
    return;
}




function dayTotal($noDays){
    //$list=array();
    $html="<table class=\"paxdata\">";

    $html.="<tr><th>Day</th><th>Date</th><th>No. Sold</th><th>Total Price(£)</th><th></th></tr>";
    //$list=array('Day','Date','No. Sold','Total Price(£)');
    //build list of dates to work with
    $inc=0;
    $hours=array();$hours[0]=0;$hours[1]=24;$hours[2]=48;$hours[3]=72;$hours[4]=96;$hours[5]=120;$hours[6]=144;$hours[7]=168;

    while($inc!=$noDays){

        $date=date('Y/n/j',time() - 60 * 60 * $hours[$inc]);        //format for calling class transData()
        $formatDate=date('Y-m-d',time() - 60 * 60 * $hours[$inc]);  //format for displaying date in table
        $day=date('l',time() - 60 * 60 * $hours[$inc]);             //text day name

        $link=explode('/',$date);

        //call to this class to get all the data for us
        $getData = new transData($date);
        $all = $getData->getData();
        $stuff = $getData->data;        

        $x=0;$price=0;
        foreach($stuff as $v){
            $price=$price+$v['price'];
            $x++;

        }
        $totalSold=$totalSold+$x;
        $totalPrice=$totalPrice+$price;

        $checkOdd=new numeric();
        $odd=$checkOdd->is_odd($inc);
        if($odd==1){$html.="<tr class=\"odd\">";}else{$html.="<tr class=\"even\">";}
        $html.="<td width=\"100px\">".$day."</td><td width=\"100px\">".$formatDate."</td><td>".$x."</td><td>".fixedToFloat($price)."</td><td><a href=\"index.php?module=chooseDate&year=$link[0]&month=$link[1]&day=$link[2]\">More info?</a></td></tr>";
        //$list=array($day,$formatDate,$x,$price);
        $inc++;
    }
    $html.="<tr><td></td><th>Totals:</th><th>".$totalSold."</th><th>".fixedToFloat($totalPrice)."</th></tr>";
    $html.="</table>";
    echo $html;


    $list = array (
                   array('aaa', 'bbb', 'ccc', 'dddd'),
                   array('123', '456', '789'),
                   array('"aaa"', '"bbb"')
                   );
    $fp = fopen('~/file.csv', 'w');
    if ($fp != false){
        foreach ($list as $fields) {
            fputcsv($fp, $fields);
        }
    }
    fclose($fp);

}

  }

これによりブラウザ経由で呼び出されます。

$date=date('Y/n/j');

$getData = new transData($date);
$all = $getData->getData();
$getData->dayTotal(7);

/var/log/system.log の応答

   PHP Notice:  Undefined variable: totalSold in /Users/me/Sites/KF/sales/specificClasses.php on line 218
   PHP Notice:  Undefined variable: totalPrice in /Users/me/Sites/KF/sales/specificClasses.php on line 219
   PHP Warning:  fopen(~/Sites/KF/file.csv): failed to open stream: No such file or directory in /Users/me/Sites/KF/sales/specificClasses.php on line 238
   PHP Warning:  fclose() expects parameter 1 to be resource, boolean given in /Users/me/Sites/KF/sales/specificClasses.php on line 244
4

2 に答える 2

5

fopenのPHP.netドキュメントから:

戻り値

成功した場合はファイルポインタリソースを返し、エラーの場合はFALSEを返します。

コードで「path_to_file/file.csv」が見つからないか、Webサーバー(またはCLI)にそこに書き込む権限がありません。

于 2012-05-01T11:28:17.937 に答える
1

正確な設定はわかりませんが、ほとんどの場合、許可の問題です。ファイルを開いたり作成したりできず、それが「ブール値」の問題である場合、fopen は FALSE を返します。「path_to_file」フォルダに権利777を設定してから、スクリプトを再度実行してみてください

また、ファイルを操作する前に、fopen の戻り値を確認する必要があります。

$fp = fopen('path_to_file/file.csv', 'w');
if ($fp != false){
    foreach ($list as $fields) {
        fputcsv($fp, $fields);
    }

    fclose($fp);
}
于 2012-05-01T11:33:25.133 に答える