从php发送电子邮件附件并上传ftp


Send email attachment from php and upload ftp

在这里,我可以将收到的文件(文件没有已知名称,它是用当前日期和时间创建的)上传到FTP中。同样,我想把附件作为邮件发送。

我得到的错误是(来自下面代码中的最后一条语句):

文件打开错误。

在发送邮件时,我无法选择接收到php中的文件。有人能告诉我为什么以及怎么做吗?

$destDir = 'myweb.net/name/' .$dir;
 $workDir = 'tmpfiles';// define this as per local system
 // get temporary file name for the uploaded file
$tmpName = basename($_FILES['uploadedfile']['tmp_name']);
$fileName = basename($_FILES['uploadedfile']['name']);
// copy uploaded file into current directory
move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$workDir."/".$tmpName)
 or die("Cannot move uploaded file to working directory");
// open connection
$conn = ftp_connect($ftp_server) or die ("Cannot initiate connection to host");
// send access parameters
ftp_login($conn, 'abcd', 'saddad') or die("Cannot login");
// perform file upload
$upload = ftp_put($conn, $destDir."/".$_FILES['uploadedfile']['name'],$workDir."/".$tmpName, FTP_BINARY);
// check upload status
// display message
if (!$upload) {
 echo "Cannot upload<br />'n";
} else {

 $to = $Remail;
 $subject = "This is subject";
 $message = "This is test message.";
// Open a file
 $file = fopen($_FILES['uploadedfile']['name'], "r" );
 if( $file == false )
  {
 echo "Error in opening file";
 exit();
 }

您正试图打开原始客户端文件名,该文件名与服务器上的任何内容都没有任何关系。服务器上唯一存在的文件是['tmp_name']中列出的文件,现在已将其移动到$workdir

你的fopen应该是

$file = fopen($workDir . '/' . $tmpName, 'r');

此外,您在用户->Web服务器上传内容上绝对没有错误处理,并且只是假设上传永远不会失败。错误的假设。