fread(): supplied argument is not a valid stream resource /Subs-Package.php

Started by T3CHN0, November 18, 2011, 08:46:00 PM

Previous topic - Next topic

emanuele

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.

Chen Zhen

Quote from: emanuele on November 19, 2011, 07:02:07 AM
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:

$fp = fopen($real_file, 'rb');
while (!feof($fp))
{
$line = fgets($fp);
if (!feof($fp))
$fwrite($output, fread($fp, 16384));
}
fclose($fp);



My SMF Mods & Plug-Ins

WebDev

"Either you repeat the same conventional doctrines everybody is saying, or else you say something true, and it will sound like it's from Neptune." - Noam Chomsky

T3CHN0

Hello

your edit fixed my forum, no more errors and no more time outs to install a mod
or white screens.

Thankyou very much....

Chen Zhen

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:

$fp = fopen($real_file, 'rb');
while (!feof($fp))
$fwrite($output, fread($fp, 16384));
fclose($fp);


Replace with:

$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.

My SMF Mods & Plug-Ins

WebDev

"Either you repeat the same conventional doctrines everybody is saying, or else you say something true, and it will sound like it's from Neptune." - Noam Chomsky

T3CHN0

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

Arantor

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:

$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.

T3CHN0

Quote from: Arantor on December 30, 2013, 10:03:54 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:

      $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

Advertisement: