Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中使用ftp将文件上载到服务器_Php_Android_Ftp_Server - Fatal编程技术网

在php中使用ftp将文件上载到服务器

在php中使用ftp将文件上载到服务器,php,android,ftp,server,Php,Android,Ftp,Server,我已尝试将图像上载到服务器,但我没有访问该文件夹的权限,因此使用了FTP连接。通过android应用程序,我对图像进行了编码并将其发送到服务器。在服务器端,我只是解码并试图上传它。但我无法上传它。你能帮我一下吗 <?php // Get image string posted from Android App $base=$_REQUEST['image']; // Get file name posted from Android App $fi

我已尝试将图像上载到服务器,但我没有访问该文件夹的权限,因此使用了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);
    ?>

函数的第三个参数需要一个文件名。您正在以字符串形式传递图像二进制文件

更改tis行:

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


请添加ftp\u pasv($conn\u id,true);成功登录后。那么只有这样才能工作。

您有任何错误吗?是的,我遇到了以下问题,\n
警告:ftp\u put(PNG)[您好,我遇到了以下问题
警告:file\u put\u contents(/tmp/)[]:无法打开流:无效参数。您的系统上是否存在目录/tmp?抱歉,我没有服务器凭据。我只有FTP凭据,因此我不知道/tmp是否存在,并且我已尝试使用$filePath=getcwd().$uniqid;但它报告了文件_put_contents()上的权限拒绝问题;。(我没有权限在文件夹上写入,因此我使用FTP)您可能应该首先解决权限问题。然后您还可以绕过这个丑陋的解决方案,通过FTP再次上载文件。
$fileName = uniqid().'.jpg';
$filePath = '/tmp/'.$uniqid;
file_put_contents($filePath, $binary);

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