Samurai Legend Posted July 7, 2016 Share Posted July 7, 2016 Hello all, I have an issue which I can not identify the issue why it occurs. Whenever, someone uses an Photobucket link to update their image it gives an error however, using an alternative image host it gives no errors? Error Message - Samurai Assault - Critical Error A critical error has occurred, and page execution has stopped. Below are the details: PHP Warning: fopen( http://s1104.photobucket.com/user/Jordan_Pollard/media/YWN.png): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found (2) Action taken: Line executed: /home/samurai/public_html/live/public/global_func.php:1519 Context at error time: Array ( => [params] => Array ( [http] => Array ( [method] => HEAD ) ) [ctx] => Resource id #4 [http_response_header] => Array ( [0] => HTTP/1.1 404 Not Found [1] => Date: Tue, 05 Jul 2016 09:30:24 GMT [2] => Server: Apache [3] => Set-Cookie: PHPSESSID=lpvp9mkgi8vjen092r0qkeqpj7; path=/; domain=.photobucket.com [4] => Expires: Thu, 19 Nov 1981 08:52:00 GMT [5] => Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 [6] => Pragma: no-cache [7] => Set-Cookie: pb_userid=ZTI2MzFlMjkyZTdjZDZiODA5NmMyYWMxM2E3ZDg2YTMkYToxOntzOjc6InRyYWNraWQiO3M6MjQ6IjE0Njc3MTEwMjQuMzA4NDk2MTY0OTQwNSI7fQ%3D%3D; expires=Sun, 23-Jul-2084 12:44:30 GMT; Max-Age=2147483646; path=/; domain=.photobucket.com [8] => Set-Cookie: pb_mobile=false; expires=Wed, 06-Jul-2016 09:30:24 GMT; Max-Age=86400; path=/; domain=.photobucket.com [9] => Set-Cookie: _sfkcs2_lpvp9mkgi8vjen092r0qkeqpj7=TQaeWewbBVqOOW%2FKIpVpHzhghsWgEYcj9n%2BK2iVetiEd0OpfQtg%3D; path=/; domain=.photobucket.com [10] => Cache-Control: no-cache [11] => Vary: User-Agent,Accept-Encoding [12] => Connection: close [13] => Content-Type: text/html; charset=utf-8 ) ) global_func.php - function isImage($url) { $url = filter_var($url, FILTER_VALIDATE_URL) ? filter_var($url, FILTER_SANITIZE_URL) : null; if(empty($url)) return false; $params = ['http' => ['method' => 'HEAD']]; $ctx = stream_context_create($params); $fp = fopen($url, 'rb', false, $ctx); if(!$fp) return false; // Problem with url $meta = stream_get_meta_data($fp); if ($meta === false) { fclose($fp); return false; // Problem reading data from url } $wrapper_data = $meta['wrapper_data']; if(is_array($wrapper_data)) foreach(array_keys($wrapper_data) as $hh) if (substr($wrapper_data[$hh], 0, 19) == "Content-Type: image") { fclose($fp); return true; } fclose($fp); return false; } line 1519: $fp = fopen($url, 'rb', false, $ctx); Quote Link to comment Share on other sites More sharing options...
HaxXXxaH Posted July 8, 2016 Share Posted July 8, 2016 try going to the link... it's not an image. http://s1104.photobucket.com/user/Jordan_Pollard/media/YWN.png Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted July 8, 2016 Share Posted July 8, 2016 Heh, that's my isImage function.. Quote Link to comment Share on other sites More sharing options...
Samurai Legend Posted July 10, 2016 Author Share Posted July 10, 2016 [uSER=53425]Magictallguy[/uSER] - Yes indeed, it is your function. You was helping me out remember :P But yeah I can't seem to see the issue why it don't work with Photobucket links? Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted July 10, 2016 Share Posted July 10, 2016 Exactly what Hax-guy said; that URL isn't an image Edit: Just tested with a direct URL given by PhotoBucket itself (http://i871.photobucket.com/albums/ab277/ANNNNNNNNA_bucket/Animated%20Gif/Psychedelic%20Animated%20Gifs/AnimatedAura.gif), seems to work fine for me Quote Link to comment Share on other sites More sharing options...
Samurai Legend Posted July 11, 2016 Author Share Posted July 11, 2016 Aha, understood. How would I make it not show the error? Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted July 11, 2016 Share Posted July 11, 2016 Set DEBUG to false in the error handler Quote Link to comment Share on other sites More sharing options...
Samurai Legend Posted July 11, 2016 Author Share Posted July 11, 2016 I need that switched on, can I not use an IF statement? Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted July 12, 2016 Share Posted July 12, 2016 I suppose you could do this: $fp = @fopen($url, 'rb', false, $ctx); Quote Link to comment Share on other sites More sharing options...
Samurai Legend Posted July 12, 2016 Author Share Posted July 12, 2016 [uSER=53425]Magictallguy[/uSER] - Thank you MTG however, I am still receiving an error: [h=1]Samurai Assault - Critical Error[/h] A critical error has occurred, and page execution has stopped. Below are the details: PHP Warning: fopen(http://s1104.photobucket.com/user/Jo...edia/YWN.png): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found (2) Action taken: Line executed: /Users/user/code/game/public/global_func.php:1519 Context at error time: Array ( => [params] => Array ( [http] => Array ( [method] => HEAD ) ) [ctx] => Resource id #38 [http_response_header] => Array ( [0] => HTTP/1.1 404 Not Found [1] => Date: Tue, 12 Jul 2016 10:34:50 GMT [2] => Server: Apache [3] => Set-Cookie: PHPSESSID=t6insiehpjfk3pq67miiri5444; path=/; domain=.photobucket.com [4] => Expires: Thu, 19 Nov 1981 08:52:00 GMT [5] => Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 [6] => Pragma: no-cache [7] => Set-Cookie: pb_userid=NWYyYWQ5ZGI0YWM5NmZiMGI1YzEwZDk1ZWUwZjllMWUkYToxOntzOjc6InRyYWNraWQiO3M6MjQ6IjE0NjgzMTk2OTAuMzgyMzY2NzYyOTc3MSI7fQ%3D%3D; expires=Sun, 30-Jul-2084 13:48:56 GMT; Max-Age=2147483646; path=/; domain=.photobucket.com [8] => Set-Cookie: pb_mobile=false; expires=Wed, 13-Jul-2016 10:34:50 GMT; Max-Age=86400; path=/; domain=.photobucket.com [9] => Set-Cookie: _sfkcs2_t6insiehpjfk3pq67miiri5444=TQaeWewbBVqOOW%2FKIpVpHzhghsWgEYcj9n%2BK2iVetiEd0OpfQtg%3D; path=/; domain=.photobucket.com [10] => Cache-Control: no-cache [11] => Vary: User-Agent,Accept-Encoding [12] => Connection: close [13] => Content-Type: text/html; charset=utf-8 ) ) Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted July 12, 2016 Share Posted July 12, 2016 $fp = @fopen($url, 'rb', false, $ctx) or exit('Couldn\'t load URL'); ? Quote Link to comment Share on other sites More sharing options...
Samurai Legend Posted July 19, 2016 Author Share Posted July 19, 2016 [uSER=53425]Magictallguy[/uSER] I still get the error Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted July 19, 2016 Share Posted July 19, 2016 My tests show no error with my code.. Quote Link to comment Share on other sites More sharing options...
TheMasterGeneral Posted July 20, 2016 Share Posted July 20, 2016 The issue appears to only happen when the URL itself does not return a "Success" header. (404, 403, etc.) When I input "http://somerandomwebsite.com/fake/url/image/watever.png" it failed on the fopen. "PHP Warning: fopen(http://somerandomwebsite.com/fake/url/image/watever.png): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden (2) Perhaps test for the returned header/response? Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted July 20, 2016 Share Posted July 20, 2016 'twas the fopen() before hitting the headers. Code updated and repiared function isImage($url = null) { $url = filter_var($url, FILTER_VALIDATE_URL) ? filter_var($url, FILTER_SANITIZE_URL) : null; if(empty($url)) return false; $params = ['http' => ['method' => 'HEAD']]; $ctx = stream_context_create($params); $fp = @fopen($url, 'rb', false, $ctx) or false; if(!$fp) return false; // Problem with url $meta = stream_get_meta_data($fp); if ($meta === false) { fclose($fp); return false; // Problem reading data from url } $wrapper_data = $meta['wrapper_data']; if(is_array($wrapper_data)) foreach(array_keys($wrapper_data) as $hh) if (substr($wrapper_data[$hh], 0, 19) == "Content-Type: image") { fclose($fp); return true; } fclose($fp); return false; } Quote Link to comment Share on other sites More sharing options...
TheMasterGeneral Posted July 20, 2016 Share Posted July 20, 2016 Weirdly enough, it works in a single file (test.php) However, in a live (in-dev) application, it does not. I find this quite strange. Code, if you need it. That's exactly how it is handled right now in my application. function isImage($url = null) { $url = filter_var($url, FILTER_VALIDATE_URL) ? filter_var($url, FILTER_SANITIZE_URL) : null; if(empty($url)) return false; $params = ['http' => ['method' => 'HEAD']]; $ctx = stream_context_create($params); $fp = @fopen($url, 'rb', false, $ctx) or false; if(!$fp) return false; // Problem with url $meta = stream_get_meta_data($fp); if ($meta === false) { fclose($fp); return false; // Problem reading data from url } $wrapper_data = $meta['wrapper_data']; if(is_array($wrapper_data)) foreach(array_keys($wrapper_data) as $hh) if (substr($wrapper_data[$hh], 0, 19) == "Content-Type: image") { fclose($fp); return true; } fclose($fp); return false; } Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted July 20, 2016 Share Posted July 20, 2016 (edited) URL given: "asdf" Code: <?php require_once __DIR__ . '/includes/globals.php'; function isImage($url = null) { $url = filter_var($url, FILTER_VALIDATE_URL) ? filter_var($url, FILTER_SANITIZE_URL) : null; if(empty($url)) return false; $params = ['http' => ['method' => 'HEAD']]; $ctx = stream_context_create($params); $fp = @fopen($url, 'rb', false, $ctx) or false; if(!$fp) return false; // Problem with url $meta = stream_get_meta_data($fp); if ($meta === false) { fclose($fp); return false; // Problem reading data from url } $wrapper_data = $meta['wrapper_data']; if(is_array($wrapper_data)) foreach(array_keys($wrapper_data) as $hh) if (substr($wrapper_data[$hh], 0, 19) == "Content-Type: image") { fclose($fp); return true; } fclose($fp); return false; } $images = [ null, '', 'http://google.com', 'http://somerandomwebsite.com/fake/url/image/watever.png', 'https://image.freepik.com/free-icon/teddy-bear_318-104767.jpg' ]; foreach($images as $image) echo (isImage($image) ? 'Yup.. <em>'.$image.'</em> is an image' : 'Nah, no image here - <em>'.($image ? $image : 'No URL given').'</em>!').'<br />'; First test (single file): Usage of code (minor update to fit my system): Second test (within [settings|preferences].php): --------------------------------------------------------------------------------------------------------------------------------------------------------------------- Url given: https://s-media-cache-ak0.pinimg.com...1983a96c11.jpg Exact same code Output: --------------------------------------------------------------------------------------------------------------------------------------------------------------------- If it's failing in your code, your server's settings (php.ini) may not allow remote file opening via fopen() Edited July 20, 2016 by Magictallguy Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.