PHP fopen、fwrite 和 fclose 继续没有错误,但没有创建文件

PHP fopen, fwrite and fclose proceed without errors but no file created

我是运行PHP7.0.22,下LAMP,二ubuntu16.04.

下面的代码在没有抛出异常的情况下继续执行,$tempFile 的值资源 ID #4。

    try {
            // Open temp file for writing
            $tempFile = fopen("/var/www/dropbox/temp.lst", "w");

            echo "tempfile=" . $tempFile .  "<br>";

            // Write list of file names to file
            for ($x = 0; $x <= $inputFileCount; $x++) {
                    fwrite($tempFile, $fileNames);
            }

            // Close temp file
            fclose($tempFile);
    } catch ( Exception $e ) {
            // send error message if you can
            echo 'Caught exception: ',  $e->getMessage(), "\n";
    }

但是,temp.lst 目录中没有文件 temp.lst 出现在具有完全写入权限的目录中。

ls -ld /var/www/dropbox/
drwxrwsrwx 2 ubuntu www 4096 Mar 25 18:13 /var/www/dropbox/

没有显示与代码相关的错误
cat /var/log/apache2/error.log

fopenfwritefclose 不抛出异常,它们 return 错误

尝试

try {
        // Open temp file for writing
        $tempFile = fopen("/var/www/dropbox/temp.lst", "w");
        if (false === $tempFile) throw new \RuntimeException("Failed to open file");

        echo "tempfile=" . $tempFile .  "<br>";

        // Write list of file names to file
        for ($x = 0; $x <= $inputFileCount; $x++) {
                if(false === fwrite($tempFile, $fileNames)) throw new \RuntimeException("Failed to write to file"); 
        }

        // Close temp file
        if(false === fclose($tempFile)) throw new \RuntimeException("Failed to close file"); 
} catch ( Exception $e ) {
        // send error message if you can
        echo 'Caught exception: ',  $e->getMessage(), "\n";
}

你应该得到一些例外