문제 설명
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 Prabha、lukassteiner、Manikandan Venkatesan)