Search code examples
phpfilefopen

fopen() not working


I'm want to read a simple string from a text file which is around 3-4 mb but fopen() fails ("can't open file" from die() is called). Here's the code:

clearstatcache();
$fh = fopen("/my/path/to/file.txt", "r") or die("can't open file");
$sql = fread($fh,filesize("/my/path/to/file.txt"));

Solution

  • Have you firstly checked to see if the file exists?

    if (!file_exists("/my/path/to/file.txt") { 
        die('File does not exist');
    }
    
    clearstatcache();
    
    $fh = fopen("/my/path/to/file.txt", "r") or die("can't open file");
    $sql = fread($fh,filesize("/my/path/to/file.txt"));