尝试使用 mailReader.php 复制文件时丢失变量值


Losing variable value when trying to copy file using mailReader.php

我正在使用在这里找到的mailReader脚本:https://github.com/stuporglue/mailreader

我正在尝试在上传到另一个文件夹后将文件复制到另一个文件夹。文件将正确上传到脚本所在的文件夹。当我尝试运行复制命令时,文件名变量为空。

这是我正在使用的代码部分: 最后三行是我添加的内容。

private function saveFile($filename,$contents,$mimeType = 'unknown'){
    $filename = preg_replace('/[^a-zA-Z0-9_-]/','_',$filename);
    $unlocked_and_unique = FALSE;
    while(!$unlocked_and_unique){
        // Find unique
        $name = time() . "_" . $filename;
        $name = substr_replace($name,".pdf",-4); // added 1-19-2016
        while(file_exists($this->save_directory . $name)) {
            $name = time() . "_" . $filename;
            $name = substr_replace($name,".pdf",-4);
        }
        // Attempt to lock
        $outfile = fopen($this->save_directory.$name,'w');
        if(flock($outfile,LOCK_EX)){
            $unlocked_and_unique = TRUE;
        }else{
            flock($outfile,LOCK_UN);
            fclose($outfile);
        }
    }
    fwrite($outfile,$contents);
    fclose($outfile);
    if (copy($this->save_directory.$name, "/attachments/" . TRANS_ID . "/". $name)) {
        unlink( $this->save_directory.$name );
    }

我通过电子邮件收到文件已上传的确认,然后收到另一封包含错误消息的电子邮件。

警告:copy(/attachments/W7652222-546/1453406138_residential-print_from_td.pdf):无法打开流:第 224 行的/home/myhost/public_html/mailreader/mailReader.php 中没有这样的文件或目录

224是我添加的代码的行号。

/附件前面缺少源文件名...

有人有什么想法吗?

$name是在

while 循环中定义的,可能无法在上层范围内访问 我的建议是将您的代码更改为:

private function saveFile($filename,$contents,$mimeType = 'unknown'){
    $filename = preg_replace('/[^a-zA-Z0-9_-]/','_',$filename);
    $unlocked_and_unique = FALSE;
    $name = '';
    while(!$unlocked_and_unique){
        // Find unique
        $name = time() . "_" . $filename;
        $name = substr_replace($name,".pdf",-4); // added 1-19-2016
        while(file_exists($this->save_directory . $name)) {
            $name = time() . "_" . $filename;
            $name = substr_replace($name,".pdf",-4);
        }
        // Attempt to lock
        $outfile = fopen($this->save_directory.$name,'w');
        if(flock($outfile,LOCK_EX)){
            $unlocked_and_unique = TRUE;
        }else{
            flock($outfile,LOCK_UN);
            fclose($outfile);
        }
    }
    fwrite($outfile,$contents);
    fclose($outfile);
    if (copy($this->save_directory.$name, "/attachments/" . TRANS_ID . "/". $name)) {
        unlink( $this->save_directory.$name );
    }

我希望这可以解决您的问题

我最终在私有函数 saveToDB 中定义了一个常量 email_id,然后在其他所有操作完成后运行脚本以使用 email_id 查询表并循环浏览移动文件的记录。