Jump to content
MakeWebGames

Updating Profile Image


Recommended Posts

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

 

(

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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);
Link to comment
Share on other sites

[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

)

 

)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

'twas the fopen() before hitting the headers.

Code updated and repiared

q7Gfq.png

 

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;
}
Link to comment
Share on other sites

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; 
}
Link to comment
Share on other sites

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):

q85YA.png

Usage of code (minor update to fit my system):

q86nX.png

Second test (within [settings|preferences].php):

q86mH.png

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

Url given: https://s-media-cache-ak0.pinimg.com...1983a96c11.jpg

Exact same code

Output:

q86J8.png

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

If it's failing in your code, your server's settings (php.ini) may not allow remote file opening via fopen()

Edited by Magictallguy
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...