2011-11-29 5 views
0

php를 사용하여 URL에서 ftp로 이미지를 업로드하는 방법은 무엇입니까? php를 사용하여 ftp를 통해 URL에서 파일을 업로드하는 방법은 무엇입니까?

는 FTP로 URL에서 이미지를 업로드 할

예 : http://test.com/imagefile.jpg 또는

http://test.com/testvideo.flv 나는

내가 여기 예제 코드를 가지고, 해당 파일은 FTP 또는 컬 PHP를 통해 업로드하고 싶지만은 '아무튼 일하지 마라.

파일이 없습니다.

$ftp_server ="ftp.yoursite.com" 
$ftp_user ="yoursite user" 
$ftp_password = "yoursite pass" 
$file_to_upload = "http://test.com/imagefile.png"; 
$remote_location = "/test"; 

// set up connection or exit with message 
$flink = ftp_connect($ftp_server) or exit("Can't connect to ftp server: $ftp_server"); 

// login or at least try 
if(ftp_login($flink, $ftp_user, $ftp_password)) { 


// if login successful use ftp_put to upload the file 
// if you upload binary files use mode FTP_BINARY 
if(ftp_put($flink, $remote_location, $file_to_upload, FTP_ASCII)) { 

    echo "Success! File is uploaded!"; 
} else { 
    echo "Can't upload file"; 

} 
} else { 
echo "Can't login with this user & password"; 

} 

// close the connection 
ftp_close($flink); 

누구든이 문제를 해결할 수 있습니까? 아니면 누구나 이보다 더 좋은 제안을 할 수 있습니까? 감사합니다. 도움을 많이 주셔서 감사합니다.

+0

$의 file_to_upload는 로컬 파일 할 필요가 당신의 ftp_put을 사용하는 경우 서버. – subroutines

답변

0

기본적으로는 같은 :

 
$connection = ftp_connect($ftp_server); 

$login = ftp_login($connection, $ftp_user, $ftp_password); 

if (!$connection || !$login) { die('Connection attempt failed!'); } 

$upload = ftp_put($connection, $remote_location, $file_to_upload, FTP_ASCII); 

if (!$upload) { echo 'FTP upload failed!'; } 

ftp_close($connection); 


//or you could do as : 

$upload = copy($source, ‘ftp://user:[email protected]/path/file‘); 

두 방법은 날 위해 일했습니다. 희망 하시겠습니까?

+0

안녕하세요, 작동하지 않습니다, 나는 코드를 테스트 $ connection = ftp_connect ($ ftp_server); $ login = ftp_login ($ connection, $ ftp_user, $ ftp_password); if (! $ connection ||! $ login) {die ('연결 시도가 실패했습니다!'); } $ upload = ftp_put ($ connection, 'uploaded /', 'http://mysite.com/myfile.jpg', FTP_ASCII); if (! $ upload) {echo 'FTP 업로드에 실패했습니다!'; } ftp_close ($ connection); – devzone

0

먼저 주어진 URL에서 파일 내용을 읽은 다음 내용을 임시 파일에 저장 한 다음 ftp를 사용하여 파일을 업로드하여이 문제를 해결했습니다.

코드 :

$config = array(
    'host' => '', 
    'port'  => '', 
    'username' => '', 
    'password' => '' 
); 

function init(array $config) 
{ 
    $port = (!empty($config['port']))? intval($config['port']) : 21; 
    $resource = ftp_connect($config['host'], $port); 
    ftp_login($resource, $config['username'], $config['password']); 
    ftp_pasv($resource, true); 
    return $resource; 
} 

function uploadFile(string $url, $resource) 
{ 
    $data = file_get_contents($url); 
    $tmpFilename = tempnam('/tmp', rand(10, 100) . '_'); 
    file_put_contents($tmpFilename, $data); 
    ftp_put($resource, '/new_file', $tmpFilename, FTP_BINARY); 
    unlink($tmpFilename); 
    ftp_close($resource); 
} 

$resource = init($config); 
uploadFile(
    "https://cdn.sstatic.net/Sites/stackoverflow/img/sprites.svg?v=1b3cdae197be", 
    $resource 
); 

더 URL에서 다운로드 할 파일에 대한 응답 및 임시 파일을 다른 서버에서 파일을 저장하고 작성 ->first, secondthird

관련 문제