移动后重命名文件

时间:2013-06-06 18:42:45

标签: php file cmd move

我正在尝试使用.cmd文件和.php文件将文件移动到“旧”文件夹时重命名该文件。此文件通过电子邮件发送到批处理作业,然后移动到旧文件夹中,上一个月和当前年度在报告结束时。现在:文件正在正确发送电子邮件,并移动到旧文件夹,但文件名在进入“旧”文件夹时未被重命名。有人可以对我的代码提出任何建议吗?我似乎无法找出错误的位置。感谢

.CMD文件的代码:

REM Email the 2 monthly stats files regarding gtwinapps
REM The report is sent at 9:20 AM every first monday of every month
REM in C:\Smurf_Reports\gtwinapps\Monthly_Stats
"c:\Program Files\xampp\php\php.exe" d:\batch\monthly_smurfreport_gtwinapps.php
move C:\Smurf_Reports\gtwinapps\Monthly_Stats\gtwinapps_statsmonthly.csv C:\Smurf_Reports\gtwinapps\Monthly_Stats\old

我的.PHP文件代码:      

  /*This application pickups up *.csv files from Monthly_Stats folder and then sends     email to ops Support at ops.support@scotiabank.com.
  The command file associated with this php file moves the .csv file into the "OLD"   folder in the same directory

  */

  $dirpath = "C:/Smurf_Reports/gtwinapps/Monthly_Stats/";

  /* Renaming the batch job file to display the previous month and year once it is moved    into the "OLD" folder for clarity purposes */
  rename  ("C:\Smurf_Reports\gtwinsapps\Monthly_Stats\gtwinapps_statsmonthly.csv", "C:\Smurf_Reports\      gtwinapps\Monthly_Stats\old\monthly_gtwinapps_" . date("F_Y", strtotime("-1  month")) . ".csv");
  rename  ("C:\Smurf_Reports\gtwinsapps\Monthly_Stats\monthly.csv", "C:\Smurf_Reports\gtwinapps\Month      ly_Stats\old\gtwinapps_statsmonthly_" . date("F_Y", strtotime("-1 month")) . ".csv");

  if ($handle = opendir($dirpath  )) 
  {

$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";


$headers .= "\nMIME-Version: 1.0\n" .
        "Content-Type: multipart/mixed;\n" .
        " boundary=\"{$mime_boundary}\"";



$email_message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 8bit\n\n" .
$message_text . "\n\n";
$email_message .= "--{$mime_boundary}\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($entry = readdir($handle)))

{
    if (strpos($entry, '.csv',1))
    {
            $filepath = "";
        $filesize = 0;

        $filepath = $dirpath."".$entry; 
        $filesize = filesize ($filepath);          


        if (file_exists($filepath) && $filesize > 1)
        {
            $fileatt = $filepath;
            $fileatt_name = $entry;

            $fileatt_type = "application/octet-stream";

            $message_text .= "<P>Hi</P>";


            $file = fopen($fileatt,'rb');
            $data = fread($file,filesize($fileatt));
            fclose($file);

            $data = chunk_split(base64_encode($data));

                    $email_message .=  "Content-Type: {$fileatt_type};\n" .
                    " name=\"{$fileatt_name}\"\n" .
                    "Content-Disposition: attachment;\n" .
                    //" filename=\"{$fileatt_name}\"\n" .
                    "Content-Transfer-Encoding: base64\n\n" .
                    $data . "\n\n";
                    $email_message .= "--{$mime_boundary}\n";


        }
        $email_from = "ops.support@yahoo.com";
    //          $email_to = "ops.support@yahoo.com";
        $email_to   = "k.j@yahoo.com,g.h@yahoo.com";

        $lastMonth = date('F-Y',strtotime('last month'));
        $email_subject = "($lastMonth) Monthly Smurf Report for  GTWINAPPS.";

        $headers .= "From: ".$email_from."\r\n";


        echo $filepath."</br>";
    }

}

    closedir($handle);
    $ok = @mail($email_to, $email_subject, $email_message, $headers);
       }

1 个答案:

答案 0 :(得分:1)

rename函数中,您需要(A)转义反斜杠(\\),或者(B)使用正斜杠作为目录分隔符