larka06 Posted September 11, 2013 Share Posted September 11, 2013 I have been working on opening, writing to, and closing a text file. I have direct address to the file in my php. I have the directory and the text file world readable, writable, and excutionable, in short 777 in the whole path. I have also assigned the file name to apache:apache (user, group). I have tried my own machine name, no luck. Here is the code // open file for appending @ $fp = fopen("/home/orders/orders.txt", 'ab'); flock($fp, LOCK_EX); // LOCK THE FILE FOR WRITING if (!$fp) { echo "<p><strong> Your order could not be processed at this time. Please try again later.</strong></p></body></html>"; exit; } fwrite($fp, $outputstring, strlen($outputstring)); flock($fp, LOCK_UN); // RELEASE WRITE LOCK fclose($fp); echo "<p>Order written.</p>"; My if is a boolean if true I write to the file. False I get not processed at this time. can anyone see something wrong? Thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/282061-can-not-open-a-text-file/ Share on other sites More sharing options...
jcbones Posted September 11, 2013 Share Posted September 11, 2013 Is apache the group that the server runs under? I know that most of the time apache server runs under the user www-data. But, being that it is 777, this shouldn't be the problem. Are you sure the file path is correct: /home/orders NOT: /home/user/orders OR, you could just remove the error suppression, and see if fopen() is throwing a warning. Quote Link to comment https://forums.phpfreaks.com/topic/282061-can-not-open-a-text-file/#findComment-1449074 Share on other sites More sharing options...
larka06 Posted September 11, 2013 Author Share Posted September 11, 2013 The path is correct. I had it the second way but in desperation thought if I put it closer to the tree stem it might help. I just took the error suppression and I got nothing from ifopen(). I am running LAMP on CentOS 6.4 I opened up home directories on selinux There is just some small thing I am not seeing. Quote Link to comment https://forums.phpfreaks.com/topic/282061-can-not-open-a-text-file/#findComment-1449076 Share on other sites More sharing options...
gizmola Posted September 11, 2013 Share Posted September 11, 2013 I don't see anything egregiously wrong, however, you should put your code inside the flock function, and also fflush before unlocking. // open file for appending $fp = fopen("/home/orders/orders.txt", 'ab'); // LOCK THE FILE FOR WRITING if (flock($fp, LOCK_EX)) { fwrite($fp, $outputstring, strlen($outputstring)); fflush($fp); flock($fp, LOCK_UN); // RELEASE WRITE LOCK echo "<p>Order written.</p>"; } else { echo "<p><strong> Your order could not be processed at this time. Please try again later.</strong></p></body></html>"; } fclose($fp); Quote Link to comment https://forums.phpfreaks.com/topic/282061-can-not-open-a-text-file/#findComment-1449079 Share on other sites More sharing options...
vinny42 Posted September 11, 2013 Share Posted September 11, 2013 in short 777 in the whole path Leaving out the @ will probably help a bit. If you want to surpress the errormessages you should do so through the config of PHP, not the code because like now; you can't see what the error is. Did you 777 the whole path, including "/home"? That would be very bad, don't do that. if a filepath doesnt work, change it to something generic like "/tmp" which will always work, and see if that makes the error go away. if so: it's your path, if not: it has nothing to do with path or permissions and your error is some where else. Debug the think, put print statements everywhere and see where the code does en does not get to, and what the state of the vars is when the script stops. Quote Link to comment https://forums.phpfreaks.com/topic/282061-can-not-open-a-text-file/#findComment-1449092 Share on other sites More sharing options...
larka06 Posted September 11, 2013 Author Share Posted September 11, 2013 I did not set home to 777. I did open home up for selinux. I have the program at a building stage so I know where I lose everything. All works well except for the opening of the file. It is for a small program that will take in orders add them up display all info and write to this flat file. Later, when I learn more, I will incorportate MySQL instead of this flat file. At present this will work for the fjist stages, that is if I can get it to open. Thanks for your time. Quote Link to comment https://forums.phpfreaks.com/topic/282061-can-not-open-a-text-file/#findComment-1449143 Share on other sites More sharing options...
vinny42 Posted September 11, 2013 Share Posted September 11, 2013 if home is not set to 777 then it is very likely that apache cannot access it's subdirectories. Apache should never be accessing anything in the home directory really, only in the documentroot of your website, that is owned by apache and that is where you should keep all the files for your website. I'd also urge you to move to a database now, rather than later. Working with a flatfile can actually be more difficult to do (or "to do right") than a database. Quote Link to comment https://forums.phpfreaks.com/topic/282061-can-not-open-a-text-file/#findComment-1449144 Share on other sites More sharing options...
larka06 Posted September 12, 2013 Author Share Posted September 12, 2013 I tried the script from gizmola and still get the same results, no open of file. vinny42 /home/******/pubic_html is my root directory. Apache owns it as well as the txt file I am trying to open. So are you saying I should move the file to /home/*****/public_html? This is where my webfiles are. Quote Link to comment https://forums.phpfreaks.com/topic/282061-can-not-open-a-text-file/#findComment-1449176 Share on other sites More sharing options...
Solution larka06 Posted September 12, 2013 Author Solution Share Posted September 12, 2013 I just switch to a ubuntu server and it works so, it is not the code it is the operating system. Many Thanks to all of you Quote Link to comment https://forums.phpfreaks.com/topic/282061-can-not-open-a-text-file/#findComment-1449183 Share on other sites More sharing options...
vinny42 Posted September 12, 2013 Share Posted September 12, 2013 it is not the code it is the operating system. Sigh. Sure, whatever... Quote Link to comment https://forums.phpfreaks.com/topic/282061-can-not-open-a-text-file/#findComment-1449195 Share on other sites More sharing options...
jcbones Posted September 13, 2013 Share Posted September 13, 2013 When you create a public_html folder in your home/user folder, make sure you symlink it back to /var/www Quote Link to comment https://forums.phpfreaks.com/topic/282061-can-not-open-a-text-file/#findComment-1449313 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.