1

Having trouble figuring out how to write a basic text file via PHP. Been trying for a week now but as an example below is some of the basic code I've been trying. I'm logged in via SSH using root to beagle bone black running PHP 5.4 on Debian Wheesy.

When I point my browser to the page it shows both hello world type and doesn't display an error but when I go to try to find the file I cannot (find / -name data.txt). The test PHP file is located in var/www I'm sure it's something basic that I'm missing... any insight would be appreciated.

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World4</p>'; 

$fp = fopen('data.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);

// the content of 'data.txt' is now 123 and not 23!

echo '<p>Hello World Again</p>';

?> 
</body>
</html>
4
  • Have you tried file_put_contents? I prefer that over the f functions. I usually use absolute paths in that, that also could be your problem here. Commented Feb 10, 2015 at 1:44
  • Welcome to Stack Overflow! Please edit your question to clarify whether you're having trouble finding the file or understanding why its contents are the way they are. Thanks for improving the question's reference value! Commented Feb 10, 2015 at 1:50
  • chris85, yes I tried several file_put_contents example code as well. Commented Feb 10, 2015 at 1:57
  • Nathan Tuggy: not sure how to edit my original post. No text file is ever created regardless of what sample code i use. Commented Feb 10, 2015 at 2:00

1 Answer 1

4

You said you're logged into SSH using root? I'm going to guess that it might be a permission problem with the data.txt file, or the parent folder. Try creating the file in the same directory as your PHP file (content you posted in your post) via SSH like this:

touch data.txt
sudo chown www-data:www-data data.txt

That should make the file belong to the default Apache user (www-data) and the default apache group (www-data).

Sign up to request clarification or add additional context in comments.

4 Comments

That did it! Thanks! Is there any way I can avoid this issue in the future or should I just reset the permissions each time I need to make a new file?
Personally, I would make a folder that would have the correct permissions, and do all you editing of files in that folder. So if you make a files/ directory on your server with permissions 777, or owner www-data:www-data like we just did, then place all editable file therein, you won't have to worry about permissions.
Many thanks Jonathan, I did a chmod 777 subfolder and it's making files fine now in the subfolder without having to change permissions for each specific new file. Perfect!
Awesome! Glad I could help. You can mark the answer as resolved if you wish to make it easier for others to see that the question is answered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.