title: fopen() / fputcsv( ) : error
body: <p>Im looking for a way to export to a csv on the fly and came accross this. </p>
  
  <pre><code>    $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);
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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?</p>
  
  <p>Is it safe to assume that this is a file permission error?</p>
  
  <p>Also since im using the exact code from the php.net page im assuming the other errors are displaying because of the fopen() issue.</p>
  
  <p>EDIT.</p>
  
  <p>It seems like i might be better to post some more info.</p>
  
  <p>The classes involved.</p>
  
  <pre><code> class transData{
  var $connection;
  var $date;
  var $data;
  
  function __construct($date){
      $this-&gt;date = $date;
      return;
  }
  
  function getData(){ //this method just pulls the results of the query and returns them as an array
      global $connection;
      $date=$this-&gt;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-&gt;query("select fromfile,count(fromfile) from transdata where soldtime like '%".$date."%' group by fromfile");
      while($row=$mysqli_result-&gt;fetch_row()){
          $count[$row[0]]=$row[1];
      }
      $mysqli_result-&gt;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-&gt;query("select * from transdata where soldtime like '%".$date."%' and fromFile='".$fromFile."' order by soldtime desc");
      while($row=$mysqli_result-&gt;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-&gt;data = $temp;
      $mysqli_result-&gt;free();
      return;
  }
  
  
  
  
  function dayTotal($noDays){
      //$list=array();
      $html="&lt;table class=\"paxdata\"&gt;";
  
      $html.="&lt;tr&gt;&lt;th&gt;Day&lt;/th&gt;&lt;th&gt;Date&lt;/th&gt;&lt;th&gt;No. Sold&lt;/th&gt;&lt;th&gt;Total Price(£)&lt;/th&gt;&lt;th&gt;&lt;/th&gt;&lt;/tr&gt;";
      //$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-&gt;getData();
          $stuff = $getData-&gt;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-&gt;is_odd($inc);
          if($odd==1){$html.="&lt;tr class=\"odd\"&gt;";}else{$html.="&lt;tr class=\"even\"&gt;";}
          $html.="&lt;td width=\"100px\"&gt;".$day."&lt;/td&gt;&lt;td width=\"100px\"&gt;".$formatDate."&lt;/td&gt;&lt;td&gt;".$x."&lt;/td&gt;&lt;td&gt;".fixedToFloat($price)."&lt;/td&gt;&lt;td&gt;&lt;a href=\"index.php?module=chooseDate&amp;year=$link[0]&amp;month=$link[1]&amp;day=$link[2]\"&gt;More info?&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;";
          //$list=array($day,$formatDate,$x,$price);
          $inc++;
      }
      $html.="&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;th&gt;Totals:&lt;/th&gt;&lt;th&gt;".$totalSold."&lt;/th&gt;&lt;th&gt;".fixedToFloat($totalPrice)."&lt;/th&gt;&lt;/tr&gt;";
      $html.="&lt;/table&gt;";
      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);
  
  }
  
    }
  </code></pre>
  
  <p>Called by this via the browser.</p>
  
  <pre><code>$date=date('Y/n/j');
  
  $getData = new transData($date);
  $all = $getData-&gt;getData();
  $getData-&gt;dayTotal(7);
  </code></pre>
  
  <p>The response in /var/log/system.log</p>
  
  <pre><code>   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
  </code></pre>