Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
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

macrumors G5
Oct 14, 2005
12,292
1,403
Remember that PHP uses "." to join strings together, not "&". ;)

For example,

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

Mitthrawnuruodo

Moderator emeritus
Mar 10, 2004
14,413
1,041
Bergen, Norway
Try to start with:
Code:
<?php
$random = (rand()%3)+1;
$imgstr = "/Users/joetalerico/Sites/images/" . $random . ".jpg";
?>
...and why fopen()? Where are you opening this php script?
 

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
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.
 

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
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

macrumors 6502
Original poster
Nov 23, 2005
358
0
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!
 

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
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

macrumors 6502
Original poster
Nov 23, 2005
358
0
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

macrumors 6502
Jul 29, 2004
346
0
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

macrumors 6502
Original poster
Nov 23, 2005
358
0
Mitthrawnuruodo -- any ideas?

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

Moderator emeritus
Mar 10, 2004
14,413
1,041
Bergen, Norway
I don't really know...

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

and getimagesize() returns an array, so seeing if it equals 2 might not give many true results...

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

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
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

Moderator emeritus
Mar 10, 2004
14,413
1,041
Bergen, Norway
jtalerico said:
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 4
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:
PHP:
while($file = readdir($dir)){
    echo "Next file: images/banner/" . $file . "<br />"; // Debugging
    if(exif_imagetype("images/banner/$file") == IMAGETYPE_JPEG){
 

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
Mitthrawnuruodo said:
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:
PHP:
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
 

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
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

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
jtalerico said:
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

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

or

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

macrumors 6502
Original poster
Nov 23, 2005
358
0
kingjr3 said:
Could try to use the is_dir() function or just check the name

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

or

PHP:
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!
 

Mitthrawnuruodo

Moderator emeritus
Mar 10, 2004
14,413
1,041
Bergen, Norway
Nermal said:
Stampyhead said:
You should check out the Sitepoint forums too (http://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... ;)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.