Asked  7 Months ago    Answers:  5   Viewed   495 times

I am hesitated to ask this question because it looks weird. But anyway. Just in case someone had encountered the same problem already... filesystem functions (fopem, file, file_get_contents) behave very strange for http:// wrapper

  • it seemingly works. no errors raised. fopen() returns resource.
  • it returns no data for all certainly working urls (e.g. http://google.com/).
    file returns empty array, file_get_contents() returns empty string, fread returns false
  • for all intentionally wrong urls (e.g. http://goog973jd23le.com/) it behaves exactly the same, save for little [supposedly domain lookup] timeout, after which I get no error (while should!) but empty string.
  • url_fopen_wrapper is turned on
  • curl (both command line and php versions) works fine, all other utilities and applications works fine, local files opened fine

This error seems inapplicable because in my case it doesn't work for every url or host.

php-fpm 5.2.11 Linux version 2.6.35.6-48.fc14.i686 (mockbuild@x86-18.phx2.fedoraproject.org)

 Answers

64

I fixed this issue on my server (running PHP 5.3.3 on Fedora 14) by removing the --with-curlwrapper from the PHP configuration and rebuilding it.

Wednesday, March 31, 2021
 
Fernando
answered 7 Months ago
60

You should first test the existence of a file by file_exists().

try
{
  $fileName = 'uploads/Team/img/'.$team_id.'.png';

  if ( !file_exists($fileName) ) {
    throw new Exception('File not found.');
  }

  $fp = fopen($fileName, "rb");
  if ( !$fp ) {
    throw new Exception('File open failed.');
  }  
  $str = stream_get_contents($fp);
  fclose($fp);

  // send success JSON

} catch ( Exception $e ) {
  // send error message if you can
} 

or simple solution without exceptions:

$fileName = 'uploads/Team/img/'.$team_id.'.png';
if ( file_exists($fileName) && ($fp = fopen($fileName, "rb"))!==false ) {

  $str = stream_get_contents($fp);
  fclose($fp);

  // send success JSON    
}
else
{
  // send error message if you can  
}
Wednesday, March 31, 2021
 
Crontab
answered 7 Months ago
97

This

foreach($matches[1] as $content)
     echo $content."rn";

only iterates over the array and makes $content the last element (you have no {} so it is a one liner).

Simple demo of your issue, https://eval.in/806352.

You could use use implode though.

fwrite($file,implode("nr", $matches[1]));

You also could simplify this by using file_put_contents. Per the manual:

This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.

So you could just do:

$re = '/<li><a href="(.*?)"/';
$str = '
<li><a href="http://www.example.org/1.html"</a></li>
                        <li><a href="http://www.example.org/2.html"</a></li>
                        <li><a href="http://www.example.org/3.html"</a></li> ';

preg_match_all($re, $str, $matches);
echo '<div id="pin" style="float:center"><textarea class="text" cols="110" rows="50">';
file_put_contents("1.txt", implode("nr", $matches[1]));
Wednesday, March 31, 2021
 
SubniC
answered 7 Months ago
49

The problem is probably your console being unable to print such a long string. If you try:

System.out.println(sb.toString().length());

instead, it will print what you expect (786432).

Friday, August 13, 2021
 
ala
answered 2 Months ago
ala
26

The String class is immutable; str.Replace will not alter str, it will return a new string with the result. Try this one instead:

String str = "{3}";
str = str.Replace("{", String.Empty);
str = str.Replace("}", String.Empty);

Console.WriteLine(str);
Tuesday, August 24, 2021
 
André Pena
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :