php fopen,fwrite和fclose不会出错,但没有创建文件



我在两个ubuntu 16.04上运行php 7.0.22

以下代码在不投掷异常的情况下进行,$ 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,出现在目录/var/www/dropbox/具有完整写入权限的目录中。

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

没有与代码相关的错误,由

显示
cat /var/log/apache2/error.log

fopenfwritefclose不要抛出异常,它们返回错误

尝试

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";
}

您应该得到一些例外

最新更新