【问题标题】:upload the file into the server using ftp in php在php中使用ftp将文件上传到服务器
【发布时间】:2015-11-13 10:28:40
【问题描述】:

我尝试将图像上传到服务器,但我无法访问该文件夹,因此我使用了 FTP 连接。通过 android 应用程序,我对图像进行了编码并将其发送到服务器。在服务器端,我只是对其进行解码并尝试上传。但我无法上传它。你能帮我解决这个问题吗?

 <?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);
    ?>

【问题讨论】:

标签: php android ftp server


【解决方案1】:

函数ftp_put 的第三个参数需要一个文件名。您将图像二进制文件作为字符串传递。

换行:

$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);

【讨论】:

  • 您好,我遇到了以下问题
    警告:file_put_contents(/tmp/)[ function.file-put-contents]:打开流失败:参数无效。
  • 您的系统上是否存在目录 /tmp?
  • 对不起,我没有服务器凭据。我只有 FTP 凭据,所以我不知道 /tmp 是否存在,我尝试使用 $filePath =getcwd( ).$uniqid;但它报告,file_put_contents(); 上的权限被拒绝问题。 (我没有在文件夹上写的权限,所以我使用的是 FTP)
  • 你应该先解决这个权限问题。然后你也可以绕过那个丑陋的解决方案,通过 FTP 再次上传文件。
【解决方案2】:

请添加 ftp_pasv($conn_id, true);登录成功后。那么只有这样才能工作。

【讨论】:

    猜你喜欢
    • 2016-03-31
    • 1970-01-01
    • 2011-03-14
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    • 1970-01-01
    • 2014-09-21
    相关资源
    最近更新 更多