doupi4649 2014-12-08 03:26
浏览 65
已采纳

如何在foreach内部打开文件(glob ... PHP

I have a small number of image files and text files. They have the same file name with different extensions. I need to read the image files and for each image I need to read some (tool top) text from the corresponding text file. The problem is the file open inside the "foreach( glob("inserts/*.png")" loop fails. I have output the filename and the fopen works fine. I thought it may be you are unable to open two file concurrently in PHP but I could find nothing about it when I googled

foreach( glob("inserts/*.png" ) as $filename ) {
   $path="public/xml/iweb/".$filename;
   $insertpath=substr($filename, 0, -3)."txt";
   $myfile = fopen($insertpath, "r");
   $rec=fread($myfile,filesize($insertpath));
   fclose($myfile);
   $name=getInnerSubstring($rec,"-");
   $HTML5.="<img class='insertimage' src='".$path."' title='".$name."' onclick='insertcomponent(\"".$insertpath."\")'>";
}

One day I hope I know enough to answer questions instead of just asking them. :(

  • 写回答

1条回答 默认 最新

  • donglv6747 2014-12-08 03:33
    关注

    There's no reason why you shouldn't be able to open multiple files and no reason the foreach should break anything that works. My hunch is that the path you're trying to open is wrong. I suggest either debugging the code to see the variables or add an echo to see their content. You should also check if fopen() and fread() don't return some false because they failed. Good luck :)

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

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退