2015-11-13 157 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,真正的);成功登录后。那么只有这样才行。