Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7862581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:11:23+00:00 2026-06-02T23:11:23+00:00

Im looking for a way to export to a csv on the fly and

  • 0

Im looking for a way to export to a csv on the fly and came accross this.

    $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);

Its just the basic code taken from the php.net page. Anyway i stuck it in to try it our before using it for my own needs. But i keep getting the following errors.

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

I’m unsure why the fopen() error is appearing as i want it to just create a new file on the fly anyway, which was what i thought it done anyway?

Is it safe to assume that this is a file permission error?

Also since im using the exact code from the php.net page im assuming the other errors are displaying because of the fopen() issue.

EDIT.

It seems like i might be better to post some more info.

The classes involved.

 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);

}

  }

Called by this via the browser.

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

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

The response in /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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:11:25+00:00Added an answer on June 2, 2026 at 11:11 pm

    From PHP.net docs for fopen:

    Return Values

    Returns a file pointer resource on success, or FALSE on error.

    Either your code can’t find "path_to_file/file.csv", or your web server (or CLI) doesn’t have permission to write there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a way to export a .jar file in Netbeans. I
I'm looking for a way to export a Word document as a PDF. I
So I'm looking for a way to export all of the .xls files in
I'm looking for the correct way to export my pictures sequence into a quicktime
Looking for the correct way and control to import and export of out of
Im looking for a way to create CSV from all class instances. What i
I am looking for some way to export a .cs file to an .html
I'm looking for a way to export saved usernames/passwords from Toad for Oracle (9.5.0.31).
I am looking for a convenient way to export every images found in an
I am looking a for way to export/save rt structure sets to dicom file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.