I hate websites. But I wanted to make one. Basically, the goal is for the user to type into two text boxes and submit it. It'll get sent to a php script (I have this much working!). Once the php script gets it, it is to open up a file, go through every other line of the file, and if it finds the first input, replace the next line with the second input. If it doesn't find it, it should append it to the end. Now, I've got what I think should work, I think it may be a permissions thing at this point:
http://cemetech.pastebin.com/jq5CHe4j
How's that look?

Right now it outputs an empty line, and then the
$input
$output
Saved!
Part.
I see no particular problems in it. Does it fail? Is /this/files/stuff.blah world-writable?
It does fail. It doesn't output the contents of the file, and it doesn't save. I chmod 777'd the file and the directory it's in.

Code:
if (strcasecmp($input,  $replies[i]))  {
should be
Code:
if (!strcasecmp($input,  $replies[i]))  {
strcasecmp() returns 0 on match, just like strcmp() in C.
Yeah, there are a few other things, too, that I've since fixed. The biggest issue with loading seemed to be permissions though. It's now:
http://cemetech.pastebin.com/HRa8bGQK
But it's still not saving correctly.
merthsoft wrote:
Yeah, there are a few other things, too, that I've since fixed. The biggest issue with loading seemed to be permissions though. It's now:
http://cemetech.pastebin.com/HRa8bGQK
But it's still not saving correctly.
For safety's sake you should be flock()ing to LOCK_EX before the write and LOCK_UN afterwards, but other than that, hmm.... I don't see any problems.

Edit: yes I do. You want to open it "w", not w.
You may also wish to strip \n from the POSTed data, as that will end up in the file.

Sticking error_reporting(E_ALL); ini_set('display_errors', true); at the top of the file would have revealed the issue Kerm pointed out above.
benryves wrote:
You may also wish to strip \n from the POSTed data, as that will end up in the file.

Sticking error_reporting(E_ALL); ini_set('display_errors', true); at the top of the file would have revealed the issue Kerm pointed out above.
Indeed, but similarly remember to severely restrict error reporting for production code to avoid giving potential attackers too much information about the system with which they are working.
benryves wrote:
You may also wish to strip \n from the POSTed data, as that will end up in the file.

Sticking error_reporting(E_ALL); ini_set('display_errors', true); at the top of the file would have revealed the issue Kerm pointed out above.
Ah, ok. That's handy.
It's telling me:
Warning: fopen(replies.txt): failed to open stream: Permission denied... (It's permissions are 777 btw).
merthsoft wrote:
benryves wrote:
You may also wish to strip \n from the POSTed data, as that will end up in the file.

Sticking error_reporting(E_ALL); ini_set('display_errors', true); at the top of the file would have revealed the issue Kerm pointed out above.
Ah, ok. That's handy.
It's telling me:
Warning: fopen(replies.txt): failed to open stream: Permission denied... (It's permissions are 777 btw).
And you checked all the silly stuff, such as that it's in the directory where you expect it to be and all that? Case-sensitivity is of course important, but I'm sure you knew that too.
It's opening it to read it, so yes.
merthsoft wrote:
It's opening it to read it, so yes.
What if you try something like fopen/fread/fclose instead of file()? I know that's not as easy, though.
But reading isn't the issue...
merthsoft wrote:
But reading isn't the issue...
Yes, but my thought was perhaps file() was leaving a random file handle of some sort open, intentionally or accidentally.
I tried, instead, taking out the reading in entirely. Now it just attempts to open it for writing and it says:
Warning: fopen(replies.txt): failed to open stream: Permission denied
merthsoft wrote:
I tried, instead, taking out the reading in entirely. Now it just attempts to open it for writing and it says:
Warning: fopen(replies.txt): failed to open stream: Permission denied
You're running this on a Linux server, right? How about trying "w+" just for fun?
Yes.
Same thing.
merthsoft wrote:
Yes.
Same thing.
Darn, that's extremely mystifying. Same if you rename the existing file and try to make it create a new file?
Yeah, the same error, which is weird. When it tries to open it for reading, it says it's not there. For writing, it says I don't have permissions. This is boggling... The folder is 777! Do I need to chown it for someone or something?
merthsoft wrote:
Yeah, the same error, which is weird. When it tries to open it for reading, it says it's not there. For writing, it says I don't have permissions. This is boggling... The folder is 777! Do I need to chown it for someone or something?
You could try chowning it to the Apache server's user and group if you want (web_user? what is that again?).

Edit: Here's a sample from one of my servers:

Code:
-rw-r--r--  1 www-data root 1176 2010-11-16 15:35 services.dat
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement