2015-11-13 156 views
0

我試圖上傳圖像到服務器,但我不能有權限訪問文件夾,所以我已經使用FTP連接。通過Android應用程序,我編碼圖像並將其發送到服務器。在服務器端,我只是解碼它,並試圖上傳它。但我無法上傳它。你能幫我解決這個問題嗎?上傳文件到服務器使用FTP在PHP

<?php 
    // Get image string posted from Android App 
    $base=$_REQUEST['image']; 
    // Get file name posted from Android App 
    $filename = $_REQUEST['filename']; 
    // Decode Image 
    $binary=base64_decode($base); 
    header('Content-Type: bitmap; charset=utf-8'); 
    $ftp_server = ""; 
    $ftp_user_name = ""; 
    $ftp_user_pass = ""; 
    $destination_file = "/upload/images/".time().jpg"; 


    $conn_id = ftp_connect($ftp_server); 
    ftp_pasv($conn_id, true); 

    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

    if ((!$conn_id) || (!$login_result)) { 
     echo "FTP connection has failed!"; 
     echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
     exit; 
    } else { 
     echo "Connected to $ftp_server, for user $ftp_user_name,$conn_id"; 
    } 

    $upload = ftp_put($conn_id, $destination_file, $binary, FTP_BINARY); 

    if (!$upload) { 
    echo "FTP upload has failed! $upload"; 
    } else { 
    echo "Uploaded $source_file to $ftp_server as $destination_file"; 
    } 
    ftp_close($conn_id); 
    ?> 
+0

你有什麼錯誤嗎? – lukassteiner

+0

是的,我有以下問題,\ n
警告:ftp_put(PNG)[]:未能打開流:在PHP文件 – Prabha

回答

0

函數ftp_put的第三個參數需要一個文件名。您將圖像二進制文件作爲字符串傳遞。

更改TIS線:

$upload = ftp_put($conn_id, $destination_file, $binary, FTP_BINARY); 

要:

$fileName = uniqid().'.jpg'; 
$filePath = '/tmp/'.$uniqid; 
file_put_contents($filePath, $binary); 

$upload = ftp_put($conn_id, $destination_file, $filePath, FTP_BINARY); 
+0

嗨無效的說法,我所面臨的以下問題
警告:file_put_contents (/ tmp /)[function.file-put-contents]:無法打開流:無效的參數。 – Prabha

+0

目錄/ tmp是否存在於您的系統上? – lukassteiner

+0

對不起,我沒有服務器憑證。我只擁有FTP憑證,所以我不知道/ tmp是否存在,並且我已經使用$ filePath = getcwd()。$ uniqid;但它報告file_put_contents();權限被拒絕問題; (我沒有權限在文件夾上書寫,因此我使用的是FTP) – Prabha

0

請加ftp_pasv($ conn_id,真正的);成功登錄後。那麼只有這樣才行。