Jump to content

Resumable Downloading


akshay

Recommended Posts

Hi.

 

I use this script in PHP to launch downloads. Works fine. But, I wanna make downloads resumable. Any suggestions?

 

Please be very helpful. I really gotta do it with a simple code.

 

<?php

$fullPath="any/path/to/file.ext";


$fd = fopen ($fullPath, "r");

if ($fd) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
    switch ($ext) {
        case "pdf":
        header("Content-type: application/pdf"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
        header("Content-Length: $fsize;\n");
        break;
        default;
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
        header("Content-Length: $fsize;\n");
    }
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd);
exit;
// example: place this kind of link into the document where the file download is offered:
// <a href="download.php?download_file=some_file.pdf">Download here</a>



?>

 

-I've already been to http://stackoverflow.com/questions/157318/resumable-downloads-when-using-php-to-send-the-file but found info was incomplete (if u could complete it... or so)

- I donot want a complex script as offered by thomthom.net

 

-I dont understand using header("Partial-Content:");  (i mean two variables passed offset, and remaining size)

 

I'd really be thankful if you can help me with a simple code for supporting Resumable Downloads.

 

Thanks! :)

 

 

 

Link to comment
Share on other sites

Other way i can do is..copy a file to a temporary path, provide resumable download (direct link), and clear link in about 3-4 days.

 

But thats not professional and would increase server load.

 

Please help.

 

Thank you

Link to comment
Share on other sites

Coz I dont know.

 

I'm a PHP intermediate and not pro. And I've not much idea about header("___"); commands, except for Location: and Refresh:

 

It's just my guess it would require 2-3 lines...

 

Please help :)

 

Thanks

Link to comment
Share on other sites

I've modified code at StackOverflow link.

 

Yet errors occur.

 

Code:

 

$file="path/to/file.ext";

$filesize = filesize($file);

$offset = 0;
$length = $filesize;

if ( isset($_SERVER['HTTP_RANGE']) ) {
        // if the HTTP_RANGE header is set we're dealing with partial content

        $partialContent = true;

        // find the requested range
        // this might be too simplistic, apparently the client can request
        // multiple ranges, which can become pretty complex, so ignore it for now
       preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);
       

        $offset = intval($matches[1]);
        $length = intval($matches[2]) - $offset;
       
} else {
        $partialContent = false;
}


$file = fopen($file, 'r');

// seek to the requested offset, this is 0 if it's not a partial content request
fseek($file, $offset);

$data = fread($file, $length);

fclose($file);

if ( $partialContent ) {
        // output the right headers for partial content

        header('HTTP/1.1 206 Partial Content');

        header('Content-Range: bytes ' . $offset . '-' . ($offset + $length) . '/' . $filesize);
}

// output the regular HTTP headers
header('Content-Type: ' . $ctype);
header('Content-Length: ' . $filesize);
header('Content-Disposition: attachment; filename="' . $fileName . '"');
header('Accept-Ranges: bytes');

// don't forget to send the data too
print($data);

 

Error:

 

ERROR

The requested URL could not be retrieved

 

While trying to process the request:

 

GET /dl/resdl.php?f=abcc182&dlpath=f63e91d899787c647670da3fe1786484&sessid=18460a3a23d2935a856dc1ba0e2c4e11 HTTP/1.1

Host: mydomain.com

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 115

Connection: keep-alive

Cookie: PHPSESSID=da19dd87e2b5862cdadd788dc3dd0d37

 

 

The following error was encountered:

 

    * Invalid Response

 

The HTTP Response message received from the contacted server could not be understood or was otherwise malformed. Please contact the site operator. Your cache administrator may be able to provide you with more details about the exact nature of the problem if needed.

 

Your cache administrator is webmaster.

Generated Sat, 24 Jul 2010 09:49:37 GMT by sv18.byethost18.org (squid/2.7.STABLE9)

Link to comment
Share on other sites

hmm, echo out the $file variable before sending the content-disposition header and see what the value is

 

EDIT: ahh of course, you are reassigning $file as a file handle. before doing $file = fopen(... assign $fileName = $file; and then use $fileName when calling the content-disposition header. That should fix it

Link to comment
Share on other sites

just as a test try $data -- i havent ever needed or wanted to do anything like this so i won't be of true help but i can give some possibilities and hope they work...

 

so try $data instead of $fileName or $file...  since you reset $file with $file = fopen(); but you set data when  you actually read the file.. so perhaps that's your key?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.