PHP help

Discussion in 'Mac Programming' started by jtalerico, Jan 26, 2006.

  1. macrumors 6502

    Joined:
    Nov 23, 2005
    #1
    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!
     
  2. macrumors demi-god

    Joined:
    Oct 14, 2005
    Location:
    Virginia Beach
    #2
    Remember that PHP uses "." to join strings together, not "&". ;)

    For example,

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

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #3
    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?
     
  4. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #4
    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.
     
  5. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #5
    If you want to display an image at a website it should be something like this:

    Code:
    echo "<img src='" . $imgstr . "' alt='description' />"; 
     
  6. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #6
    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!
     
  7. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #7
    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!
     
  8. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #8
  9. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #9
    You are the man!!
     
  10. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #10
    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
     
  11. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #11
    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
     
  12. macrumors 6502

    Joined:
    Jul 29, 2004
    #12
    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.
     
  13. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #13
    what does the -4, -5 thing mean? I am trying to open ALL images in a dir.
     
  14. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #14
    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!
     
  15. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #15
    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\" />";
    ;)
     
  16. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #16
    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.">";
    }
    }
    ?>
     
  17. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #17
    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){
     
  18. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #18

    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
     
  19. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #19
    The 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... :)
     
  20. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #20
    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?
     
  21. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #21
    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.
     
  22. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #22

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

    Stampyhead

    Joined:
    Sep 3, 2004
    Location:
    London, UK
    #23
    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.
     
  24. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #24
    Hey... what's wrong with the Mac programming forum here at MR...? :eek:

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

    Nermal

    Staff Member

    Joined:
    Dec 7, 2002
    Location:
    Whakatane, New Zealand
    #25
    Forgery! :eek: Muahahaha!
     

Share This Page