Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Björn Norén
Björn Norén
9,569 Points

"permission denied" What to do?

Hi, I have created this page: http://studenter.miun.se/~bjno1501/dt057g/webbutveckling2/php/practise/project/project.php and I get this error when I try adding a name: "Warning: file_put_contents(./document.txt): failed to open stream:"

So I have 3 questions:

  1. How do I correct this error?
  2. When I refresh using localhost, the newly added disappears. How do I do to make it stay permanent?
  3. How do I add multiple users so the first name doesn't get overwritten?

Any help is very much appreciated!!

Here's the code:

<?php

$file = "./document.txt";

?>

<html>

<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">

Name: <input type="text" name="fname">

<input type="submit">

</form>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

// collect value of input field


$name = $_POST['fname'];


if (empty($name)) {


    echo "Name is empty";


} else {


    $ser = serialize($name); 


    file_put_contents($file, $ser);


    $document = file_get_contents($file);      


    $unser_document = unserialize($document);


    echo "<br /><br />" . $unser_document;  


}

}

?>

</body> </html>

3 Answers

Robert Walker
Robert Walker
17,146 Points

Basically the console for your server, allowing you to change the file permssion for the folder or file you require.

I strongly suggest you either ask your host to do it or take the following course here on Treehouse:

https://teamtreehouse.com/library/console-foundations

The video with the most relevance would be:

https://teamtreehouse.com/library/console-foundations/users-and-permissions/file-permissions

However, learning the console now will be a huge advantage moving on as it gives you all the tools you will need to work with other remote computers or VM.

Björn Norén
Björn Norén
9,569 Points

Hi again, now I got accessed the permission. Thank you very for helping me out, it means a lot :)

Robert Walker
Robert Walker
17,146 Points

No problem at all, glad you got it sorted out!

Robert Walker
Robert Walker
17,146 Points

The issue is related to your file " ./ " can not be used, you would need to use document.txt and the path you wanted to use.

By path I mean: http://example.com/path/to/your/folder

$file = 'document.txt';

// Write the contents to the file, 
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time

file_put_contents('/path/to/your/folder/' . $file, $ser, FILE_APPEND | LOCK_EX);

$document = file_get_contents('/path/to/your/folder/' . $file);      

$unser_document = unserialize($document);

This is how I normally do it in my projects not as clean but just how I like it.

if (!$handle = fopen(SRV . "log/pdo.txt", 'a')) {

    echo 'Unable to open text file!';

}else{

    fwrite($handle, "\n ERROR:" . $pdoError );
    fclose($handle);

}
Björn Norén
Björn Norén
9,569 Points

Hi, thanks for reaching out :)

I tried your tips so I changed the code to this:

<?php

//Serilizing

$ser = serialize($items);

$file = "document.txt";

file_put_contents("/home/bjno1501/public_html/dt057g/webbutveckling2/php/practise/PHPList/". $file, $ser, FILE_APPEND | LOCK_EX);

?>

.. But I still get the error: Warning: file_put_contents(/home/bjno1501/public_html/dt057g/webbutveckling2/php/practise/PHPList/document.txt): failed to open stream: Permission denied in /home/bjno1501/public_html/dt057g/webbutveckling2/php/practise/PHPList/test2.php on line 44

Do I need to change the permission manually?

Thanks!

Robert Walker
Robert Walker
17,146 Points

Ahh sorry I thought when I read this last time it was just failed to open stream and not a permission issue too.

Yes you would need to change the permission:

Try adjusting the folder permissions.

from a terminal, run chmod 777 (from the folder)

Björn Norén
Björn Norén
9,569 Points

Thanks! Could you explain a little more in detail? I have seen that answer while googling but I don't really get how execute that command! ^_^

Like this newbie question, what is a terminal?