Javascript – Upload local file from webbrowser directly to FTP server, bypass the web server

ftpjavascriptPHPupload

I want to upload user uploaded file (from website <form>) to my FTP server, in which I want to bypass the server and want that file should be sent to FTP server directly. Is it possible using PHP or JavaScript?

In current scenario when I upload file using HTML form and PHP to Apache server, the file is stored in /tmp/ directory and then I can transfer it to FTP location. But this takes double time in uploading as the filed is first uploaded to Apache server and then to the FTP server.

Cloud servers run this way, where Apache server can be by passed and file can be posted directly to cloud server)

I want this so that we can overcome the HTTP part and want to upload large file to FTP server without getting any HTTP upload restrictions.

Best Answer

The HTML form tag does not support the FTP.


You cannot use PHP either as it cannot access local (as of webbrowser) files.


So JavaScript is the only likely solution.

The XMLHttpRequest class theoretically supports FTP:

Despite its name, XMLHttpRequest can be used to retrieve any type of data, not just XML, and it supports protocols other than HTTP (including file and ftp).

But in reality it probably does not.
See the accepted answer to What is the syntax to do a cross-domain XMLHTTPREQUEST to an FTP server?

So actually, there does not seem to be any solution readily available.


If possible, run a web server on the FTP server host and send the files using the web server (HTTP).


Another alternative is to stick with the transferring via your web server, but in a streaming mode. Post your file to the web server. Make the handling script continuously read the incoming data and have them continuously uploaded to the FTP server.

You won't save a bandwidth, but you will save time (as both transfers happen nearly in parallel).

Using the ftp_fput with a handle to the standard input should do (didn't try).

Related Topic