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

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

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建