开发者

Uploading file through FTP using PHP

开发者 https://www.devze.com 2023-01-27 19:50 出处:网络
I\'m curious how to upload file through FTP using PHP. Let\'s say I have upload form and user have uploaded a f开发者_开发知识库ile. How to transfer the file (without moving from temp directory) to so

I'm curious how to upload file through FTP using PHP. Let's say I have upload form and user have uploaded a f开发者_开发知识库ile. How to transfer the file (without moving from temp directory) to some FTP host using PHP?


Here you go:

$ftp = ftp_connect($host, $port, $timeout);
ftp_login($ftp, $user, $pass);
 
$ret = ftp_nb_put($ftp, $dest_file, $source_file, FTP_BINARY, FTP_AUTORESUME);

while (FTP_MOREDATA == $ret)
    {
        // display progress bar, or something
        $ret = ftp_nb_continue($ftp);
    }
 
// all done :-)

Error handling omitted for brevity.

Please note: you have to have ext-ftp installed and enabled.


Here is a code sample

 $ftp_server="";
 $ftp_user_name="";
 $ftp_user_pass="";
 $file = "";//tobe uploaded
 $remote_file = "";

 // set up basic connection
 $conn_id = ftp_connect($ftp_server);

 // login with username and password
 $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

 // upload a file
 if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
    echo "successfully uploaded $file\n";
    exit;
 } else {
    echo "There was a problem while uploading $file\n";
    exit;
    }
 // close the connection
 ftp_close($conn_id);


How about FTP upload via Curl? (Note: you can also use curl for SFTP, FTPS)

<?php

$ch = curl_init();
$localfile = '/path/to/file.zip';
$remotefile = 'filename.zip';
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, 'ftp://ftp_login:password@ftp.domain.com/'.$remotefile);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);
if ($error_no == 0) {
    $error = 'File uploaded succesfully.';
} else {
    $error = 'File upload error.';
}

?>


Here's a function to do it for you.

function uploadFTP($server, $username, $password, $local_file, $remote_file){
    // connect to server
    $connection = ftp_connect($server);

    // login
    if (@ftp_login($connection, $username, $password)){
        // successfully connected
    }else{
        return false;
    }

    ftp_put($connection, $remote_file, $local_file, FTP_BINARY);
    ftp_close($connection);
    return true;
}

Usage:

uploadFTP("127.0.0.1", "admin", "mydog123", "C:\\report.txt", "meeting/tuesday/report.txt");


For anyone want to show a the upload progress while doing file transfers, this is a great library php-ftp-client to start :

The code

$interval = 1;
$ftp->asyncDownload('illustrations/assets.zip', 'assets.zip', function ($stat) use ($interval) {
    ob_end_clean();
    ob_start();

    echo sprintf(
        "speed : %s KB/%ss | percentage : %s%% | transferred : %s KB | second now : %s <br>",
        $stat['speed'],
        $interval,
        $stat['percentage'],
        $stat['transferred'],
        $stat['seconds']
    );

    ob_flush();
    flush();
}, true, $interval);

Result in the browser :

Uploading file through FTP using PHP


FTP password must be in single quote otherwise it will not accept special characters

$ftp_server="";
$ftp_user_name="";
$ftp_user_pass=''; // this is the right way 
$file = "";//tobe uploaded
$remote_file = "";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号