dongpou1935 2012-04-03 16:01
浏览 33
已采纳

文件写入冲突:file_get_contents()&fputs()

Im at a bit of a loss, i have 2 scripts 1 which pulls email attachments from a mailbox and a second one which then parses the attachments and adds them to the DB.

This works ok most of the time, but is throwing up a few issues every now an again. Sometimes the email attachment is created, but not populated (blank file except for the name) and sometimes its just not created (downloaded) at all.

The first script opens a new file and writes to it, the second script then accesses the content of that file. Could these issues be because the file is still open when the second script is attempting to access it?

They run alternatively every 15 seconds.

1st script (its pretty big so i have attempted to just show the parts in question)

            for ($jk = 1; $jk <= imap_num_msg($mbox); $jk++) {
                echo "~~~~~~~~~~~~~~BEGIN!~~~~~~~~~~~~~~~~~~
";
                echo imap_num_msg($mbox);
                $structure = imap_fetchstructure($mbox,$jk);  echo "imap_fetchstructure()
";  
                $parts = $structure->parts; echo "structure->parts
";
                $fpos=2;


                for($i = 1; $i < count($parts); $i++) { echo "loop through parts of email
";
                    $message["pid"][$i] = ($i);
                    $part = $parts[$i];

                    if($part->disposition == "ATTACHMENT") { echo "if ATTACHMENT exists then grab data from it
";
                        $message["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
                        $message["subtype"][$i] = strtolower($part->subtype);
                        $ext=$part->subtype;
                        $params = $part->dparameters;
                        $filename=$part->dparameters[0]->value;

                        $num = $this->append();
                        $newFilename = $this->addToDB($filename,$num);
                        echo $newFilename."- Added tp DB
";

                        $mege="";
                        $data="";
                        $mege = imap_fetchbody($mbox,$jk,$fpos);  
                        $filename="$newFilename";
                        $fp=fopen($savedirpath.$filename,w); echo "Create file at specified location
";
                        $data=$this->getdecodevalue($mege,$part->type);
                        fputs($fp,$data); echo "Write data to the file
";
                        echo ">>>>>>>>>>>>> File ".$savedirpath.$newFilename." ~ now exists!
";
                        fclose($fp);
                        $fpos+=1;

                        imap_mail_move($mbox,'1:1','Processed');
                        echo "****************************************************
";
                        echo "* Matched - Download and move to Processed folder. *
";
                        echo "****************************************************
";
                        echo "


";

                    }

                }

            }

        }else{
            imap_mail_move($mbox,'1:1','Other');
            echo "***************************************************
";
            echo "******** No Match - Move to Other folder **********
";
            echo "***************************************************
";
        }

        imap_close($mbox);
    }

The 2nd script does a bunch of parsing by taking file names added to the db in the 1st script, then sticking them into the following.

        $addXML = "<xml>".file_get_contents($filename)."</xml>";

        $tickets = simplexml_load_string($addXML);
  • 写回答

1条回答 默认 最新

  • doudu22272099831 2012-04-04 09:48
    关注

    For anyone who might encounter something similar, i figured out why certain files where appearing blank.

    The blank files that where being created where coming from emails that had multiple email attachments. It worked fine with single attachments and the first attachment in multiple attachment emails.

    for($i = 1; $i < count($parts); $i++) { echo "loop through parts of email
    ";
    
    //some code
    
    if($part->disposition == "ATTACHMENT") { echo "if ATTACHMENT exists then grab data from it
    ";
    
    //bunch of code that gets the attachment using the section number
    
    imap_mail_move($mbox,'1:1','Processed');
    echo "****************************************************
    ";
    echo "* Matched - Download and move to Processed folder. *
    ";
    echo "****************************************************
    ";
    echo "
    
    
    ";
    }
    

    }

    Basically to get multiple attachments this part loops, but i had the imap_mail_move() function in the loop so the email was moved to a different folder before any other iteration could do its stuff for the other email attachments, hence the blank files

    D'oh!

    As for it skipping certain emails, i was having a play about with

    for ($jk = 1; $jk <= imap_num_msg($mbox); $jk++) { }
    

    It turned out that this was crapping out after about 4 iterations, causing some of the emails to be skipped. At this point im not sure why, however for my purposes i don't actually need this for loop so i have removed it.

    I know this was a daft mistake on my part regarding the imap_mail_move(), but i decided to post this in case it might help anyone else in future.

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

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示