dpyln64620 2018-03-25 18:53
浏览 274
已采纳

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

I am running PHP 7.0.22, under LAMP, on two ubuntu 16.04.

The following code proceeds without throwing an exception and $tempFile has the value 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(), "
";
    }

However, no file, by the name of temp.lst, appears in the directory /var/www/dropbox/ which has full write permission.

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

No errors, related to the code, are shown by

cat /var/log/apache2/error.log
  • 写回答

1条回答 默认 最新

  • doukanmang3687 2018-03-25 19:07
    关注

    fopen, fwrite, fclose don't throw Exceptions, they return errors

    Try

    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(), "
    ";
    }
    

    and you should get some exceptions

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效