Jump to content

can not open a text file


Go to solution Solved by larka06,

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/282061-can-not-open-a-text-file/
Share on other sites

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.

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.

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);
 

 


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.

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.