PHP fgets/fread not getting contents of file

Hi, I have the code below on an ISP's server. The line numbers I just put here for reference. When the var. $info is printed it gives:

1 /home/capio/public_html/shop/orderno
9 05-5000
Here's the thing, the "5" line above is empty, it should show "5 05-5000". I can't figure out why when I read the file with fgets or fread I get nothing as if the file is empty. Line 18 (below) writes to the file and I have looked at the file and it certainly contains the data "05-5000". And, the file can't get that data without line 18 below working right which is an fwrite.

I opened up all the permissions on the "orderno" file to 777 but still no change.

I'm just starting with PHP so it could easily be something simple I just don't know.

Any help would be appreciated.

function getNextOrderNo() {
1      global $info;  //Just something that I can throw text in & view later
2      $fpath = substr($_SERVER["PATH_TRANSLATED"], 0, strrpos($_SERVER["PATH_TRANSLATED"], "/")); //Found on ExpertExhchange, works great. Have same result without this line at all.
3      $fn = $fpath.'/orderno'; //Gets the full path to the file with text in it.
4      $fp = fopen($fn, 'w+');
5      $info .= "1 ".$fn." <br>\n"; //TEST LINE: Let's print the filename and path
6      if (!$fp) return; //exit routine if we can't get to the file
7      $lastOrderNo = fgets($fp); //Get the Text in the file
8      $info .= "5 ".$lastOrderNo." <br>\n"; //TEST LINE: Let's see what we pulled out of the file.
9      if (strlen($lastOrderNo)!= 7) $lastOrderNo = date("m")."-4999";
10      $lastOrderNo=preg_split('/-/', $lastOrderNo);
11      if ($lastOrderNo[0]!=date("m")) {
12            $orderNo=date("m")."5000";
13      } else {
14            $lastOrderNo[1]++;
15            $orderNo=$lastOrderNo[0]."-".$lastOrderNo[1];
16      }
17      $info .= "9 $orderNo<br>\n";//TEST LINE: Let's see what we are putting into the file.
18      fwrite($fptr, $orderNo);
19      fclose($fptr);
20      return $orderNo;
Who is Participating?
RoonaanConnect With a Mentor Commented:
Try using $info = file_get_contents($fn);

RegProctorAuthor Commented:
Thanks, I tried that can got the same result.

I then thought that this could not be a PHP problem so I tried move the file and the path for the files to just /home/capio/public_html/orderno taking out the "shop" subdirectory--same result.

Something tells me that perhaps my ISP needs to change something in the PHP or server setup because how can a simple statement like file_get_contents(myfile) not work? Then again, the shop subdir. was create for a commercial shopping cart called X-Cart and runs all sorts of PHP without a problem. Perhaps there something like an "Include" that's sometimes required for the function to work right.

I also added error_reporting(E_ALL); just under the global line but no errors got reported.
What does the below line give?

echo '<pre>'.htmlspecialchars(file_get_contents($fn)).'</pre>';

It still might be a coding issue of course.

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

RegProctorAuthor Commented:
The file is exactly 7 character long and those 7 characters are: 05-5000 which is put into the file from the fwrite statement (I tested this and made sure the writing was working at least) at the bottom of the code. The line you asked me to to try gives a blank line.

On another note I should mention that the code put above should be a little different on lines 18 & 19. That is,
18     fwrite($fptr, $orderNo);
19     fclose($fptr);
should be:
18     fwrite($fp, $orderNo);
19     fclose($fp);
Sorry about that, I was just changing the var. name when I cut & pasted and hadn't rechecked the code.
When it gives a blank line, then either $fn doesn't point to the correct file, or file $fn is empty/not accessible.

RegProctorAuthor Commented:
Well, let's see, it would be impossible to point to the wrong file because the same file handle (line 4) is used to write the file and that works. The write (line 18) is done after the read (line 7). The permissions on the file are 777 for now, so, how can it be in accessible for reading only?  and if it was an error would be generated and i am not getting any read errors, just no data.

Using an editor with FTP capability I have written to and read from the same file without any problems.

I agree with your assessment but I am looking for a possible reason for the file being inaccessable, not just an obvious deduction as to what's wrong, I've done that already. A clue as to why so I can fix it is what I need.
RegProctorAuthor Commented:
I think this is harder than I orginally thought so I'm making an adjustment.
RegProctorAuthor Commented:
Breakthrough! Not solved but the first clue, this works: $info .= "0 ".file_get_contents($fn)." <br>\n"; if I put it BEFORE the fopen so I can get the code working but I sure would like to know why a simple fopen followed by an fread or fgets doesn't work.

Btw, although the fopen is like this $fp = fopen($fn, 'w+');, I have also tried it like: $fp = fopen($fn, 'r+');, with the same result.
You have to fclose the file before being able to read it, probably.

RegProctorAuthor Commented:
Thanks, but your comment doesn't make any sense in the context of the preceeding comment.

Obviously you have to fclose() the file to use file_get_contents() just from what transpired above or as I have, use  file_get_contents() before you call fopen() on the same file. This shows that fopen is locking the file for read & write as it probably should, but, you can't use fgets() or fread() without first using fopen(), at least according to all the documentation. The question is, why does fgets/fread not work in the code above when clearly the fopen() does work with the same file handle and file_get_contents() works with the same string that is passed to the fopen statement.
RegProctorAuthor Commented:
Moderator, you can close this. While no one has been able to determine why the fgets/fread doesn't work yet, using the get_file_contents() is a workaround I can use, and have. I don't know if not solving the problem but providing a workaround counts as a solution I should accept or not, please let me know what would be normal in this case. Also, is there a way i can close an issue (without clicking on accept) or is that strckly your job?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.