note 115663 deleted from function.fopen by cmb
| From: | cmb@php.net | Date: | Sun, 09 Feb 2020 08:13:00 +0000 |
| Subject: | note 115663 deleted from function.fopen by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-214044@lists.php.net to get a copy of this message | ||
Note Submitter: Antoine
----
On a Windows webserver, when using fopen with a file path stored in a variable, PHP will return an
error if the variable isn't encoded in ASCII, which may be the case if the file file path is
retrieved from a database.
Possible workaround :
<?
$encoding = mb_detect_encoding($filePath);
$filePath = mb_convert_encoding($filePath, "ASCII", $encoding);
$filePath = str_replace("?", "", $filePath);
$filePath = addslashes($filePath);
if(file_exists($filePath)) {
echo "File Found.";
$handle = fopen($filePath, "r");
$fileContents = fread($handle, filesize($filePath));
fclose($handle);
if(!empty($fileContents)) {
echo "<pre>".$fileContents."</pre>";
}
}
else {
echo "File Not Found.";
}
?>