fwrite() error.

Discussion in 'PHP' started by blueparukia, Sep 15, 2007.

  1. #1
    PHP Warning: fwrite(): supplied argument is not a valid stream resource in /home/bezdredg/public_html/php/output.php on line 6

    My files are CHMODded to 777, and the file is created just fine. This is my first PHP script, so I have tried to troubleshoot, but to no avial.

    The basic structure of my script, is you input data into a form and when you submit it, it creates a new .php file and writes that data to it.

    All the form stuff is working, I just need help with this one error.

    My code is
    <?php
    $result = $_POST['code'];
    $user = $_POST['username'];
    $filename = "".$user.".php ";
    $filehandle = fopen($filename, 'w') or die("File Load Failed");
    fwrite ($filename, $result);
    ?>
    PHP:
    Thanks,

    BP
     
    blueparukia, Sep 15, 2007 IP
  2. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #2
    fwrite($filehandle, $result) not fwrite($filename, $result)
     
    hamidof, Sep 15, 2007 IP
    blueparukia likes this.
  3. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #3
    Awesome thanks. And how would I make it redirect to the created file?

    I tried: Header ("Location: "$filehandle""); as well as $filename, but I know that is just not right (especially since it doesnt work)

    Anyway, green rep is given :)
     
    blueparukia, Sep 15, 2007 IP
  4. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks!

    You will have to figure out the exact address of that file, maybe something like this:
    $the_url = 'http://www.yoursite.com/' .$filename;
    header('Location: ' .$the_url);
     
    hamidof, Sep 15, 2007 IP
  5. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #5
    I am using
    <?php
    /* Redirect to a different page in the current directory that was requested */
    $user = $_POST['username'];
    $filename = "".$user.".php ";
    $host  = $_SERVER['HTTP_HOST'];
    $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extra = $filename;
    $result = $_POST['code'];
    $filehandle = fopen($filename, 'w') or die("We have failed to open your file");
    fwrite ($filehandle, $result);
    chmod("$filename", 0777);
    header("Location: http://$host$uri/$extra")
    ?>
    Code (markup):
    The only problem is it says "Can not find filename.php" on server...error 404.

    And I have triple check, it is there, and the chmod worked fine.

    However, in Cpanel, they only show up as Text/x-generic.

    So what is the problem here?

    Thanks,

    BP
     
    blueparukia, Sep 15, 2007 IP
  6. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You have too many quotes in your header statement, and $filehandle won't work.

    
    header('Location: ' . $filename);
    
    Code (markup):
    Will work.

    BUT...

    If the code posted doesn't contain the opening and closing php tags <?php and ?> it won't run.

    Also, if there are syntax errors in the posted code, it won't run.

    Lastly, you realize that by allowing people to post arbitrary php code which you then execute, you are opening up your server to hackers. I hope you aren't doing this on a live page anywhere :eek:
     
    sea otter, Sep 15, 2007 IP
  7. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Try:
    echo "http://$host$uri/$extra";

    And see if it's the right URL.
     
    hamidof, Sep 15, 2007 IP
  8. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #8
    I am aware of the dangers involved, I just wanna get everything working before I secure it.

    And yes hanidorf, it is the correct URL. I even type it manually, and it still doesn't show up. Is there a possibility it is a hidden file or something?

    BP
     
    blueparukia, Sep 16, 2007 IP
  9. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #9
    What you are trying to do can be done with a database, that is you save what ever user enters (filter it first and escape it) in the database and a file that shows it to users based on the ID (or username here).

    Like here on DP if you look at the URL it's showthread.php?t=478277 and this forum software doesn't make an actual file for each thread. If it did, then this forum would end up with 500,000+ files and the owner couldn't change the theme or it wouldn't be searchable easily.

    This is the proper way to make an application because you can end up with 1,000s of files and you won't be able to change anything in them effectively.

    Also if you want the name of the user and the .php in the URL, that can be done with Apache's mod_rewrite.

    I hope this helps.
     
    hamidof, Sep 16, 2007 IP
  10. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #10
    Well, this is gonna sound odd.

    I want to make a new file everytime I submit the form. Each file will have different styles and whatever. This sounds very odd I know.

    BP
     
    blueparukia, Sep 16, 2007 IP