簡體   English   中英

在php中使用ftp將文件上傳到服務器

[英]upload the file into the server using ftp in php

我試圖將圖像上傳到服務器,但是我沒有訪問該文件夾的權限,所以我使用了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);
    ?>

函數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);

請添加ftp_pasv($ conn_id,true); 成功登錄后。 然后,只有這將起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM