user2263764
user2263764

Reputation: 351

Upload file with sftp and php

I'm using the library phpseclib0.3.7 to connect via SFTP to my web server. The users that are present have the following structure:

/dirhome/FirstUser/FirstUser/
/dirhome/SecondUser/SecondUser/
/dirhome/....../.....

Where:

   dirhome: 
      own        -> root:root
      permission -> 700
   FirsUser: (is the name of a user (example))
      own        -> root:root
      permission -> 755
   FirsUser: (is the name of a user (example))
      own        -> FirstUser:mygroup
      permission -> 700

The same structure for the second user and so on. With this facility, users can view / edit / create content only within their directory.

I can easily connect in PHP using the library and view folders/files, but do not know how to upload files from user's local PC on the remote server within your personal folder.

The library provides the method:

   ->put($remotepath, $localpath, NET_SFTP_LOCAL_FILE);

Il problema è: How can I open a dialog box to allow the user to select a file on user's PC and upload in her personal directory?

If I put: $localpath = "C:\\Users\\****\\Desktop\\test.txt"

I get: C:\\Users\\****\\Desktop\\test.txt is not a valid file

But if I take a file that resides locally on the server works fine, but it does not make sense because users can not put its files. There are several days that I try but without success. Also the download does not work, essentially for the same problem.

Upvotes: 1

Views: 1381

Answers (1)

Johannes H.
Johannes H.

Reputation: 6167

PHP is a server side scripting language. When your browser requests a PHP generated site, PHP runs on the server and your browser only gets to see the result. For that reason, PHP obviously has no way to access client side files: it already ran before anything even reaches the client computer.

I'm assuming the server with the PHP files on it and the SFTP server are different servers, otherwise your whole question doesn't make too much sense.

So, what you need to do here is a two step approach: First, you need to upload the files to the server who runs the PHP files the regular way, using a HTTP POST request. You can send the request to a PHP script, that then uses SFTP to move the files to the other server.


For downloads (as you asked this in your comments) it works similar: the browser requests a PHP script that fetches the file from the SFTP server, and then sends it to the browser as HTTP response. The browser should then display a regular file download dialog and the user can select where to store it.


FOr uploading you should consider using some kind of cron job or a background job started using PHP's exec() instead, as you will most likely either run into max execution timeouts or have to set them way higher than you should if you upload them usign PHP, especially for large files. The alternative is to use a separate PHP configuration (depending on what version of PHP you are running you can use .htaccess files, .user.ini files or different PHP-FPM pools for that) to increase the execution time for only the upload script.

Upvotes: 1

Related Questions