PDA

View Full Version : PHP help




jtalerico
Jan 26, 2006, 11:47 AM
I am trying to get a random picture script working.... I have used PHP before, but not recently...
<?php
$random = (rand()%3)+1;
$handle = fopen("/Users/joetalerico/Sites/images/"&random".jpg", "wb");
?>

So what i have is a directory that has images numberd 1 -3 1.jpg, 2.jpg, 3.jpg.... I want to generate a number 1-3 and then take that string/int and put it into the path name to be opened. If i am doing this all wrong please dont write the code for me, just point me in the right direction. Thanks!



aristobrat
Jan 26, 2006, 12:38 PM
Remember that PHP uses "." to join strings together, not "&". ;)

For example,

$date= "26";
print "Today is January " . $date. ", 2006";

Mitthrawnuruodo
Jan 26, 2006, 12:50 PM
Try to start with:
<?php
$random = (rand()%3)+1;
$imgstr = "/Users/joetalerico/Sites/images/" . $random . ".jpg";
?>
...and why fopen()? Where are you opening this php script?

jtalerico
Jan 26, 2006, 12:50 PM
Thanks, now here is what i got....


<?php
$random = (rand()%3)+1;
echo "<br>";
echo $random;
$filename = "images/".$random.".jpg";
$handle = fopen($filename, "rb");
$contents = fread($handle,filesize($filename));
?>


But the image wont display. What should i look at?

i try echo $contents but it just gives me the ascii of the image.

Mitthrawnuruodo
Jan 26, 2006, 12:53 PM
If you want to display an image at a website it should be something like this:

echo "<img src='" . $imgstr . "' alt='description' />";

jtalerico
Jan 26, 2006, 12:58 PM
okay, i guess i have to do it that way, i thought there was a different way of doing it.. Guess i was wrong...

Thanks!

jtalerico
Jan 26, 2006, 01:41 PM
New question :)

say i want to check to see if a file has the extension of .jpg

how would i do it?

if(is_file($file)) {
echo "<bhabha>";
}

Is there like a is_image ? kind of thing that you guys know of? Thanks!

