2017-05-10 6 views
1

PHP를 사용하여 선택한 로컬 파일을 FTP로 업로드하고 싶습니다.PHP를 사용하여 선택한 파일을 FTP로 업로드

<form method="POST" ENCTYPE="multipart/form-data"> 
    <input type="file" name="filename"/><br/> 
    <input type="submit" value="Upload"/> 
</form> 

그리고이 업로드 시도 :

$source_file=$_FILES['filename']['tmp_name']; 
$remote_file='/www/img/file.txt'; 



if (ftp_put($conn_id, $remote_file, $source_file, FTP_BINARY)) 
{ 
echo "successfully uploaded $source_file\n"; 
} 
else 
{ 
echo "There was a problem while uploading $source_file\n"; 
} 

결과 :

경고 : ftp_put() :

로컬 파일은 $_POST 방법을 사용하여 선택 파일 이름은 비워 둘 수 없습니다

$_FILES['filename']['tmp_name']이 비어 있습니다.

나는 $source_file이 경로 여야한다는 것을 알고 있습니다. 그래서 질문이 있습니다 : 선택한 파일의 올바른 경로는 무엇입니까?

+1

시도 지역에 업로드 된 파일을 먼저 이동 한 다음 ftp로 이동하십시오 .https : //secure.php.net/manual/en/function.move-uploaded-file.php –

+0

[파일 업로드 가능 FTP 및 PHP를 사용하여] (http://stackoverflow.com/questions/34465646/upload-a-file-using-ftp-and-php) –

답변

0

다음 단계를 수행해야

(1) 이동 업로드 된 파일 첫 번째 서버

$uploads_dir = '/uploads'; 

$remote_file='/www/img/file.txt'; 

$source_file=$_FILES['filename']['tmp_name']; 

$name = basename($_FILES["filename"]['tmp_name']); 

$file_path = $uploads_dir."/".$name; 

move_uploaded_file($source_file, $file_path); 

(2) FTP로 로컬 서버의 파일 경로를 부여 업로드

if (ftp_put($conn_id, $remote_file, $file_path, FTP_BINARY)) 
{ 
echo "successfully uploaded $source_file\n"; 
} 
else 
{ 
echo "There was a problem while uploading $source_file\n"; 
} 
+0

감사합니다. 그것은 작동합니다 :) *, PNG없이 모든 확장명을 업로드 할 수 있습니다. PNG 파일을 어떻게 업로드 할 수 있는지 알고 계십니까? – MaciejM

+0

Yay Congrats @MaciejM – Akshay

+0

네가'$ remote_file = '/ www/img/file.txt'라고 명명했기 때문에 네가 업로드 된 파일에서 가져온 확장자로 동적으로 만든다. – Akshay