The main site grabs the file from the file server then provides it to the user. How can I set main site to generate direct download link
<p>Sorry to bother you. I have download script. Its is downloading from remote server to local server then send to user for download. The main site grabs the file from the file server then provides it to the user. I want main site to pass direct link from remote server.</p>

<p>I have no idea How can i send it to user directly. Now it is creating load and bandwidth problems on my site. Any idea, how to do this.</p>

<pre><code>public function download()
    {

        // remove session
        if (isset($_SESSION['showDownload']))
        {
            // reset session variable for next time
            $_SESSION['showDownload'] = null;
            unset($_SESSION['showDownload']);
            session_write_close();
        }

        // php script timeout for long downloads (2 days!)
        set_time_limit(60 * 60 * 24 * 2);

        // load the server the file is on
        $storageType         = 'local';
        $storageLocation     = _CONFIG_FILE_STORAGE_PATH;
        $uploadServerDetails = $this-&gt;loadServer();
        if ($uploadServerDetails != false)
        {
            $storageLocation = $uploadServerDetails['storagePath'];
            $storageType     = $uploadServerDetails['serverType'];

            // if no storage path set &amp; local, use system default
            if ((strlen($storageLocation) == 0) &amp;&amp; ($storageType == 'local'))
            {
                $storageLocation = _CONFIG_FILE_STORAGE_PATH;
            }
        }

        // get file path
        $fullPath = $this-&gt;getFullFilePath($storageLocation);

        // open file - via ftp
        if ($storageType == 'remote')
        {
            // connect via ftp
            $conn_id = ftp_connect($uploadServerDetails['ipAddress'], $uploadServerDetails['ftpPort'], 30);
            if ($conn_id === false)
            {
                $this-&gt;errorMsg = 'Could not connect to ' . $uploadServerDetails['ipAddress'] . ' to upload file.';
                return false;
            }

            // authenticate
            $login_result = ftp_login($conn_id, $uploadServerDetails['ftpUsername'], $uploadServerDetails['ftpPassword']);
            if ($login_result === false)
            {
                $this-&gt;errorMsg = 'Could not login to ' . $uploadServerDetails['ipAddress'] . ' with supplied credentials.';
                return false;
            }

            // prepare the stream of data
            $pipes = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
            if ($pipes === false)
            {
                $this-&gt;errorMsg = 'Could not create stream to download file on ' . $uploadServerDetails['ipAddress'];
                return false;
            }

            stream_set_write_buffer($pipes[0], 10000);
            stream_set_timeout($pipes[1], 10);
            stream_set_blocking($pipes[1], 0);

            $fail = false;
            $ret  = ftp_nb_fget($conn_id, $pipes[0], $fullPath, FTP_BINARY, FTP_AUTORESUME);
        }
        // open file - locally
        else
        {
            $handle = @fopen($fullPath, "r");
            if (!$handle)
            {
                $this-&gt;errorMsg = 'Could not open file for reading.';
                return false;
            }
        }

        // download speed
        $speed = 0;

        // if free/non user
        $Auth = Auth::getAuth();
        if (($Auth-&gt;loggedIn == false) || ($Auth-&gt;level == 'free user'))
        {
            $speed = (int) SITE_CONFIG_FREE_USER_MAX_DOWNLOAD_SPEED;
        }
        else
        {
            $speed = (int) SITE_CONFIG_PREMIUM_USER_MAX_DOWNLOAD_SPEED;
        }

        // do we need to throttle the speed?
        if ($speed &gt; 0)
        {
            // create new throttle config
            $config = new ThrottleConfig();

            // set standard transfer rate (in bytes/second)
            $config-&gt;burstLimit = $speed;
            $config-&gt;rateLimit = $speed;

            // enable module (this is a default value)
            $config-&gt;enabled = true;

            // start throttling
            $x = new Throttle($config);
        }

        // output some headers
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-type: " . $this-&gt;fileType);
        header("Pragma: public");
        header("Content-Disposition: attachment; filename=\"" . str_replace("\"", "", $this-&gt;originalFilename) . "\"");
        header("Content-Description: File Transfer");
        header("Content-Length: " . $this-&gt;fileSize);

        // output file - via ftp
        if ($storageType == 'remote')
        {
            while ($ret == FTP_MOREDATA)
            {
                $contents = stream_get_contents($pipes[1]);
                if ($contents !== false)
                {
                    echo $contents;
                    flush();
                }

                $ret = ftp_nb_continue($conn_id);
            }

            /*
              $contents = stream_get_contents($pipes[1]);
              if($contents !== false)
              {
              echo $contents;
              flush();
              }
             */

            fclose($pipes[0]);
            fclose($pipes[1]);
        }
        // output file - local
        else
        {
            while (($buffer = fgets($handle, 4096)) !== false)
            {
                echo $buffer;
            }
            fclose($handle);
        }
        exit();
    }

    public function loadServer()
    {
        // load the server the file is on
        if ((int) $this-&gt;serverId)
        {
            // load from the db
            $db                  = Database::getDatabase(true);
            $uploadServerDetails = $db-&gt;getRow('SELECT * FROM file_server WHERE id = ' . $db-&gt;quote((int) $this-&gt;serverId));
            $db-&gt;close();
            if (!$uploadServerDetails)
            {
                return false;
            }

            return $uploadServerDetails;
        }

        return false;
    }
</code></pre>

<p>Thank you in advance for your time.</p>

<p>Explanations:</p>

<p>I don't have direct link for any files. The function up is generating the links and the file name on remote server is different (Handel by the function mentioned above).</p>
