SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast Sirus's Avatar
    Join Date
    Sep 2001
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Writting to files

    PHP Code:
    <? 
        
    if ($fp fopen("$dir/$name""w") === FALSE){
            echo 
    "An error occured";
        } else {
            echo 
    "'$name' added successfully";
            
    chmod ("$dir/$name",0777);
            
    fwrite($fp,$price);
            
    fwrite($fp,$description);
            
    fwrite($fp,$picture);
        }
    ?>
    This is my code, but it is not working. What am I doing wrong?

    I don't really know how to write to files, and I couldn't find very many sites about it. Is fwrite even the command to use? It creates the file, but then says:
    Warning: Supplied argument is not a valid File-Handle resource

    Thanks for the help,
    -Jeff

  2. #2
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code should work fine.. I tested it myself and it ran perfectly. The only problem I ran into were the permissions for your webserver to be able to write to that directory. Make sure it has it

  3. #3
    SitePoint Enthusiast Sirus's Avatar
    Join Date
    Sep 2001
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the quick reply, however it still doesn't work

    All the file permissions should be correct, here is the exact errors I get:

    'Test1' added successfully
    Warning: Supplied argument is not a valid File-Handle resource in /web/sites/242/rnest/www.rnest.f2s.com/main/NewItem.php on line 18

    Warning: Supplied argument is not a valid File-Handle resource in /web/sites/242/rnest/www.rnest.f2s.com/main/NewItem.php on line 19

    Warning: Supplied argument is not a valid File-Handle resource in /web/sites/242/rnest/www.rnest.f2s.com/main/NewItem.php on line 20


    So it creates the file, changes the permissions, but then it isn't writting.

    Any idea?

    -Jeff

  4. #4
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why don't you post the entire script so we can see what is in lines 18-20?
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  5. #5
    SitePoint Enthusiast Sirus's Avatar
    Join Date
    Sep 2001
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is the only PHP in the whole document, and those are lines 18,19, and 20:
    PHP Code:
    12 <? 
    13     
    if ($fp fopen("$dir/$name""w") === FALSE){
    14         echo "An error occured";
    15     } else {
    16         echo "'$name' added successfully";
    17         chmod ("$dir/$name",0777);
    18         fwrite($fp,$price);
    19         fwrite($fp,$description);
    20         fwrite($fp,$picture);
    21     }
    22 ?>

  6. #6
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, first off, where is $dir, $name, $price, $description, and $picture set?

    Secondly, why are you using === for your logically equal? I've never seen that.

    Thirdly, there was this note on php.net
    It may apply:

    Note: On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' included in fopen() mode parameter.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  7. #7
    SitePoint Enthusiast Sirus's Avatar
    Join Date
    Sep 2001
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is a form on another page and it submits the values to this page.

    I don't know why I am using ===. I changed it to == though.

    I changed the mode from "w" to "wb" and it still doesn't work

    cupid, are you sure that code will work? If so, I will try moving to a different provider.

    Does anyone else have any ideas?

  8. #8
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Defender1
    Secondly, why are you using === for your logically equal? I've never seen that.
    that's fine. check the PHP manual for info on it.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  9. #9
    SitePoint Enthusiast Sirus's Avatar
    Join Date
    Sep 2001
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got it working by going like this:

    PHP Code:
    <?
       
    if ($fp fopen("$dir/$name""w")){
          echo 
    "'$name' added successfully";
          
    chmod ("$dir/$name"0777);
          
    fputs ($fp,$price);
          
    fputs ($fp,"/n");
          
    fputs ($fp,$description);
          
    fputs ($fp,"/n");
          
    fputs ($fp,$picture);
          
    fputs ($fp,"/n");
       } else { 
          echo 
    "An error occured";
         }
    ?>
    The only problem now is it writes /n into the file, but what I want it to do is put in a line feed (like pressing enter)

    How would I do this?

    -Jeff

  10. #10
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    are you trying this?

    PHP Code:
    <?php
      
    if ($fp fopen("$dir/$name""w")) {
          
    chmod ("$dir/$name"0777);
          
    $str "$price\n
                  
    $description\n
                  
    $picture\n";
          
    fwrite($fp,$str);
          echo 
    "'$name' added successfully";
          
    fclose($fp);
       } else      
          echo 
    "An error occured";
    ?>

  11. #11
    SitePoint Enthusiast Sirus's Avatar
    Join Date
    Sep 2001
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah Paul, thanks a ton.

    -Jeff

  12. #12
    SitePoint Enthusiast Sirus's Avatar
    Join Date
    Sep 2001
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I am not very good at PHP yet.

    In a different file, how would I reverse this proccess, to get the variables $picture, $description, and $price back out of the text file.

    I can't figure out how to read just a line.

    -Jeff

  13. #13
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you can use the file() function, it will read the file and store each line as an element of the array $output,

    PHP Code:
    $output file('/dir/yourfile'); 
    Here's the manual page for this function : http://www.php.net/manual/en/function.file.php


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
  •