Advertisement:

Author Topic: fread(): supplied argument is not a valid stream resource /Subs-Package.php  (Read 14559 times)

Offline T3CHN0

  • Full Member
  • ***
  • Posts: 630
  • Gender: Male
  • knowledge is power
    • Tarago Pravia Estima

Offline emanuele

  • SMF Super Hero
  • *******
  • Posts: 14,156
  • Gender: Male
  • THERE'S JUST ME
There are no differences between the two versions of Subs-Packages in the function involved.
I can't see how changing the file could have fixed the issue.
Try to put the Subs-Packages.php file from a freshly downloaded install package back overwriting the one you have now. I still think it was a problem with permissions or something similar.

The point where the error occurs is when SMF tries to open and read (almost) all the files to create a backup.
What's the operating system of your server?

At fopen manual page a problem with line endings is reported. Don't know if it could be relevant in this case.


Take a peek at what I'm doing! ;D



Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

Offline Chen Zhen

  • Sophist Member
  • *****
  • Posts: 1,191
  • Gender: Male
  • If you're going through hell, keep going!
    • Underdog-01 on GitHub
    • WebDev.ca
At fopen manual page a problem with line endings is reported. Don't know if it could be relevant in this case.

I encountered this same issue just now while repairing someone's forum & upgrading with SMF 2.0.2.
I was able to figure out a solution and it seems to be related to what was stated in the above quote.

When processing text files feof() will not return true for the last line of the file & gets stuck in an endless loop.

Here is a work around using fgets() that I used and it seems to do the trick:
Code: [Select]
$fp = fopen($real_file, 'rb');
while (!feof($fp))
{
$line = fgets($fp);
if (!feof($fp))
$fwrite($output, fread($fp, 16384));
}
fclose($fp);

 
« Last Edit: February 21, 2012, 02:04:57 AM by -Underdog- »

My SMF Mods & Plug-Ins

WebDev
Simple Portal Support

Request politely & demand nothing.

Offline T3CHN0

  • Full Member
  • ***
  • Posts: 630
  • Gender: Male
  • knowledge is power
    • Tarago Pravia Estima
Hello
 
 your edit fixed my forum, no more errors and no more time outs to install a mod
 or white screens.
 
 Thankyou very much....
 

Offline Chen Zhen

  • Sophist Member
  • *****
  • Posts: 1,191
  • Gender: Male
  • If you're going through hell, keep going!
    • Underdog-01 on GitHub
    • WebDev.ca
I'm glad it worked out for you.

The edit you tried from my forum had some extra logic in it.
Here is the exact edit for those having the same issue:

file: Sources / Subs-Package.php

find:
Code: [Select]
$fp = fopen($real_file, 'rb');
while (!feof($fp))
$fwrite($output, fread($fp, 16384));
fclose($fp);

Replace with:
Code: [Select]
$fp = fopen($real_file, 'rb');
if (!$fp)
continue;
     
while (!feof($fp))
{
$line = fgets($fp);
if (!feof($fp))
$fwrite($output, fread($fp, 16384));
}     
fclose($fp);

feof does not process the last line of a text file properly. ref. http://ca3.php.net/manual/en/function.feof.php (invalid pointer)
When fgets attempts to process an empty line (after the last line) it causes feof to flag true & the loop ends.
There are other work arounds for a feof infinite loop shown in the link I just provided but I opted this one.
« Last Edit: February 26, 2012, 10:23:13 PM by -Underdog- »

My SMF Mods & Plug-Ins

WebDev
Simple Portal Support

Request politely & demand nothing.

Offline T3CHN0

  • Full Member
  • ***
  • Posts: 630
  • Gender: Male
  • knowledge is power
    • Tarago Pravia Estima
your the man,
this will help hundreds with the same error.
would be a good edit for SMF to include in there install packages to stop this from happening to others who are new to SMF
cheers

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 71,982
    • StoryBB/StoryBB on GitHub
So I looked into this - that code up there looks like it's corrupting the package backups made by SMF on installing a mod, because it's reading twice from the file handle but only outputting one of the two reads that it makes.

I'd personally suggest the replacement should be:

Code: [Select]
$fp = @fopen($real_file, 'rb');
while ($fp && !feof($fp))
{
$buffer = fread($fp, 16384);
if (strlen($buffer) == 0)
break;
$fwrite($output, $buffer);
}
@fclose($fp);

This way, if the file can't be opened for whatever reason (which is conceivable, if unlikely), we won't have a valid file pointer, or if the file pointer becomes invalid through EOF at the end of a read, or we have an empty read, either way we escape the loop safely. This appears to work consistently and correctly for the testing I've done.

I'm glad this was investigated a second time because in the testing of the suggested fix I've found another bug that needs fixing.
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Offline T3CHN0

  • Full Member
  • ***
  • Posts: 630
  • Gender: Male
  • knowledge is power
    • Tarago Pravia Estima
Fix Correct installing mods Forum Freezes Hangs on Install /Subs-Package.php
« Reply #7 on: October 26, 2015, 09:29:00 PM »
So I looked into this - that code up there looks like it's corrupting the package backups made by SMF on installing a mod, because it's reading twice from the file handle but only outputting one of the two reads that it makes.

I'd personally suggest the replacement should be:

Code: [Select]
      $fp = @fopen($real_file, 'rb');
      while ($fp && !feof($fp))
      {
         $buffer = fread($fp, 16384);
         if (strlen($buffer) == 0)
            break;
         $fwrite($output, $buffer);
      }
      @fclose($fp);

This way, if the file can't be opened for whatever reason (which is conceivable, if unlikely), we won't have a valid file pointer, or if the file pointer becomes invalid through EOF at the end of a read, or we have an empty read, either way we escape the loop safely. This appears to work consistently and correctly for the testing I've done.

I'm glad this was investigated a second time because in the testing of the suggested fix I've found another bug that needs fixing.

So here I go again and once again this edit helped me.
I am so happy past post are stored so we all can look back at passed posts.
After installing about 20 mods I could not install anymore mods, Forum will hang and time out and fill the logs with errors
and the errors are not reporting the correct problem only just the php code it error on.

I used your code this time Arantor and I have just tested installing 2 mods perfectly and no errors in my logs.
So I am back on top of things again.

This edit should be changed by SMF as a default setting I think. It is very important to have this working correctly.

Thanks again guys for all your support before.

Regards
Techno