Jump to content

Recommended Posts

well if by broken you mean inexistent...

then you might try to fopen() it and see what happens...

 

I haven't actually tried that but if fopen() gets redirected search the result for 404 else it should return false that it isn't able to open the requested file...

Guest prozente

You can connect to a server with a socket using TCP on port 80 and submit a HEAD HTTP request

 

I'll use the telnet program as an example to illustrate the socket connection.

 

Say you wanted to see if the file

 

http://phpfreaks.com/forums/Themes/default/images/icons/profile_sm.gif

 

existed on the server

 

telnet phpfreaks.com 80

HEAD /forums/Themes/default/images/icons/profile_sm.gif HTTP/1.0

Host: phpfreaks.com

 

Now hit enter twice and you'll receive a response

 

HTTP/1.1 200 OK
Date: Thu, 19 Apr 2007 02:55:54 GMT
Server: Apache/2.2.4 (Unix) DAV/2 PHP/5.2.1
Last-Modified: Tue, 09 Jan 2007 18:46:42 GMT
ETag: "070000-230-f8916410"
Accept-Ranges: bytes
Content-Length: 560
Connection: close
Content-Type: image/gif

 

This shows the file does exist, if it didn't you would receive a 404 error

 

for example

 

telnet phpfreaks.com 80

 

HEAD /forums/Themes/default/images/icons/nonexistant.gif HTTP/1.0

Host: phpfreaks.com

Hit enter twice

 

The response you would receive would be

 

HTTP/1.1 404 Not Found
Date: Thu, 19 Apr 2007 03:01:56 GMT
Server: Apache/2.2.4 (Unix) DAV/2 PHP/5.2.1
Connection: close
Content-Type: text/html; charset=iso-8859-1

 

This shows the file doesn't exist

 

 

Now to do this in PHP you'd use a socket

and craft the HTTP packet and send it to the server.

 

Look at Example 2203 in the PHP manual on the sockets page, it shows a good example.

 

 

 

what about this, if the image fails it will display an image saying "Error loading $imgname"

 

;D

 

use

<img src = "testimage.php?file=http://www.yahoo.com/image.jpg">

 

create script below

filename = testimage.php

<?php
function LoadJpeg($imgname)
{
    $im = @imagecreatefromjpeg($imgname); /* Attempt to open */
    if (!$im) { /* See if it failed */
        $im  = imagecreatetruecolor(150, 30); /* Create a black image */
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc  = imagecolorallocate($im, 0, 0, 0);
        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
        /* Output an errmsg */
        imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
    }
    return $im;
}
header("Content-Type: image/jpeg");
$img = LoadJpeg($_GET['file']); 
imagejpeg($img);
?> 

 

 

Hope it helps

 

--MadTechie

 

 

EDIT you may need to use urlencode & urldecode  for passing the URL 4:22am here, i'm off to bed

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

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

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.