Mitthrawnuruodo
Jan 26, 2006, 01:46 PM
Something like exif_imagetype() (http://no2.php.net/manual/en/function.exif-imagetype.php)? :)

jtalerico
Jan 26, 2006, 02:00 PM
You are the man!!

jtalerico
Jan 26, 2006, 09:24 PM
echo "<table cellpadding=0 cellspacing=0 border=1 width=500>\n";
echo "<tr>";
echo "<td bgcolor=light grey>\n";
$dir = opendir('images/banner/');
while(false != ($file = readdir($dir))){
if(exif_imagetype("$file")){
echo "<img src=".$file.">";
}
}

Doesnt seem to be working like i thought....


Warning: exif_imagetype() [function.exif-imagetype]: Read error! in /Users/joetalerico/Sites/global.inc on line 19

Warning: exif_imagetype() [function.exif-imagetype]: Read error! in /Users/joetalerico/Sites/global.inc on line 19

Warning: exif_imagetype(2005_0105Image_30012.jpg) [function.exif-imagetype]: failed to open stream: No such file or directory in /Users/joetalerico/Sites/global.inc on line 19

jtalerico
Jan 26, 2006, 09:34 PM
Fixed some parts of the code.... Still getting errors..

$dir = opendir('images/banner/');
while(false != ($file = readdir($dir))){
if(exif_imagetype("images/banner/"$file) == IMAGETYPE_JPEG){
echo "<img src=images/banner/".$file.">";


Warning: exif_imagetype() [function.exif-imagetype]: Read error! in /Users/joetalerico/Sites/global.inc on line 19

Warning: exif_imagetype() [function.exif-imagetype]: Read error! in /Users/joetalerico/Sites/global.inc on line 19

Poeben
Jan 26, 2006, 10:51 PM
Not the most elegant way in your case, but you could use the substr() function.

something like:

substr("filename.jpg", -4); // will return ".jpg" as string
substr("filename.jpeg", -5); // will return ".jpeg" as string

then you can dance your $var == ".jpg" ditty

I have done this sort of thing when dealing with many different file types. There is probably a better way, but whatever.

jtalerico
Jan 27, 2006, 05:17 AM
what does the -4, -5 thing mean? I am trying to open ALL images in a dir.

jtalerico
Jan 27, 2006, 06:40 AM
I have tried try-catch blocks to catch the file read error.. But still nothing... here is the code that i am working with right now...

$dir = opendir('images/banner/');
while(false != ($file = readdir($dir))){
if(getimagesize("images/banner/$file") == 2){
echo "<img src=images/banner/".$file.">";
}
}

I know it is really ugly but i would like to get it to work. I am still getting the read errors.. I know the file is there. And i have done the echo "images/banner/$file" to make sure it is there, and it worked. But i cannot get if(getimagesize("images/banner/$file") == 2){ that statement to be happy!

Mitthrawnuruodo
Jan 27, 2006, 08:50 AM
I don't really know...

But...
while(false != ($file = readdir($dir)))
should be
while(($file = readdir($dir)) != false)
or just
while($file = readdir($dir))
should also work because the while exits when the expression is false, anyway...

and getimagesize() (http://no2.php.net/manual/en/function.getimagesize.php) returns an array, so seeing if it equals 2 might not give many true results...

and
echo "<img src=images/banner/".$file.">";
will not validate (http://validator.w3.org/), and should be
echo "<img src='images/banner/".$file."' alt='Description' />";
or
echo "<img src=\"images/banner/".$file."\" alt=\"Description\" />";
;)

jtalerico
Jan 27, 2006, 09:31 AM
Cool! I think i am getting somewhere...

http://69.68.181.132/~joetalerico/gallery.php

I am still getting read errors, is there a way to throw them? Because not everything in there is a jpeg.. So maybe a try catch again?

<?
$dir = opendir('images/banner/');
while($file = readdir($dir)){
if(exif_imagetype("images/banner/$file") == IMAGETYPE_JPEG){
echo "<img src=images/banner/".$file.">";
}
}
?>

Mitthrawnuruodo
Jan 27, 2006, 09:45 AM
http://69.68.181.132/~joetalerico/gallery.php

I am still getting read errors, is there a way to throw them? Because not everything in there is a jpeg.. So maybe a try catch again?Those two warnings you get:
Warning: exif_imagetype() [function.exif-imagetype]: Read error! in /Users/joetalerico/Sites/gallery.php on line 4What excactly are you trying to read/feed the exif_imagetype() function...?

Maybe try to write out what you're doing (<-- Best debugging tip I can give ;)), something like:
while($file = readdir($dir)){
echo "Next file: images/banner/" . $file . "<br />"; // Debugging
if(exif_imagetype("images/banner/$file") == IMAGETYPE_JPEG){

jtalerico
Jan 27, 2006, 09:51 AM
Those two warnings you get:
What excactly are you trying to read/feed the exif_imagetype() function...?

Maybe try to write out what you're doing (<-- Best debugging tip I can give ;)), something like:
while($file = readdir($dir)){
echo "Next file: images/banner/" . $file . "<br />"; // Debugging
if(exif_imagetype("images/banner/$file") == IMAGETYPE_JPEG){


Yup i have done that! :) The problem is it is not a image, i want to throw that exception.. I guess when it is not a image it will show that error.. Check it out now..
http://69.68.181.132/~joetalerico/gallery.php

Mitthrawnuruodo
Jan 27, 2006, 10:13 AM
Yup i have done that! :) The problem is it is not a image, i want to throw that exception.. I guess when it is not a image it will show that error.. Check it out now..
http://69.68.181.132/~joetalerico/gallery.phpThe problem is the folder structure, you'll have to not call exit_imagetype() for this folder . and the parent folder ..

Other than that it seem to work... :)

jtalerico
Jan 27, 2006, 10:26 AM
Right.. But how can i get it to skip those two folders, they are the first thing it will hit.

maybe something like if(($dir = .) || ($dir = ..)){

Some how skip it?

jeremy.king
Jan 27, 2006, 11:10 AM
Right.. But how can i get it to skip those two folders, they are the first thing it will hit.

maybe something like if(($dir = .) || ($dir = ..)){

Some how skip it?

Could try to use the is_dir() function or just check the name


while($file = readdir($dir)){
echo "Next file: images/banner/" . $file . "<br />"; // Debugging
if(!is_dir($file) && (exif_imagetype("images/banner/$file") == IMAGETYPE_JPEG)){


or


while($file = readdir($dir)){
echo "Next file: images/banner/" . $file . "<br />"; // Debugging
if(!($file == "." || $file == "..") && (exif_imagetype("images/banner/$file") == IMAGETYPE_JPEG)){


P.S. Remember http://www.php.net has a very extensive language reference with examples.

jtalerico
Jan 27, 2006, 11:54 AM
Could try to use the is_dir() function or just check the name


while($file = readdir($dir)){
echo "Next file: images/banner/" . $file . "<br />"; // Debugging
if(!is_dir($file) && (exif_imagetype("images/banner/$file") == IMAGETYPE_JPEG)){


or


while($file = readdir($dir)){
echo "Next file: images/banner/" . $file . "<br />"; // Debugging
if(!($file == "." || $file == "..") && (exif_imagetype("images/banner/$file") == IMAGETYPE_JPEG)){


P.S. Remember http://www.php.net has a very extensive language reference with examples.


I have been using php.net alot!! :) thanks, ill give it a try!

Stampyhead
Jan 27, 2006, 02:51 PM
You should check out the Sitepoint forums too (www.sitepoint.com). There are some very knowledgeable PHP gurus over there. They are they ones I pitch my toughest PHP problems to and they have always been very helpful.

Mitthrawnuruodo
Jan 27, 2006, 07:17 PM
You should check out the Sitepoint forums too (www.sitepoint.com). There are some very knowledgeable PHP gurus over there. They are they ones I pitch my toughest PHP problems to and they have always been very helpful.I'm going to second this. I haven't actually looked at the forums, but I learnt PHP from a Sitepoint book :)Hey... what's wrong with the Mac programming forum here at MR...? :eek:

Ok, with a little help from php.net... ;)

Nermal
Jan 27, 2006, 09:24 PM
Forgery! :eek: Muahahaha!

jtalerico
Jan 29, 2006, 12:00 AM
You guys have been a great help! Thanks!

Thom_Edwards
Jan 29, 2006, 03:31 AM
echo "<img src='" . $imgstr . "' alt='description' />";

a little off-topic, but i wouldn't recommend using alt if you want 'description' to show up on mouseovers. use title instead, since that will show the text on mouseovers in all modern browsers. (alt won't respond to mouseovers in mozilla browsers and firefox.) the alt attribute is used when the image doesn't load (or using lynx) and text is put in its place. really, i guess, you would want to use both attributes.

the only reason i mention this is that i find a lot of people using alt for mouseover text.

jtalerico
Jan 31, 2006, 07:49 PM
New question!

i am using filetype() for debugging, But it seems that filetype() can only go so far back? Am i wrong?

http://69.68.181.132/~joetalerico/gallery.php

jtalerico
Jan 31, 2006, 08:19 PM
BTW i did mkdir try to do it... I dont understand why it is not coming up as a dir.

jtalerico
Feb 1, 2006, 01:24 PM
$dir = opendir("images/photos/");
while($file = readdir($dir)){
echo "<b>".$file."</b>";
if(is_dir($file) == true) {
echo filetype('images/photos/folder');
$loca = "images/photos/$file";


There is the code, for some reason some of the dir's are not showing up as dir's... Any idea why? I have chmod 744 them....

zimv20
Feb 1, 2006, 01:26 PM
I have chmod 744 them....
use 755. directories aren't accessible if they don't have the execute bit on.

jtalerico
Feb 1, 2006, 01:29 PM
use 755. directories aren't accessible if they don't have the execute bit on.

Just gave that a shot, still no go. If i do filetype() to the dir containing the dir's it works...

jtalerico
Feb 2, 2006, 07:21 AM
Possible bug in my build?