SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    fopen and check if file exits/has data?

    Hi all,

    Right now i have a piece of code that uses fopen. Like

    Code:
    $handle = fopen($filename, "r");
    This code resists on top off my page btw.

    Now if the $filename has data all runs well. But when it's empty the rest of my code isn't working.

    So what is the most simple solution?

    I was thinking in the line of
    Code:
    if ($handle) {
    .....
    Grtz

  2. #2
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    $test fgets($handle);
    if ( 
    $test ) {
        
    rewind($handle); 
    That should do it

  3. #3
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm,

    in plain english??

    This means fgets check if $handle has data.

    So if $handle has data $test is true/1

    rewind set the "pointer" to the beginning of the file.


    Should i put my all the code (when the file has data)
    between
    if ($test) {
    rewind ($handle);
    here
    } else {
    here code if file is empty
    }

    I am asking this because i have a table code with somewhere
    Code:
     while (!feof($handle) ) { 
     $contents = fgets($handle);
     $data = explode(';', $contents);
     
    
     if ($contents <>'') {
    and i don't think this is correct

    Code:
    if ($test) { 
    rewind($handle);
     
    while (!feof($handle) ) { 
     $contents = fgets($handle);
     $data = explode(';', $contents);
     
    
     if ($contents <>'') {

  4. #4
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about simply using filesize?
    PHP Code:
    if (filesize($filename)) {
        foreach (
    file($filename) as $line) {
            
    $data explode(';'$line);
            
    // .. do something
        
    }


  5. #5
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry my code works fine.

    It is just that the file i read can have no data, but it will always exicit

  6. #6
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but it will always exicit
    Sorry, I didn't understand that.

    Anyway, as I said, to see if a file contains data try the filesize function.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •