Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 02:33:17 AM

Login with username, password and session length
Search:     Advanced search
The look of the forum is still being worked on. Thank you for your patience.
193705 Posts in 16235 Topics by 17055 Members
Latest Member: mouthquiet8

* Home Help Login Register
AppleGeeks.com  |  Help / Advice  |  Programming  |  Topic: Checking for the existance of remote files in PHP 0 Members and 1 Guest are viewing this topic.
Pages: [1] 2 Go Down Print
Author Topic: Checking for the existance of remote files in PHP  (Read 7150 times)
Crash
Guest
« on: June 19, 2006, 02:31:04 PM »

I created an image gallery on my website and am still working out the bugs. One of them is that I'm trying to check for the existance of an image so that it won't display the anchor to that image if it doesn't exist. I do this by using the file_exists() function as so:
Code:
if (file_exists("{$imgdir}{$subsec}{$picid}.jpg")) {blah blah anchor code blah blah}
Do not worry about all those variables. They simply create a filename location that I have determined is correct. However, the current location is on Photobucket, since my hosting service does not let me hotlink my images that I've uploaded to the hosting server, even if I'm hotlinking them from my very own site Roll Eyes. Even though the file location is correct, file_exists still returns FALSE. I assume this is because the file is not local?
I've devised another gallery system that will use a function I stumbled upon that returns the list of all files in a directory; can't quite recall what it was. However, I would still like to know what the problem with my old system was, and also, if this same problem will exist with remote files when I try to get a list of all files in a directory using the new system.
Logged
ABoerma
Guest
« Reply #1 on: June 19, 2006, 02:36:27 PM »

Even though the file location is correct, file_exists still returns FALSE. I assume this is because the file is not local?
Correct. If I recall correctly, all these functions work only on local files.
Logged
Crash
Guest
« Reply #2 on: June 19, 2006, 03:05:53 PM »

Thanks ABoerma. Any clue to what I can do with remote files? Or should I locate a hosting service with both PHP and the ability to link images on the server?
Logged
ABoerma
Guest
« Reply #3 on: June 19, 2006, 03:11:31 PM »

I'm not sure if there is anything that can check for the existence of remote files, but, to be honest, I've never had to use anything like that either. If I were you, I'd look for another hosting service.

By the way, do you have any idea as to why can't you hotlink those local files? (You might want to check the file permissions.)
Logged
Crash
Guest
« Reply #4 on: June 19, 2006, 04:25:09 PM »

I'm not sure if there is anything that can check for the existence of remote files, but, to be honest, I've never had to use anything like that either. If I were you, I'd look for another hosting service.

By the way, do you have any idea as to why can't you hotlink those local files? (You might want to check the file permissions.)
I'll look into a new service. The no hotlinking is hosting policy. I tried messing around with the file permissions a while ago but they were already set to allow access (755).
According to chapter 39 of the PHP manual, if the command allow_url_fopen is placed in the php.ini file, most functions that take a filename as a parameter will also take URLs. I'm guessing my hosting service didn't do this.
Another system I've devised for doing the gallery uses the function imagecreatefromjpeg(). If I understand it correctly, it returns the actual image data. So perhaps if I access the images through PHP using this function instead of having the user try to access the image when the HTML code asks for it from the user-end, the service will allow it because its technically not hotlinking.

One more thing: Before I posted the stuff on Photobucket, I attempted to use the file_exists() function locally. It also returned FALSE in this situation. This leads me to conclude that file_exists lets me know if the file both physically exists and virtually exists to the user accessing the file. Correct?
Logged
ABoerma
Guest
« Reply #5 on: June 19, 2006, 05:09:38 PM »

I think I'll just draw a blank here.  Embarrassed Anyone else?
Logged
Crash
Guest
« Reply #6 on: June 19, 2006, 09:41:10 PM »

I think I'll just draw a blank here.  Embarrassed Anyone else?
Dude, no problem. I'm the one asking all these damn questions that I should be embarassed about. I think I saw this method being used by MacHall. Instead of specifying a picture file name in the src field of the img tag, the source file is instead a .php file thats getting a hash code of some sort being sent to it. I assume thats what this is, and also the reason why when you try to save their comics, the default file name is 'index.php' and the extension is .jpeg instead of the normal .jpg (imagecreatefromjpeg). I assume this is so because when you access the URL for a picture (http://www.applegeeks.com/cool.jpg), it returns to you the 0s and 1s of the picture file. Similarly a PHP file could be designed to return the 0s and 1s of a picture file as well by putting that data into a variable using the imagecreatefromjpeg() function and then returning the variable. The img tag can then take the data retrieved by the src field and place it where ever it needs to go. Just a guess of course. If this is the case, could somebody indicate whether this would be a more professional/secure way of showing pictures or would the traditional picture URL method be better?
Logged
rescbr
Newbie
*
Posts: 17


php, python 'n' c++ programmer


WWW
« Reply #7 on: June 24, 2006, 09:46:25 PM »

Well, you can bypass the image access in your hosting doing a file_get_contents and echoing the "image" (i've tested this on images stored locally on my hosting provider, maybe it works on photobucket):

Code:
<?php
$filename
=@$_GET["img"];
$explfilename=explode("."$filename);
$size=count($explfilename);

$extension=$explfilename[$size-1];

if(
$extension == "png"){
$mime="image/png";
}

if(
$extension == "jpg"){
$mime="image/jpeg";
}

//checks if file exists
if (file_exists("$filename")) {
$localfile file_get_contents("$filename");
header(&#39;Content-type: $mime&#39;); //image/png or image/jpeg
echo($localfile);
} else {
   
header("HTTP/1.0 404 Not Found"); //sends a 404
}
?>


usage: http://www.some.site.com/img.php?img=teh_image_file.png
« Last Edit: June 24, 2006, 09:55:09 PM by rescbr » Logged
Crash
Guest
« Reply #8 on: June 25, 2006, 10:54:59 AM »

Well, you can bypass the image access in your hosting doing a file_get_contents and echoing the "image" (i've tested this on images stored locally on my hosting provider, maybe it works on photobucket):

Code:
<?php
$filename
=@$_GET["img"];
$explfilename=explode("."$filename);
$size=count($explfilename);

$extension=$explfilename[$size-1];

if(
$extension == "png"){
$mime="image/png";
}

if(
$extension == "jpg"){
$mime="image/jpeg";
}

//checks if file exists
if (file_exists("$filename")) {
$localfile file_get_contents("$filename");
header(&#39;Content-type: $mime&#39;); //image/png or image/jpeg
echo($localfile);
} else {
   
header("HTTP/1.0 404 Not Found"); //sends a 404
}
?>


usage: http://www.some.site.com/img.php?img=teh_image_file.png
Although thats a unique way of doing it, I still need a way to check for file existance, which isn't possible with remote files.
Logged
dough
Full Member
***
Posts: 221


Tacos!


WWW
« Reply #9 on: June 25, 2006, 11:16:52 AM »

I"m just starting down the happy trail of learning PHP ... so my theory is based off of crazy speculation Tongue

I tried to make a dynamic page in JavaScript that'd grab all available image files (be it 1 or 1001).  It didn't work, though for many little problems.  Hence, I started to learn PHP.

Anyways, with rescbr's theory ... is it possible to add a function that will test the width of an image?  With that JavaScript run I tried, I couldn't find a way to test for the existance of images, but I just tested the image's width -- if it was greater than 0 pixels wide, I know there's an image there.

If I'm understanding things correctly, PHP should rock this.  JavaScript's problem, the images would load while the script is running and when there was an image that hadn't loaded yet, the code stopped because the "yet-to-be-there" picture had a 0 width at that time.  PHP, dunno exactly how that'd work, but being a Hypertext Preprocessor it might be able to skip that problem.

Like I said, I'm still a beginner with PHP, so I could be completely wrong -- but my suggestions are that, if you can't just test for the core existance of the image, test the image's width? or if there's a way, test the actual file size of the image? if either is above 0, you know you have something there.
Logged

Captain Jack Harkness: Who looks at a screwdriver and thinks: 'ooh, this could be a little more sonic'?
The Doctor: What, you've never been bored? Never had a long night, never had a lot of cabinets to put up?
rescbr
Newbie
*
Posts: 17


php, python 'n' c++ programmer


WWW
« Reply #10 on: June 25, 2006, 11:50:06 AM »

In PHP, to get the width of an image, you need to use the GD extension (usually installed on webhosts) like this:

Code:
<?php
function 
getWidth($img_filename){
$explfilename=explode("."$img_filename);
$size=count($explfilename);
$extension=$explfilename[$size-1];
//create image using GD library

switch($extension) {
case "png":
$im = @imagecreatefrompng($img_filename);
break;
case "gif":
/*  GIF support was removed from the GD library in Version 1.6,
    and added back in Version 2.0.28. This function is not 
    available between these versions. (from PHP.net manual) */
$im = @imagecreatefromgif($img_filename);
break;
case "jpg":
$im = @imagecreatefromjpeg($img_filename);
break;
default:
$im false
}

if($im){
return imagesx($im); //get the image width
} else {
return false//can&#39;t open image or another error
}
}
?>

Usage:
getWidth("imagefilename.png"); // returns the image width or false (error happened)

----##----

Like I said, I'm still a beginner with PHP, so I could be completely wrong -- but my suggestions are that, if you can't just test for the core existance of the image, test the image's width? or if there's a way, test the actual file size of the image? if either is above 0, you know you have something there.
Reading your topic again, it can works, but in another way, without using GD (better):

Code:
<?php
if(!
getimagesize($filename)){
//the image doesn&#39;t exists, so do something
} else {
//do another thing, because it exists!
}
?>

Quote
From PHP manual at http://www.php.net/getimagesize:
array getimagesize ( string filename [, array &imageinfo] )

The getimagesize() function will determine the size of any GIF, JPG, PNG, SWF, SWC, PSD, TIFF, BMP, IFF, JP2, JPX, JB2, JPC, XBM, or WBMP image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML <IMG> tag.

If accessing the filename image is impossible, or if it isn't a valid picture, getimagesize() will return FALSE and generate an error of level E_WARNING.

(ehm... my getWidth function can be rewritten using "getimagesize"...)
« Last Edit: June 25, 2006, 12:01:13 PM by rescbr » Logged
rescbr
Newbie
*
Posts: 17


php, python 'n' c++ programmer


WWW
« Reply #11 on: June 26, 2006, 06:27:16 AM »

Yesterday, I managed a simpler and better idea, only using fopen/fread/fclose:
(yeah, fopen also opens URLs -- only if webhosting allows)
Code:
<?php
$imgfile=@$_GET["img"];
if(!isset($imgfile)){ //if not set GET param img, return a 404 and die
header("HTTP/1.0 404 Not Found");
die();
}

$fh=fopen($imgfile"rb"); //try to open the file passed - "rb" for windoze compatibility
if(!$fh){ //some error happened! die with a 404
header("HTTP/1.0 404 Not Found");
die();
}

//read image contents
$img=fread($fh);
fclose($fh); //close for resource savings

//output the image file to user
$mime=getMimeFromExt($imgfile);
header("Content-Type: $mime");
echo $img;


//getMimeFrromExt function code below
function getMimeFromExt($filename){
//get the extension
$explfilename=explode("."$filename);
$size=count($explfilename);
$extension=$explfilename[$size-1];

//switch extensions -> mime

switch(strtolower($extension)){
case &#39;bmp&#39;: return &#39;image/bmp&#39;;
case &#39;gif&#39;: return &#39;image/gif&#39;;
case &#39;png&#39;: return &#39;image/png&#39;;
case &#39;jpg&#39;: return &#39;image/jpeg&#39;;
case &#39;jpeg&#39;: return &#39;image/jpeg&#39;;
case &#39;tif&#39;: return &#39;image/tiff&#39;;
case &#39;tiff&#39;: return &#39;image/tiff&#39;;
default: return false;
}
}
?>


use like the first code, img.php?img=filename.jpg

Hope it helps. Cheesy
« Last Edit: June 26, 2006, 06:30:45 AM by rescbr » Logged
noah howard
Sr. Member
****
Posts: 281


Otter?


WWW
« Reply #12 on: July 17, 2006, 08:24:58 PM »

Are you absolutely sure your filenames are going in properly?

I'm not familiar with the syntax you are using.
May not be your issue, btu have you tried it as:
Code:
if (file_exists($imgdir . $subsec . $picid . ".jpg")) {blah blah anchor code blah blah}
Logged

 
God never let the sun set on the British Empire, you know why? He didn't trust the buggers in the dark.
lg_alucard
Guest
« Reply #13 on: July 17, 2006, 09:46:23 PM »

Oh no, php!  Run awaaaaaaaaaay... I wish coldfusion didn't cost OMGLOTSOFMONEY dollars(USD).  Use it at work and love it!  Very much like my favorite language Ruby  Grin

Now back to the topic... I really hope you don't hand highlight that syntax... reveal your secrets!
Logged
Keizuki
Hero Member
*****
Posts: 1364


wth is personal text?!


WWW
« Reply #14 on: July 18, 2006, 06:18:04 AM »

Oh no, php!  Run awaaaaaaaaaay... I wish coldfusion didn't cost OMGLOTSOFMONEY dollars(USD).  Use it at work and love it!  Very much like my favorite language Ruby  Grin

Now back to the topic... I really hope you don't hand highlight that syntax... reveal your secrets!

PHP has auto-highlighting func.

On the forum you use...

[ code ] .... [ /code ] (minus the spaces!)

Code:
<?php

if(isset($_POST[&#39;submit&#39;]))
{
    echo $_POST[&#39;text_box_1&#39;];
}
else
{
    die(
"Hacking Attempt!");
}
?>
Logged

EU Dragonblight - lvl 80 Hunter - Vance - LF more instance goers! Cheesy
Pages: [1] 2 Go Up Print 
AppleGeeks.com  |  Help / Advice  |  Programming  |  Topic: Checking for the existance of remote files in PHP
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.116 seconds with 20 queries.