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
file_exists
The function called
file_existswill 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.
fopen
fopenis 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
freadspecifies how many bytes to read as maximum, where you have currently specified4.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.txtin your question correctly – that is.How would I read the whole file at once?
PHP provides a function called
file_get_contentsthat will read the entire file and return it as a string, though if you are really looking for a way of reading your file usingfreadyou should use something as the below: