繁体   English   中英

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

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

我在两个ubuntu 16.04上的LAMP下运行PHP 7.0.22。

以下代码继续运行而不会引发异常,$ tempFile的值为Resource 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";
}

你应该得到一些例外

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM