[英]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接收attach_file的返回並寫入'handler'$ stream。
然后它發送該流注意:我沒有關閉流,因為當我關閉它時... ftp_fput失敗了。 所以我把它打開了。
但是,如果你看到這一行
echo "Bytes: " . $ok . "<br />";
它是寫字節:
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。
$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()
返回時, streamed_attach()
的文件指針指向文件的末尾。 因此,當您嘗試使用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;
}
}
另一種方法是在ftp_put
streamed_attach()
關閉流,然后使用ftp_put
和文件名而不是ftp_fput
與流。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.