简体   繁体   中英

ftp_get() does not download, instead moves file to website root folder

I have a website developed under CakePHP. I'm trying to change the download system from Apache to FTP.

I currently am doing the FTP by hand with PHP (not using any plugins or libraries).

My code succesfully connects to the FTP server etc. The problem I'm having is that when I call ftp_get($conn_id, $localFile, $remoteFile, FTP_BINARY) , it succesfully executes, though the file does not download, instead, its moved to the /webroot/ folder, which serves, as the name suggests, as the root folder of the website (using .httaccess rules).

I mention the .httaccess because I may suspect this is what causing the FTP to route the download to "moving" it to the root, but I'm not sure.

The following is my FTP download code:

    $conn_id = ftp_connect($host);
    $login_result = ftp_login($conn_id,$ftp_user,$ftp_pass);

     if($login_result)
     {
         try 
         {
             if(ftp_get($conn_id, $localFile, $remoteFile, FTP_BINARY))
             {
                 $this->log("Downloaded...","debug");
             }
             else
             {
                 $this->log("Couldn't download...","debug");
             }
         }
         catch(Exception $e)
         {
             $this->log("Error: " . $e->getMessage(),"debug");
         }
     }
     else
     {
         $this->log("Coudln't connect to FTP server...","debug");
     }

     ftp_close($conn_id);

Yes, I checked (printed out) the $conn_id and the $login_result and they are working.

What is inside the paths?

 $remoteFile = "/downloads/rdticoiroe1432584529/YourMusic.zip";
 $localFile = "Music.zip";

The code does not throw any errors. I also tried using the fotografde/cakephp-ftp repo plugin for cakephp and FTP, and it does the same behaviour...

EDIT 1:

Its a music download site, right now we serve file downloads with Apache (which is very slow), the idea is to move to FTP downloads. I'm trying to use FTP protocol to download the file when the client requests download of it.

EDIT 2:

The whole idea of this question, is me trying to move to a more optimized method to serve files to clients. I own a 100Mbit transfer server and downloads are preeetty slow. I'm using Apache at the moment to download files to clients who request it.

I completely misunderstood about using PHP-FTP to serve files to the clients Hard Drive.

So, I'm looking for some guidance at what methods/protocols do people use when they serve files to clients who request download. (This is a music download site).

You have a fundamental misunderstanding here: the "remote file" in an FTP connection is something remote from you - a file on another server somewhere, for instance. In your case, I think the file you're trying to serve is "local" in this sense, but you want a way of getting it to a client quicker.

There is no way of pushing a file out to a client's hard drive (think of the security implications), you can only make it available . Right now, the client can request it using HTTP, and Apache will give them the content. If you installed an FTP server, they could connect with an FTP client and request it that way. Similarly, you could install a different web server, like nginx or lighttpd, which might be faster.

Nothing you do in PHP, however, will be faster than just Apache, because you still have Apache involved, but now you have your PHP code as well, which at best requires the time to execute your code, before doing what Apache was going to do anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM