Having problem with getimagesize when the image is not there I get an error.

Posted on 2003-03-12
Medium Priority
Last Modified: 2010-04-06
I am trying to write a PHP script that check to see if my webcam is on and if it isn't it redirects to a offline webcam page.

I have the code get the image with getimagesize and then do an if statement to see if the image array is equal to zero and if it is redirect the user to the offline webcam page.

It works with no errors when the webcam is on, but all kind of errors when I turn the webcam off.

Warning: php_hostconnect: connect failed in /home/virtual/site191/fst/var/www/html/webcam/decide-image.php on line 7

Warning: getimagesize: Unable to open '' for reading. in /home/virtual/site191/fst/var/www/html/webcam/decide-image.php on line 7

Warning: Cannot add header information - headers already sent by (output started at /home/virtual/site191/fst/var/www/html/webcam/decide-image.php:7) in /home/virtual/site191/fst/var/www/html/webcam/decide-image.php on line 11

Here is the code:

$offlineurl = "offlinecam.htm";
$onlineurl = "cam.htm";

$image = getimagesize("");
     if ($width == 0) {
          header("Location: $offlineurl");
What I am doing wrong?
Question by:rellerbe
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3

Expert Comment

ID: 8126619

Warning: php_hostconnect: connect failed in /home/virtual/site191/fst/var/www/html/webcam/decide-image.php on line 7

means you need to put error checking around your code at least:

if ($image){
#do stuff
print "can't read from camera"

what file/ folder permssions do you have on cam1.jpg?


Author Comment

ID: 8131796
Well I dont know what permissions I have because the webcam program act as its own webserver.  I tried what you said with the error checking and now I don't get an error but the code doesn't seem to work right.

$offlineurl = "phptest1.html";
$onlineurl = "phptest2.html";

$get_image ="";
$image = getimagesize($get_image);
     if ($width == 0) {
          header("Location: $offlineurl");
          } else {
     header("Location: $onlineurl");

Any ideas?

Expert Comment

ID: 8135258
can you use getimagesize on an image on the same server as you are running this script? ie get the script to work on a local image file first.

$get_image ="cam_1.jpg"; # put an image called cam_1.jpg in the same folder as this script
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users


Author Comment

ID: 8145121
The code works, but now if I delete the image it gives me the error. Do I need to use fopen instead, or is there another way to see if the image is there and if it isn't it won't give me an error.

Expert Comment

ID: 8146106
$image= "cam_1.jpg";

if (-e $image){
print "it exists"
print "there is no file"

checks for existence of the file on your local server; haven't tried it using http:// ref. Give it a try!

Accepted Solution

markhoy earned 200 total points
ID: 8146303


Whenever you reference a file from PHP (whether through GetImageSize or ReadFile or whatever), you need to provide the full file name, not just it's location under the document root.

Understand that PHP is installed on the machine, and it will access files as if you referenced them from a command line.  It is NOT limited to accessing the webserver's document root, which is one of the things that makes PHP so cool.

In your above code, the correct call would be:
$size = GetImageSize("/var/www/html/userpics/sslcg.jpg");

Assuming that the DocumentRoot of your webserver is "/var/www/html", you can also use:
$size = GetImageSize($DOCUMENT_ROOT."/userpics/sslcg.jpg");


or, maybe:
$image = "whatever.gif";
$image_size = getimagesize($image);
print("<IMG SRC=\"$image\" image_size[3]>\n");

$image_size[0] is width in pixel
$image_size[1] is height
$image_size[2] is type (1,gif, 2,jpg, 3,png)
$image_size[3] is a string
height=# width=#


Author Comment

ID: 8146820
Thanks for the help.  You put me in the right direction. I got it to work with the @Getimagesize to supress the errors.  It seems to work well now, so thanks for helping.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question