2012-08-30 7 views
1

如何通過FTP保存圖像?這裏是我的代碼:設置imagepng路徑以通過FTP保存

$conn_id = ftp_connect('***'); 
$login_result = ftp_login($conn_id, '***','***'); 
if($login_result) echo 'connected'; 
$save = "FTP://temp/". $FileName; 

imagepng($this->Picture, $save); 

/*if (ftp_put($conn_id,$save,$save, FTP_ASCII)) 
    echo "successfully uploaded \n"; 
else 
    echo "There was a problem while uploading \n"; 
*/ 
+0

我相信你會首先通過FTP上傳之前寫入鏡像磁盤,所以最終尺寸是已知的。但我可能是錯的。 – Brad

+0

'ftp_ *'函數中'$ save'路徑不正確;使用內置函數,您只能上載可訪問磁盤上的文件或打開的文件處理程序(例如,從'fopen'返回的處理程序); – Passerby

+0

還有其他方法,如'ob_start()'和'ob_get_clean()'。 –

回答

3

你可以這樣做:

ob_start(); 
imagepng($this->Picture); 
$image = ob_get_clean(); 
$stream = stream_context_create(array('ftp' => array('overwrite' => true))); 
file_put_contents("ftp://user:[email protected]/folder/".$FileName, $image, 0, $stream); 
+0

謝謝,我有一個問題覆蓋圖像到FTP:我得到這個問題,在創建圖表:Warning:file_put_contents(... assessment_result.png)[function.file-put-contents]:無法打開流:遠程文件已經存在並覆蓋上下文選項未指定
FTP服務器報告213 6865在....... pChart \ pChart \ pChart.class.php在線3185 –

+0

是的,抱歉,'overwrite'未啓用。現在檢查.. –

0

希望這有助於你

<? 

//Configuration 
$ftp_server = "ftpaddress"; 
$ftp_user = "username"; 
$ftp_pass = "password"; 

// set up a connection or die 
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 

//trying to connect with login details 
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) { 
    echo "Connected! "; 
} else { 
    echo "Couldn't connect!"; 
} 

//You can change it with the file name, this is for if you're upload using form. 

    $myName = $_POST['name']; //This will copy the text into a variable 
    $myFile = $_FILES['file_name']; // This will make an array out of the file information that was stored. 
?> 

<?PHP 
     $destination_path = "dest/path/"; 

//your desired path for uploading) 

    $destination_file = $destination_path."img.jpg"; 

//This will create a full path with the file on the end for you to use, I like splitting the variables like this in case I need to use on on their own or if I'm dynamically creating new folders. 

     $file = $myFile['tmp_name']; 

//Converts the array into a new string containing the path name on the server where your file is. 

    $upload = ftp_put($conn_id, $destination_file, $file, FTP_BINARY);// upload the file 
    if (!$upload) {// check upload status 
     echo "FTP upload of $destination_file has failed!"; 
    } else { 
     echo "Uploaded $file to $conn_id as $destination_file"; 
    } 

?> 
+0

謝謝,但我不能設置該功能將ob_get_clean()保存到ftp。你能開發這種方法嗎?file_put_contents('ftp:// user:[email protected]/folder/'.$FileName,$ image); –