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

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2

    Error after uplading to the host


    Hello : i wrote this simple function and it was working at on my pc . But after loading my php file to the host but it failed .
    This function rewrites the file due to it's directory .
    $fp should contain my file .

    PHP Code:
    function rewrite ($filepath) {
     @ 
    $fp fopen($filepath,'rb'); 
      if(!
    $fp) {
        echo 
    "Voting failed.";
      }
         while(!(
    feof($fp))){
           
    $k fgets($fp,10);
        }
      
    fclose($fp);
      
    $k ++;
     
     @ 
    $fp fopen($filepath,'w');
      if(!
    $fp) {
       echo 
    "voting failed";
      }
      
    fwrite($fp,"");
      
    fwrite($fp,$k);  
      
    fclose($fp);  
     } 
    This are mistakes:

    Warning: fgets() expects parameter 1 to be resource, boolean given in /home/vol11/byethost24.com/b24_12865034/mq4me.byethost24.com/htdocs/questproc.php on line 48

    Warning: feof() expects parameter 1 to be resource, boolean given in /home/vol11/byethost24.com/b24_12865034/mq4me.byethost24.com/htdocs/questproc.php on line 47
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,230
    Rep Power
    593
    What happens if you do this:
    PHP Code:
     function rewrite ($filepath) {
     
    $fp fopen($filepath,'rb'); 
      if(
    $fp===false) {
        echo 
    "Voting failed.";
      }
         while(!(
    feof($fp))){
           
    $k fgets($fp,10);
        }
      
    fclose($fp);
      
    $k ++;
     
     
    $fp fopen($filepath,'w');
      if(
    $fp===false) {
       echo 
    "voting failed";
      }
      
    fwrite($fp,"");
      
    fwrite($fp,$k);  
      
    fclose($fp);  
     } 
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2
    the same thing
    Warning: fgets() expects parameter 1 to be resource, boolean given in /home/vol11/byethost24.com/b24_12865034/mq4me.byethost24.com/htdocs/questproc.php on line 48

    Warning: feof() expects parameter 1 to be resource, boolean given in /home/vol11/byethost24.com/b24_12865034/mq4me.byethost24.com/htdocs/questproc.php on line 47
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,910
    Rep Power
    1045
    Hi,

    that's no wonder, because none of you stops in case of an error.

    Sorry, but this is just weird. Why do you use this crappy error surpressing operator? Why do you read the last filesize() % 10 characters of the file? Why do you read all intermediate characters just to throw them away? Why do you open the file twice?

    I think you're really, really on the wrong track with this. If you tell us what you're trying to do, I'm sure we can show you a sane way of doing it.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2
    Thank you very much . It works now . The failure was in the filepath. i rowte in like this :
    ask\f1.txt
    needed like this :
    ask/f1.txt
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,910
    Rep Power
    1045
    Originally Posted by FAQer
    It works now .
    I really hope that's not enough for you.

    Comments on this post

    • gw1500se agrees : I wonder what the OP thought the point of checking for a successful open was, if a failure was essentially ignored anyway?
    • FAQer agrees : i'm noob. i'm totaly agree with that. but what do u mean ?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,230
    Rep Power
    593
    Which line is 48?

    The error means something is happening to $fp that makes it no longer a valid resource if it was in the first place. This is not a complete surprise given your odd code. The function opens a file for read which may or may not have been successful but regardless continues to reads lines, one at time time into $k. After that loop you increment $k which I guess is some integer value. Then you again open a file for write which may or may not have succeeded but regardless, overwrite the same file with a blank line followed by an integer.

    I should have looked more closely before. Try this:
    PHP Code:
    function rewrite ($filepath) {
       
    $fp fopen($filepath,'rb'); 
       if(
    $fp===false) {
         echo 
    "Voting failed.";
       }
       else {
           while(!(
    feof($fp))){
           
    $k fgets($fp,10);
        }
         
    fclose($fp);
         
    $k ++;
     
         
    $fp fopen($filepath,'w');
         if(
    $fp===false) {
            echo 
    "voting failed";
         }
         else {
          
    fwrite($fp,"");
          
    fwrite($fp,$k);  
          
    fclose($fp);  
        } 
      }

    Last edited by gw1500se; April 16th, 2013 at 02:08 PM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2
    Originally Posted by Jacques1
    Why do you open the file twice?
    I tried to open it once, but that gives me an error. i don't really know why. In java, c(++/#) it enough to open once. but here i was made to open it twice to avoid errors.
    Originally Posted by Jacques1
    Why do you read the last filesize() % 10 characters of the file?
    You see, i thought it's first 10 characters, it's enough for me. I need to store the amount of votes for my question. i can post href to the """site""" that i made.
    Originally Posted by Jacques1
    Why do you use this crappy error surpressing operator?
    PHP is the most hardest language i used to learn so I'm trying to do everything due to the manual .There i have found this operator . They say :"it helps".

    I see you are good in this so if u can advice me some better manual i'll use it. Your answers already helped me , not once.So please do that again .
  16. #9
  17. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,910
    Rep Power
    1045
    First of all: It's great that you don't stop after "it works" and instead try to improve your code. That's the right attitude.



    Originally Posted by FAQer
    You see, i thought it's first 10 characters, it's enough for me.
    The first 10 characters would be a single fgets($fp, 10). But you read again and again until the end of the file and only keep the last result. If your file was, for example, 12 bytes long, you'd read the last 2 bytes.



    Originally Posted by FAQer
    I need to store the amount of votes for my question. i can post href to the """site""" that i made.
    You should generally avoid storing data in text files, except for very simple and unimportant data -- like a single number.

    In any other case, use an actual database. It's much more reliable, flexible and hassle-free. You don't even need a full-blown database system like PostgreSQL or MySQL. There are small embedded databases like SQLite.

    If you do use a file, avoid the low-level byte reading functions as much as possible. You can read a complete file with file_get_contents() and write a string to a file with file_put_contents().

    When dealing with files, you also need to take care of edit conflicts. This means you have to lock the file before editing it.

    In your example:

    PHP Code:
    $counter_file '/path/to/counter';
    $counter_fp fopen($counter_file'r+');
    if (
    $counter_fp)
    {
        
    // get exclusive lock
        
    flock($counter_fpLOCK_EX);
        
    // check if the file is empty
        
    if (filesize($counter_file))
        {
            
    $counter = (int) fread($counter_fpfilesize($counter_file));
            
    $counter++;
            
    // rewind file pointer and clear file
            
    rewind($counter_fp);
            
    ftruncate($counter_fp0);
        }
        else
            
    $counter 1;
        
    fwrite($counter_fp$counter);
        
    // release lock and close file
        
    flock($counter_fpLOCK_UN);
        
    fclose($counter_fp);

    As you can see, that's a lot of code for such a trivial task. So files are really not good for storing dynamic data.



    Originally Posted by FAQer
    PHP is the most hardest language i used to learn so I'm trying to do everything due to the manual .There i have found this operator . They say :"it helps".
    Nope, it's one of the worst features of PHP, because it covers up errors. Like in your example: Instead of getting a sensible error message like "file doesn't exist" with the exact line, you ran into a weird boolean error later in the code.



    Originally Posted by FAQer
    I see you are good in this so if u can advice me some better manual i'll use it.
    To be honest, I don't know any tutorial I would recommend wholeheartedly. I've looked for one myself, but all of them have their weaknesses.

    The quakenet tutorial isn't all that bad. When you know the basics, you can learn the rest through the documentation, forums, blog articles etc. You need to keep up to date, anyway. That's the most important part of learning to program.

    Comments on this post

    • FAQer agrees : Thank you for your attention and your advises, they are very helpful. for me.
    Last edited by Jacques1; April 16th, 2013 at 06:10 PM.

IMN logo majestic logo threadwatch logo seochat tools logo