1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

php [function.fopen]: failed to open stream: Permission denied in

Discussion in 'PHP' started by DeFacto, May 4, 2008.

  1. #1
    Hi,

    I've tried to write data in txt file
    form.php
    
    <table cellpadding="0" cellspacing="0" style="margin-left:100px; margin-top:100px">
    <tr>
    <td>
    <form action="processscript.php" method="post">
    <textarea rows="20" cols="40" name="content" style="font-family:verdana">
    <?
    $fn = "b.txt";
    print htmlspecialchars(implode("",file($fn)));
    ?>
    </textarea>
    <input name="fname" type="hidden" value="<?php echo $fn; ?>"><br>
    <input type="submit" value="Keisti" style="font-family:verdana; margin-top:10px">
    </form>
    </td>
    
    PHP:
    processscript.php
    
    <?
    $fn = $_POST['fname'];
    $content = stripslashes($_POST['content']);
    $fp = fopen($fn,"w") or die ("Error opening file in write mode!");
    fputs($fp,$content);
    fclose($fp) or die ("Error closing file!");
    echo "<meta http-equiv=\"refresh\" content=\"0; url=form.php\" />\n";
    ?>
    
    PHP:
    everything ran smooth on my pc, but i got error after upload of file onto server.
     
    DeFacto, May 4, 2008 IP
  2. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Did you check if the file permissions allow you to write to that file? Or is that file behind some authentication system like .htaccess password protection?
     
    rohan_shenoy, May 4, 2008 IP
  3. DeFacto

    DeFacto Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    file permissions allows to write, because i could do it in my pc.
    code of .htaccess
    
    SetEnvIfNoCase Referer "^http://(www\.)?my_page_name\.(puslapiai|projektas)\.lt" locally_linked=1
    
    <FilesMatch "\.(gif|png|jpg|jpeg)$">
       Order allow,deny
       Allow from env=locally_linked
    </FilesMatch> 
    
    PHP:
     
    DeFacto, May 4, 2008 IP
  4. Randombase

    Randombase Peon

    Messages:
    224
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    DeFacto: that's where you're wrong, there is a difference between a Windows (local) and a Linux (server). On Windows, all files are writable by default. On Linux, you have to specify this per file, by chmodding it correctly. To make a file writable, you should chmod it 777.

    .htaccess has absolutely no influence or whatsoever on your PHP script.
     
    Randombase, May 4, 2008 IP
  5. Altari

    Altari Peon

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    OK...So first make sure the PHP settings are allowing it. After that, you might want to "chown" that directory and set the permissions in the 700s, so your system can always write to it.
     
    Altari, May 4, 2008 IP
  6. DeFacto

    DeFacto Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Could you please show some example how to do it, because i am newbie in such a thing :eek:
     
    DeFacto, May 4, 2008 IP
  7. Randombase

    Randombase Peon

    Messages:
    224
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    In your FTP client, look for something called permissions, most of the time it is Right click -> File permissions/properties. If you are allowed to enter a number here, enter 0777. If you cannot enter a number, then check off all checkboxes (should be nine in total).

    Since the file you are writing to is dynamicly inputted, you should chmod the whole directory where you are writing to 0777.
     
    Randombase, May 4, 2008 IP
  8. DeFacto

    DeFacto Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    that is for folder
    
    drwxr-xr-x   
    
    PHP:
    that is for file
    
    -rw-r--r--
    
    PHP:
    using total commander.
     
    DeFacto, May 4, 2008 IP
  9. wendallb

    wendallb Active Member

    Messages:
    180
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    78
    #9
    If you have cpanel you can change permissions using the file manager in cpanel, needs to be 777 for both
     
    wendallb, May 4, 2008 IP
  10. DeFacto

    DeFacto Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks to everybody, problem solved :)
     
    DeFacto, May 4, 2008 IP