PHP에서 ftp를 사용하여 서버에 파일 업로드 (upload the file into the server using ftp in php)


문제 설명

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

참조 솔루션

방법 1:

The third param of the function ftp_put expects a filename. You are passing the image binary as a string.

Change tis line:

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

To:

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

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

방법 2:

please add ftp_pasv($conn_id, true); after successful login. Then only this will work.

(by PrabhalukassteinerManikandan Venkatesan)

참조 문서

  1. upload the file into the server using ftp in php (CC BY‑SA 2.5/3.0/4.0)

#PHP #FTP #Android #server






관련 질문

bash의 rsync가 php 생성 --exclude-from 파일을 구문 분석하지 않음 (rsync in bash not parsing php-generated --exclude-from file)

PHP 배열 값 저장 (PHP Save array value)

검색으로 배열에서 특정 데이터 가져오기 (get specific datas from a array by a search)

창 서비스를 사용하여 PHP 파일 트리거 (Trigger a php file using window service)

yii2 컨트롤러 작업 주입은 어떻게 작동합니까? (How does the yii2 Controller action injection works)

php와 drupal을 사용하여 pdf를 텍스트로 변환 (pdf to text convert using php and drupal)

PHP에서 카테고리 및 하위 카테고리 목록 검색 (Retrieve Category & Subcategory list in PHP)

PDO - COUNT(*) 결과를 얻습니까? (PDO - get the result of a COUNT(*)?)

PHP - MySQL 쿼리 문제 (PHP - Mysql query problem)

제품용 Reviews.io API의 Foreach 루프 (Foreach loop in Reviews.io API for Products)

숫자를 나누고 점 뒤에 하나의 숫자를 유지하십시오. (Split the number and keep one number after the dot)

내 메시지 입력이 데이터베이스에 들어가지 않습니다. (My message input doesn't get into database)







코멘트