如何在PHP中重命名FTP Move命令中的文件
在批处理作业电子邮件发送到“旧”文件夹后,我正在尝试移动文件 我已经成功地确定了这样做的代码。现在该文件名为如何在PHP中重命名FTP Move命令中的文件,php,ftp,command,rename,Php,Ftp,Command,Rename,在批处理作业电子邮件发送到“旧”文件夹后,我正在尝试移动文件 我已经成功地确定了这样做的代码。现在该文件名为monthly\u mssql,我正在尝试将该文件重命名为monthly\u mssql\u mmmyyyy 将月份和年份添加到文件末尾后,我可以跟踪确定哪个月的报告。如何编辑现有的命令文件 CMD文件的我的代码: REM The report is generated automatically and must be sent at 9:50 AM of the first Monda
monthly\u mssql
,我正在尝试将该文件重命名为monthly\u mssql\u mmmyyyy
将月份和年份添加到文件末尾后,我可以跟踪确定哪个月的报告。如何编辑现有的命令文件
CMD文件的我的代码:
REM The report is generated automatically and must be sent at 9:50 AM of the first Monday of every month, file is located
REM in C:\Reports\mssql\Monthly_Stats
"C:\Program Files\xampp\php\php.exe" "c:\htdocs\MultipleReport\monthly_smurfreport_email.php"
move C:\Smurf_Reports\mssql\Monthly_Stats\monthly_mssql.csv C:\Smurf_Reports\mssql\Monthly_Stats\old
我的PHP文件代码:
$dirpath = "C:/Reports/mssql/Monthly_Stats/";
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 = "k.j@yahoo.com";
$dirpath=“C:/Reports/mssql/Monthly_Stats/”;
如果($handle=opendir($dirpath))
{
$semi_rand=md5(time());
$mime_boundary=“==Multipart_boundary_x{$semi_rand}x”;
$headers.=“\n时间版本:1.0\n”。
“内容类型:多部分/混合;\n”。
“边界=\”{$mime\u boundary}\”;
$email\u message=“这是MIME格式的多部分邮件。\n\n”。
“{$mime\U边界}\n”。
“内容类型:text/html;字符集=\”iso-8859-1\“\n”。
“内容传输编码:8位\n\n”。
$message\u text.“\n\n”;
$email\u message.=“{$mime\u boundary}\n”;
/*这是在目录上循环的正确方法*/
while(false!==($entry=readdir($handle)))
{
if(STRPO($entry,.csv',1))
{
$filepath=“”;
$filesize=0;
$filepath=$dirpath.“.”$entry;
$filesize=filesize($filepath);
如果(文件存在($filepath)&&$filesize>1)
{
$fileatt=$filepath;
$fileatt_name=$entry;
$fileatt_type=“应用程序/八位字节流”;
$message_text.=“Hi
”;
$file=fopen($fileatt,'rb');
$data=fread($file,filesize($fileatt));
fclose($文件);
$data=chunk_split(base64_encode($data));
$email_message.=“内容类型:{$fileatt_Type};\n”。
“name=\”{$fileatt\u name}\“\n”。
“内容处置:附件;\n”。
//“filename=\”{$fileatt\u name}\“\n”。
“内容传输编码:base64\n\n”。
$data。“\n\n”;
$email\u message.=“{$mime\u boundary}\n”;
}
$email\u from=“k。j@yahoo.com";
//$email\u to=“k。j@yahoo.com";
$email\u to=“k。j@yahoo.com“
$lastmount=日期('F-Y',标准时间('lastmount');
$email_subject=“($lastmount)MS-SQL报告。”;
$headers.=“发件人:”.$email\u From.\r\n”;
echo$filepath。“”;
}
}
closedir($handle);
$ok=@mail($email\u to、$email\u subject、$email\u message、$headers);
}
/*如果(文件存在($filename)&&$size>228&&&$EmailAdd“”)
{
$fileatt=$filename;
$fileatt_name=$groupname.“.csv”;
如果($uemail“”)
$email_from=$uemail;
其他的
$email_from=$username.“@yahoo.com”;
$email\u to=$email\u from;
$email_subject=“优先消息”“。$p.”-“$groupname.”-“$displaydate;
$headers.=“发件人:”.$email\u From.\r\n”;
$headers.=“抄送:”.$EmailAdd.\r\n”;
$headers.=“密件抄送:k。j@yahoo.com";
$email_message.=“Hi
”;
$email_message.=“请查看随附的每月蓝精灵报告统计数据。”\n”;
$email_message.=“
谢谢,
;
设置错误处理程序(“myErrorHandler”);
$ok=@mail($email\u to、$email\u subject、$email\u message、$headers);
如果($ok)
{
取消链接($filename);
}
其他的
{
$file=“C:/Reports/Operation/Daily_Stats/smtperrlog.txt”;
$fh=fopen($file,'r+');
$contents=fread($fh,filesize($file));
fclose($fh);
$stderr=fopen('C:/Reports/Operation/Daily_Stats/smtperrlog.txt','w+');
fwrite($stderr,“”);
fclose($stderr);
$email\u fromerr=“k。j@yahoo.com";
$email\u toerr=“k。j@yahoo.com";
$email\u subjecterr=“每月统计”;
$email_messageerr=$contents;
$headerserr.=“发件人:”.$email\u fromerr.\r\n”;
@邮件($email\u toerr、$email\u subjecterr、$email\u messageerr、$headerserr);
}
}
*/
$message_text=“”;
$p=“”;
$EmailAdd=“”;
$headers=“”;
$counter=0;
在我看来,用批处理文件移动它是不必要的。我会用这个方法直接在PHP中移动它。您可以在文件末尾附加日期
这方面的一个例子是:
重命名(“C:\Smurf\u Reports\mssql\Monthly\u Stats\Monthly\u mssql.csv”,“C:\Smurf\u Reports\mssql\Monthly\u Stats\old\Monthly\u mssql.date”(“m\U Y”)。.csv”)代码>
编辑
$cMonth = intval(date("m")); //Retrieves current month, converts to int value
$lastMonth = ($cMonth == 1 ? "12" : $cMonth - 1); //If it's January, let's set the month to December
$cYear = ($cMonth == 1 ? intval(date("Y")) - 1 : date("Y")); //If it's January, let's also set the year back by one so the dates match up
rename("C:\Smurf_Reports\mssql\Monthly_Stats\monthly_mssql.csv", "C:\Smurf_Reports\mssql\Monthly_Stats\old\monthly_mssql_" . $lastMonth . "_" . $cYear . ".csv");
为什么不在PHP脚本中移动文件呢?这并不难。但是如果你真的想使用批处理,这里有一个关于如何生成mmddyyy字符串的很好的答案:--我在某处剪了一个片段,但是找不到。我所要做的就是将它添加到php文件中。谢谢Josh,这段代码工作得很好。我还有一个问题,这段代码显示的是当前月份。我如何让它显示在当前月份的前一个月?Josh,你认为你能帮我解决我遇到的类似错误吗@KJ看起来好像有人在掩护你。:)如果我已经解决了你的问题,你介意这样做吗?谢谢,我希望你一切顺利!
$cMonth = intval(date("m")); //Retrieves current month, converts to int value
$lastMonth = ($cMonth == 1 ? "12" : $cMonth - 1); //If it's January, let's set the month to December
$cYear = ($cMonth == 1 ? intval(date("Y")) - 1 : date("Y")); //If it's January, let's also set the year back by one so the dates match up
rename("C:\Smurf_Reports\mssql\Monthly_Stats\monthly_mssql.csv", "C:\Smurf_Reports\mssql\Monthly_Stats\old\monthly_mssql_" . $lastMonth . "_" . $cYear . ".csv");