#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Fwrite please help


    Hello everyone first time posting, I'm new to the coding world and i have a problem with fwrite. it won't write the csv data to the txt file.
    PHP Code:
    if (($handle fopen("stubs.csv""r")) != FALSE) {
        while ((
    $data fgetcsv($handle1000",")) != FALSE) {
            
    $num count($data);
            
    $g=0;
            
    $fn=fopen("stuby.txt""w");
            while (
    $g<=$num){
                
    fwrite($fn,$data[$g]);
                
    $g++;
            }
        }
    }
    fclose($fn);
    fclose($handle); 
    any suggestions would be great
    thank you
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,465
    Rep Power
    594
    Are you sure the fopen is working? You should be checking for error conditions:
    PHP Code:
    $fn=fopen("stuby.txt""w") ||  die("Failed to open output file: ".error_get_last()); 
    Does the file exist even though it is empty? Are error messages turned on?
    PHP Code:
    ini_set('display_errors',1); 
    error_reporting(E_ALL); 
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by gw1500se
    Are you sure the fopen is working? Does the file exist even though it is empty? Are error messages turned on?
    yes fopen is definitively working,yes it exist and it is empty, and yes error messages are turned on.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,465
    Rep Power
    594
    Did you make sure there is something to write?
    PHP Code:
    echo "<pre>";
    print_r($data);
    echo 
    "</pre>";
    while (
    $g<=$num){
            
    fwrite($fn,$data[$g]);
             
    $g++;

    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by gw1500se
    Did you make sure there is something to write?
    yes there is data it should be able to write. it echos this:

    <pre>Array
    (
    [0] => date
    [1] => 8
    [2] => d5
    )
    </pre><pre>Array
    (
    [0] => dot
    [1] => 2
    [2] => f4
    )
    </pre><pre>Array
    (
    [0] => dad
    [1] => 3
    [2] => h3
    )
    </pre><pre>Array
    (
    [0] => adas
    [1] =>
    [2] =>
    )
    </pre><pre>Array
    (
    [0] =>
    [1] =>
    [2] => g1
    )
    </pre>

    but it still won't write to the txt file.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,465
    Rep Power
    594
    Then you need to add more error checking. For debugging purposes try this.
    PHP Code:
    while ($g<=$num){
       echo 
    "$data[$g]<br />";
       if (
    fwrite($fn,$data[$g++])===false) {
          
    $err=error_get_last();
          echo 
    "Write failed: ".$err['message']."<br />";
       }

    The error message may or may not have anything useful but at least you will know what you are trying to write and whether or not it failed.

    P.S. I moved the increment operator to make your code a little cleaner.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    PHP Code:
     if (($handle fopen("stubs.csv""r")) != FALSE) {
        while ((
    $data fgetcsv($handle1000",")) != FALSE) {
            
    $num count($data);
            
    $g=0;
            
    $fn=fopen("stuby.txt""w"); // <- You re-open this same file on every iteration of the loop.  Opening it in write mode truncates the file.  I would expect only the final line to show up in the file.  The last line being empty or something like that might make nothing show up in this file.  The file should still be created though by this code, assuming your directory permissions are correct.
            
    while ($g<=$num){
                
    fwrite($fn,$data[$g]);
                
    $g++;
            }
        }
    }
    fclose($fn);
    fclose($handle); 

    Comments on this post

    • gw1500se agrees : Rats!!! I completely missed that.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo