Author Topic: download files more than 200 M ?  (Read 24834 times)

0 Members and 1 Guest are viewing this topic.

Offline abdoh2010

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Racing 4 Education
download files more than 200 M ?
« on: December 12, 2005, 06:00:33 PM »
after researching i find out that the problem is start with this code inside download.php witch download by buffering witch will not serve me with files more that 200 M,
Code: [Select]
function get_file_data($file_path) {
  global $script_url;
  ob_start();
  @ob_implicit_flush(0);
  @readfile($file_path);
  $file_data = ob_get_contents();
  ob_end_clean();
  if (!empty($file_data)) {
    return $file_data;
  }
  elseif (is_remote_file($file_path)) {
    $file_data = get_remote_file($file_path);
  }
  else {
    if (!file_exists($file_path)) {
      $file_path = preg_replace("/\/{2,}/", "/", get_document_root()."/".$file_path);
    }
    if (file_exists($file_path)) {
      $file_size = @filesize($file_path);
      $fp = @fopen($file_path, "rb");
      if ($fp) {
        $file_data = @fread($fp, $file_size);
        @fclose($fp);
      }
    }
  }
  if (empty($file_data)) {
    if (ereg("^\/", $file_path)) {
      preg_match("/^(http:\/\/[^\/]+)/i", $script_url, $regs);
      $script_url = $regs[1];
    }
    $file_data = get_remote_file($script_url."/".$file_path);
  }
  return (!empty($file_data)) ? $file_data : 0;
}
so i start a thread on webhostingtalk asking for new code witch may serve me and we finally agrae that the best way is by this code
Code: [Select]
$handle = fopen("http://www.example.com/", "rb");
$contents = '';
while (!feof($handle)) {
    $contents .= fread($handle, 8192);
    // do something else
}
fclose($handle);

i tried to use it on the funcation but i didnt get it right

so can any one help me with it please ?

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: download files more than 200 M ?
« Reply #1 on: December 13, 2005, 12:49:05 AM »
I see no difference between this code and the original, atleast in the final.
But, you've gave me another idea.
By default 4images read entire file into memory, that allow it to determing exact size of the file (only usefull for remote files) and that is the problem on most servers where memory limit per script is very low.
Now, my idea is to start sending the file content while its still reading it, and theoreticaly, if I'm not misstaken, it should not use as much memory...
I'll do some research on this one and see if it works in reality :D

P.S. I'm using this kind of method to control the download speed :)
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: download files more than 200 M ?
« Reply #2 on: December 13, 2005, 04:16:39 AM »
I'm back with good news :)
My theory above was correct. I've tested it with 700mb file. With default 4images the whole 700mb were loaded into the memory (I was monitoring it with linux "top" command).
After implimenting my code, the memory usage didnt move from normal, and file was successfuly downloaded, and also, the download dialog window poped up instantly, without 10 seconds delay as with default code :)

Before we begin, please note, that zip type downloads and remote files are not supported by these changes, in fact, remote files download might not work any longer (I did not test it)

Open download.php
1) Find:
Code: [Select]
function get_file_data($file_path) {Replace with:
Code: [Select]
function get_file_data($file_path, $size = 0) {
  if ($size)
  {
    if (!file_exists($file_path) || !$size = filesize($file_path)) return false;
    return $size;
  }

2) Find:
Code: [Select]
    elseif (!$file['file_data'] = get_file_data($file['file_path'])) {Replace with:
Code: [Select]
    elseif (!$file['file_size'] = get_file_data($file['file_path'], 1)) {
3) Find:
Code: [Select]
    if ($action == "zip" && !eregi("\.zip$", $file['file_name']) && function_exists("gzcompress") && function_exists("crc32")) {Insert below:
Code: [Select]
      $file['file_data'] = get_file_data($file['file_path']);
4) A few lines below find:
Code: [Select]
    $file['file_size'] = strlen($file['file_data']);Comment it out or simply remove that line.
Note that there are two instances of this line, you need the last instance. below that line should be echo $lang['download_error']."\n<!-- EMPTY FILE PATH //-->";

5) Find:
Code: [Select]
if (!empty($file['file_data'])) {Comment it out or remove that line.

6) Find:
Code: [Select]
  echo $file['file_data'];Replace with:
Code: [Select]
if (!empty($file['file_data'])) {
  echo $file['file_data'];
}
else
{
  if ($handle = @fopen($file['file_path'], "rb"))
  {
    while (!feof($handle))
    {
      print fread($handle, 512000);
      @ob_flush();
      @flush();
    }
    fclose($handle);
  }

That's it.
In last step number 512000 represent how many bytes the script will read at ones. (1 KB = 1024 B; 1024 * 500 = 512000 B = 500 KB) You might want to play with that value to find best perfomance.
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline abdoh2010

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Racing 4 Education
Re: download files more than 200 M ?
« Reply #3 on: December 13, 2005, 09:23:16 AM »
waw waw waw

you really save the day  :D

for the zip types download i will disable it any way ,, it will save me resource
and for the remote type ,, all my downloadable files are on my website

i will test it and reply to you,

Offline abdoh2010

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Racing 4 Education
Re: download files more than 200 M ?
« Reply #4 on: December 13, 2005, 10:06:36 AM »
I tried it and it's technically work but I think that there is something wrong with this code
Code: [Select]
  if (!empty($file['file_path'])) {
    @set_time_limit(120);
    if ($remote_url) {
      header("Location: ".$file['file_path']);
      exit;
    }
    elseif (!$file['file_size'] = get_file_data($file['file_path'], 1)) {
      ?>
      <script language="javascript" type="text/javascript">
      <!--
      window.open('<?php echo $file['file_path']; ?>','imagewindow','toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes');
      // -->
      </script>
      <meta http-equiv="Refresh" content="0; URL=<?php echo $site_sess->url($url"&"); ?>">
      <?php
      
echo $lang['download_error']."\n<!-- NO FILE DATA / FILE NOT FOUND //-->";
      exit;
    }


You can test it and see by your self

The default code
http://www.racing4e.com/download.php?image_id=1266


After modify the file
http://www.racing4e.com/download1.php?image_id=1266

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: download files more than 200 M ?
« Reply #5 on: December 13, 2005, 02:45:03 PM »
getting mysql error message, cant test it...:?
maybe you should explain in words what is wrong ;)

[EDIT]
Just tested on a fresh v1.7.1
in Step 1 I've made a mistake in the searched string
in Step 4 I've added more explanation.
and in Step 6 had an extra closing bracket }

[EDIT]
Your file woked just fine on my server...
What path does it show you if you add
Code: [Select]
echo $file['file_path'];above
Code: [Select]
      echo $lang['download_error']."\n<!-- NO FILE DATA / FILE NOT FOUND //-->";
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline abdoh2010

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Racing 4 Education
Re: download files more than 200 M ?
« Reply #6 on: December 15, 2005, 11:08:10 AM »
sorry for lat

i was out of town

the site is back again if you would like to see the link again


the problem is that refresh is come repetly with out stop and the webpage is this

Code: [Select]
      <script language="javascript" type="text/javascript">
      <!--
      window.open('http://www.racing4e.com/data/media/51/Riyadh%20Event.zip','imagewindow','toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes');
      // -->
      </script>
      <meta http-equiv="Refresh" content="0; URL=">
      Error with download
<!-- NO FILE DATA / FILE NOT FOUND //-->


also the new window is open repetly in visable and with out downloading the file
just open new visable window and close it and opening another window again and again


Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: download files more than 200 M ?
« Reply #7 on: December 15, 2005, 02:32:28 PM »
do you use download_url for your files? if so, as I mentioned above, this will not work with this code.
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline abdoh2010

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Racing 4 Education
Re: download files more than 200 M ?
« Reply #8 on: December 16, 2005, 12:00:01 AM »
No

I change the code to the following
Code: [Select]
    elseif (!$file['file_size'] = get_file_data($file['file_path'], 1)) {
      ?>
  <script language="JavaScript" type="text/javascript">
  <!--
  function beginDownload() {
    idl = -1;
    idl = location.search.indexOf("idl=n");
    if (idl < 0) document.write('<iframe height="0" width="0" src="<?php echo $file['file_path']; ?>"></iframe>');
  }
  window.onLoad=beginDownload();
  //-->
  </script>
  <noscript>
  <iframe height="0" width="0" src="<?php echo $file['file_path']; ?>"></iframe>
  </noscript>

      <?php
      
exit;
    }

And its work, but I have two problems

The first one is when I click on download button it move me on new Waite page.

the second problem is when I add
Code: [Select]
<meta http-equiv="Refresh" content="0; URL=<?php echo $site_sess->url($url"&"); ?>"> it give me the same Waite page raptly

I would like when I click on the download button the small download windows come immediately in the same detail page with out moving to Waite page

Offline urbanf

  • Pre-Newbie
  • Posts: 1
    • View Profile
Re: download files more than 200 M ?
« Reply #9 on: December 16, 2005, 03:32:12 PM »
Hi
i have the same problem but with tiff-files that are 10-50mb
V@nos fix helped me
but kan ju help me with zip-files to
i asume that the  lightbox start working when the zipbutton starts working 

 :lol: Urban

Offline abdoh2010

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Racing 4 Education
Re: download files more than 200 M ?
« Reply #10 on: December 16, 2005, 10:48:54 PM »
waiting for you V@no  :)

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: download files more than 200 M ?
« Reply #11 on: December 17, 2005, 12:14:19 AM »
abdoh2010, sorry, I cant help you untill I see what kind of file path for the file. As I said, if its web address (aka starts with http://) this code will not work. And what you are trying to do is simply redirect direct to the file - does not need anything "fancy" coding...but then, with your method anyone can leech/hotlink your files from other sites and you wont know about it, untilll end of the month when you see your site's bandwidth usage.
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline abdoh2010

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Racing 4 Education
Re: download files more than 200 M ?
« Reply #12 on: December 17, 2005, 06:35:38 AM »
after rechecking i find out that the problem is not with your code

the problem is that there is some files on the server that 4images can not find, like thery are not exist, but they are there.

i will try to fix this thing by editing database

Offline abdoh2010

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Racing 4 Education
Re: download files more than 200 M ?
« Reply #13 on: December 17, 2005, 03:23:29 PM »
Dear V@no

using the MOD 'Files Check' that you made solve the rest of my problems
now every thing is ok with the modification that you made for download.php

so i just want to say

thank you  :)

Offline Mirwais

  • Newbie
  • *
  • Posts: 39
    • View Profile
Re: download files more than 200 M ?
« Reply #14 on: April 14, 2006, 01:35:47 PM »
Hi!!
Does this code is for default download button in detail page??


I have problem with my videos, only 25 mb of them download.

will these codes help me without changing memory limit per script ???????????

hope you undertsand

 :cry: :cry: