PHP 无法将电子邮件附件上传到 ftp 服务器
PHP unable to upload emails attachments to ftp server
我一直在尝试将我从 IMAP 电子邮件中获取的附件上传到 FTP 服务器,这是我的代码:
检索附件
<?php
/*
attachedFile() returns the attachment already decoded.
For this example the encode type was base64, so it used imap_base64 to decode
*/
function attachedFile($uid, $section, $encoding) {
# Returns file as an encoded string
$file = imap_fetchbody($this->conn, $uid, $section, FT_UID);
switch ($encoding) {
case 0:
case 1:
$file = imap_8bit($file); break;
case 2:
$file = imap_binary($file); break;
case 3:
$file = imap_base64($file); break;
case 4:
$file = quoted_printable_decode($file); break;
}
$mime = imap_fetchmime($this->conn, $uid, $section, FT_UID);
# returning decoding file, I use mime in another section
return array('file' => $file, 'mime' => $mime);
}
/*
Returns file as stream using attached_file() returns
*/
function streamedAttach( $filename, $uid, $section, $encoding ) {
# Returns file as stream using attached_file() returns
$attach = $this->attachedFile($uid, $section, $encoding);
$attachment = $attach['file'];
$stream = fopen($filename, 'w+');
if ( $stream === false ) return false;
$ok = fputs($stream, $attach['file']);
# raw printing
echo "Bytes: " . $ok . "<br />";
if ($ok===false) return false;
return $stream;
}
?>
streamedAttach 接收 attached_file 的 return 并写入 'handler' $stream.
然后它发送该流注意:我没有关闭流,因为当我关闭它时...ftp_fput 失败了。所以我把它打开了。
但是如果你看到这一行
echo "Bytes: " . $ok . "<br />";
正在写入字节:
BYTES written
这是要上传到FTP
function login($user, $pass) {
$ok = $this->conn!==false;
if ($ok) {
# Credentials
$this->user = $user;
$this->pass = $pass;
$login = @ftp_login($this->conn, $this->user, $this->pass);
$pasv = ftp_pasv($this->conn, true);
$ok = ($login && $pasv);
}
return $ok;
}
function uploadStreamedFile($stream, $destination) {
$ok = $this->conn!==false;
if ($ok) {
$ok = ftp_fput($this->conn, $destination, $stream, FTP_BINARY);
fclose($stream);
}
return $ok;
}
现在,upload_streamed_file 接收 $stream 并使用 ftp_fput(因为它是一个指针)在服务器上创建一个文件...糟糕的是文件大小为 0 KB。
files on server
这是我给他们打电话的地方
$ftp = new ftp_conn();
$ftp->basePath = "POS_ARCHIVE";
# This is the same name as in the email
$filename = "th.jpg";
# Sample data
$uid = 18;
$section = 2;
$encode = 3;
$path = "POS_ARCHIVE/4700327771/$filename";
echo $filename . "<br />";
if ($ftp->login(FTP_UID, FTP_PWD)) {
# $mailbox I have the instance before.
$streamed = $mailbox->streamedAttach($filename, $uid, $section, $encode);
$ok = $ftp->uploadStreamedFile($streamed, $path);
if ( $ok ) echo "There you go!";
else echo "Nope, nope.";
} else
echo "Nope, nope.";
你能帮帮我吗,我已经用了 2 个小时了...
当streamed_attach()
returns时,流的文件指针指向文件末尾。因此,当您尝试使用 ftp_fput()
上传它时,它会立即读取 EOF 并上传一个空文件。您需要返回到文件的开头,以便您可以读取您写入文件的内容。
function upload_streamed_file( $stream, $destination ) {
if ( $this->conn === false ) {
return false; //Later I'll add more details of why not.
} else {
//error( $destination );
rewind($stream);
$ok = ftp_fput( $this->conn, $destination, $stream, FTP_BINARY );
fclose( $stream );
return $ok;
}
}
另一种方法是关闭 streamed_attach()
中的流,然后使用 ftp_put
文件名而不是 ftp_fput
流。
我一直在尝试将我从 IMAP 电子邮件中获取的附件上传到 FTP 服务器,这是我的代码:
检索附件
<?php
/*
attachedFile() returns the attachment already decoded.
For this example the encode type was base64, so it used imap_base64 to decode
*/
function attachedFile($uid, $section, $encoding) {
# Returns file as an encoded string
$file = imap_fetchbody($this->conn, $uid, $section, FT_UID);
switch ($encoding) {
case 0:
case 1:
$file = imap_8bit($file); break;
case 2:
$file = imap_binary($file); break;
case 3:
$file = imap_base64($file); break;
case 4:
$file = quoted_printable_decode($file); break;
}
$mime = imap_fetchmime($this->conn, $uid, $section, FT_UID);
# returning decoding file, I use mime in another section
return array('file' => $file, 'mime' => $mime);
}
/*
Returns file as stream using attached_file() returns
*/
function streamedAttach( $filename, $uid, $section, $encoding ) {
# Returns file as stream using attached_file() returns
$attach = $this->attachedFile($uid, $section, $encoding);
$attachment = $attach['file'];
$stream = fopen($filename, 'w+');
if ( $stream === false ) return false;
$ok = fputs($stream, $attach['file']);
# raw printing
echo "Bytes: " . $ok . "<br />";
if ($ok===false) return false;
return $stream;
}
?>
streamedAttach 接收 attached_file 的 return 并写入 'handler' $stream.
然后它发送该流注意:我没有关闭流,因为当我关闭它时...ftp_fput 失败了。所以我把它打开了。
但是如果你看到这一行
echo "Bytes: " . $ok . "<br />";
正在写入字节:
BYTES written
这是要上传到FTP
function login($user, $pass) {
$ok = $this->conn!==false;
if ($ok) {
# Credentials
$this->user = $user;
$this->pass = $pass;
$login = @ftp_login($this->conn, $this->user, $this->pass);
$pasv = ftp_pasv($this->conn, true);
$ok = ($login && $pasv);
}
return $ok;
}
function uploadStreamedFile($stream, $destination) {
$ok = $this->conn!==false;
if ($ok) {
$ok = ftp_fput($this->conn, $destination, $stream, FTP_BINARY);
fclose($stream);
}
return $ok;
}
现在,upload_streamed_file 接收 $stream 并使用 ftp_fput(因为它是一个指针)在服务器上创建一个文件...糟糕的是文件大小为 0 KB。
files on server
这是我给他们打电话的地方
$ftp = new ftp_conn();
$ftp->basePath = "POS_ARCHIVE";
# This is the same name as in the email
$filename = "th.jpg";
# Sample data
$uid = 18;
$section = 2;
$encode = 3;
$path = "POS_ARCHIVE/4700327771/$filename";
echo $filename . "<br />";
if ($ftp->login(FTP_UID, FTP_PWD)) {
# $mailbox I have the instance before.
$streamed = $mailbox->streamedAttach($filename, $uid, $section, $encode);
$ok = $ftp->uploadStreamedFile($streamed, $path);
if ( $ok ) echo "There you go!";
else echo "Nope, nope.";
} else
echo "Nope, nope.";
你能帮帮我吗,我已经用了 2 个小时了...
当streamed_attach()
returns时,流的文件指针指向文件末尾。因此,当您尝试使用 ftp_fput()
上传它时,它会立即读取 EOF 并上传一个空文件。您需要返回到文件的开头,以便您可以读取您写入文件的内容。
function upload_streamed_file( $stream, $destination ) {
if ( $this->conn === false ) {
return false; //Later I'll add more details of why not.
} else {
//error( $destination );
rewind($stream);
$ok = ftp_fput( $this->conn, $destination, $stream, FTP_BINARY );
fclose( $stream );
return $ok;
}
}
另一种方法是关闭 streamed_attach()
中的流,然后使用 ftp_put
文件名而不是 ftp_fput
流。