0

I dont know where exactly I am going wrong here. I have narrowed the problem down to $file not being set to anything and I do not know where I have gone wrong. Please help :)

Code: ($_GET['unit'] is 1)

                $filepathhalf = "http://sureclean.netai.net/data/";
                $date = date("Ymd");
                $unitnum = $_GET['unit'];
                $ext = ".txt";
                $filepath = $filepathhalf.$unitnum.$date.$ext;
                $bool = file_exists($filepath);
                if($bool = true)
                {
                    $fh = fopen($filepath, 'r');
                    $file = fread($fh, 4);
                    fclose($fh);
                }
                else{};
                $file = strval($file);
                echo $file;

yourwebsite/data/120120714.txt:

true
1
  • 4
    $bool = true is probably not what you want, assignment (=) isn't the same thing as comparison (==). Commented Jul 14, 2012 at 9:01

2 Answers 2

3

file_exists

The function called file_exists will only work on local files, you cannot check wether a resource on the web (over HTTP) is available with this function.

This together with the fact that you are using assignment instead of comparison in the below snippet provides the wrong behavior of your script.

if($bool = true) 
  { ... }

fopen

fopen is not always allowed to read from external sources, but are you sure this is required? if your file is indeed local please don't try reading it over the net when you could access it in a more simple manner.


fread

The 2nd argument to fread specifies how many bytes to read as maximum, where you have currently specified 4.

Currently you are trying to read 4 bytes from the the filehandle stored in $fh, and you are indeed reading these four bytes.

$file will contain only spaces since that is what is being stored in the first four bytes of your file, if you have indented the contents of yourwebsite/data/120120714.txt in your question correctly - that is.


How would I read the whole file at once?

PHP provides a function called file_get_contents that will read the entire file and return it as a string, though if you are really looking for a way of reading your file using fread you should use something as the below:

$file_data = fread ($fh, filesize ($filename));
Sign up to request clarification or add additional context in comments.

1 Comment

Note that fread() can execute "short read" because of underlying implementation. Always check the return value and if the read was shorter than you expected, execute another fread() to read the missing bytes, too. This is no different from C level programming.
1

A miner mistake in if condition:

if($bool == true)
{
     // do the stuff
}
else{
     // also show some message here if file does not exists.
}

You can use some other functions to read the file. Like: file('filename.txt'); or file_get_contents('filename.txt');

Comments

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.