Reputation: 2641
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
Upvotes: 0
Views: 1656
Reputation: 63797
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
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.
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.
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));
Upvotes: 3
Reputation: 1846
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');
Upvotes: 1