Hướng dẫn php sftp list files

  • SFTP Server
  • Lệnh sftp (client)

sftp-server trong SSH

sFTP (Secure File Transfer Protocol hay SSH File Transfer Protocol), giao thức truyền file an toàn (sử dụng SSH). Khi cài đặt OpenSSH Server, nó đã có sẵn sftp-server. Bạn chỉnh việc sử dụng một trình FTP Client có hỗ trợ giao thức SFTP để kết nối, duyệt file, tải file, upload giữa máy trạm và server.

Nội dung chính

  • sftp-server trong SSH
  • Lệnh sftp - upload / download file
  • Not the answer you're looking for? Browse other questions tagged php file-upload ftp or ask your own question.
  • How do I upload files to an FTP server?
  • How do I allow PHP to upload files?
  • How do I transfer files from one FTP server to another in PHP?
  • What is FTP PHP?

Một số trình FTP Client có thể sử dụng như: FileZilla, WinSCP, Free FTP ... hoặc sử dụng chương trình sftp của OpenSSH

Lệnh sftp - upload / download file

Khi bạn cài đặt OpenSSH Client tại máy, nó có ngay lệnh sftp, giúp bạn có ngay một trình CLient làm việc với giao thức sftp.

Để kết nối sftp thực hiện như sau

sftp [email protected]

Trong đó [email protected] - kết nối SSH được

Hướng dẫn php sftp list files

Nó kết nối và làm việc với hệ thống file giữa server và local. Nó có dấu nhắc nhập lệnh là sfpt>

Bạn có thể gõ ? nhấn enter để xem danh sách lệnh

Lệnh Thông tin
ls Liệt kê file trên Server, giống lệnh ls của Linux
lls Liệt kê file thư mục hiện tại của máy Local
cd Chuyển thư mục làm việc trên Server - giống lệnh cd của Linux
lcd Chuyển thư mục làm việc của máy Local
df [path] Thông tin file, thư mục (kích thước ...)
pwd Xem đường dẫn hiện tại của Server, giống pwd Linux
lpwd Xem đường dẫn hiện tại của máy Local
mkdir Tạo thư mục, giống mkdir Linux
renname oldpath newpath Đổi tên file trên Server
rm path Xóa file, thư mục (đệ quy thêm -r)
get remote [local] Tải file remote (thư mục), về lưu tại thư mục local hiện tại hoặc chỉ ra local. Để tải đệ quy cả thư mục thêm tham số -r
get -r data
Lệnh trên tải thư mục data, thư mục con trong data của Server về thư mục hiện tại của máy local (client)
put local [remote] Tải lên local lưu vào thư mục hiện tại của Server, hoặc chỉ ra trong tham số remote Để upload cả thư mục con bên trong, thêm vào -r
put -r mycode
Lệnh trên upload thư mục mycode, thư mục con trong mycode của máy Local lên thư mục hiện tại của Server
exit Thoát

I'm having trouble using PHP to SFTP upload files to a remote server. When I use cURL, I'm getting the error described here:

SFTP from PHP - undefined constant CURLOPT_PROTOCOLS and CURLPROTO_SFTP?

I also tried phpseclib as suggested in:

SFTP from within PHP

But when i try phpseclib, i get these errors:

Warning: require_once(Math/BigInteger.php) [function.require-once]: failed to open stream: No such file or directory in /home/john/public_html/test/Net/SSH2.php on line 53

Fatal error: require_once() [function.require]: Failed opening required 'Math/BigInteger.php' (include_path='.:/usr/share/php:/usr/share/pear') in /home/john/public_html/test/Net/SSH2.php on line 53

I then tried using system commands in php like so, but nothing happened:


I also tried


but my php server says ss2_connect is not defined.

I tried to do the following from terminal

scp file.csv 
password

But the server does not allow scp command. I do not have shell access to create ssh keys.

All i can do right now is sftp from terminal and manually upload. But I really want to automate this so a PHP website can do all this.

There aren't many tutorials on how to SFTP upload from PHP. Is it because it's a bad thing to do? If so, what should I be doing? The server I want to upload to only allows sftp connections.

I'm curious how to upload file through FTP using PHP. Let's say I have upload form and user have uploaded a file. How to transfer the file (without moving from temp directory) to some FTP host using PHP?

Nội dung chính

  • Not the answer you're looking for? Browse other questions tagged php file-upload ftp or ask your own question.
  • How do I upload files to an FTP server?
  • How do I allow PHP to upload files?
  • How do I transfer files from one FTP server to another in PHP?
  • What is FTP PHP?

jwueller

30k4 gold badges65 silver badges70 bronze badges

asked Dec 2, 2010 at 12:58

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.

Juljan

2,2611 gold badge15 silver badges20 bronze badges

answered Dec 2, 2010 at 13:03

Linus KleenLinus Kleen

32.9k11 gold badges89 silver badges99 bronze badges

1

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

answered Dec 2, 2010 at 13:06

Shakti SinghShakti Singh

81.9k20 gold badges132 silver badges150 bronze badges

2

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


answered Aug 2, 2016 at 19:40

ethanpilethanpil

2,4822 gold badges24 silver badges33 bronze badges

2

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

answered Aug 29, 2015 at 16:31

UserUser

3753 silver badges14 bronze badges

3

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 
", $stat['speed'], $interval, $stat['percentage'], $stat['transferred'], $stat['seconds'] ); ob_flush(); flush(); }, true, $interval);

Result in the browser :

answered Aug 17, 2020 at 2:05

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 = "";

answered Sep 21, 2018 at 9:12

Not the answer you're looking for? Browse other questions tagged php file-upload ftp or ask your own question.

How do I upload files to an FTP server?

If you have an FTP client like FileZilla, transferring files is a simple three-step process..

Open FileZilla from your desktop or Start menu..

Type in the following at the top and click Quickconnect. Host: ftp.dugeo.com. Username: upload. Password: upload..

Drag and drop the relevant files into the upload folder..

How do I allow PHP to upload files?

PHP File Upload.

Configure The "php.ini" File. First, ensure that PHP is configured to allow file uploads. ... .

Check if File Already Exists. Now we can add some restrictions. ... .

Limit File Size. The file input field in our HTML form above is named "fileToUpload". ... .

Limit File Type. ... .

Complete Upload File PHP Script..

How do I transfer files from one FTP server to another in PHP?

$file = "file_name. jpg"; $destination = fopen("ftp://username:[email protected]/" . $file, "wb"); $source = file_get_contents($file); fwrite($destination, $source, strlen($source)); fclose($destination); The image needs to be transferred to an FTP server.

What is FTP PHP?

PHP FTP Introduction The FTP functions give client access to file servers through the File Transfer Protocol (FTP). The FTP functions are used to open, login and close connections, as well as upload, download, rename, delete, and get information on files from file servers.