1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

helpt with a script

Discussion in 'PHP' started by izlik, Jan 7, 2010.

  1. #1
    Hello guys. i got this code of a friend who found it somewhere. it rotates images on my site randomly each time it's reloaded. i was wondering if someone could help me out here, i wanna display the image name under the image shown, so for example when someone updates the page and the script chooses an image let's say "justme.jpg" i want it to display under the image the name of the file, wich then is "justme.jpg". wich variable keeps the filename in this script ?

    $img = null;
    
    if (substr($folder,-1) != '/') {
    	$folder = $folder.'/';
    }
    
    if (isset($_GET['img'])) {
    	$imageInfo = pathinfo($_GET['img']);
    	if (
    	    isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
            file_exists( $folder.$imageInfo['basename'] )
        ) {
    		$img = $folder.$imageInfo['basename'];
    	}
    } else {
    	$fileList = array();
    	$handle = opendir($folder);
    	while ( false !== ( $file = readdir($handle) ) ) {
    		$file_info = pathinfo($file);
    		if (
    		    isset( $extList[ strtolower( $file_info['extension'] ) ] )
    		) {
    			$fileList[] = $file;
    		}
    	}
    	closedir($handle);
    
    	if (count($fileList) > 0) {
    		$imageNumber = time() % count($fileList);
    		$img = $folder.$fileList[$imageNumber];
    	}
    }
    
    if ($img!=null) {
    	$imageInfo = pathinfo($img);
    	$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
    	header ($contentType);
    	readfile($img);
    } else {
    	if ( function_exists('imagecreate') ) {
    		header ("Content-type: image/png");
    		$im = @imagecreate (100, 100)
    		    or die ("Cannot initialize new GD image stream");
    		$background_color = imagecolorallocate ($im, 255, 255, 255);
    		$text_color = imagecolorallocate ($im, 0,0,0);
    		imagestring ($im, 2, 5, 5,  "IMAGE ERROR", $text_color);
    		imagepng ($im);
    		imagedestroy($im);
    	}
    }
    
    ?>
    PHP:

     
    izlik, Jan 7, 2010 IP
    luckymurari likes this.
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Im guessing $_GET['img']; contains your file name.
     
    danx10, Jan 7, 2010 IP
  3. luckymurari

    luckymurari Active Member

    Messages:
    629
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    90
    #3
    No, $_GET['img] doesn't contain it. The name is in $imageInfo['basename'] .
    You could print it by putting <?php echo $imageInfo['basename']; ?>

    Echoing $_GET['img'] .. prints out entirew path .. sokmething like /foldername/filename.jpg.
     
    luckymurari, Jan 7, 2010 IP
  4. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #4
    thanks a lot! :)
     
    Last edited: Jan 7, 2010
    izlik, Jan 7, 2010 IP
  5. luckymurari

    luckymurari Active Member

    Messages:
    629
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    90
    #5
    Try out and tell if the solution worked :)

    Btw, I too have to thank you coz, I came to know about Pathinfo funtion of PHP by looking at your code :) .
     
    luckymurari, Jan 7, 2010 IP
  6. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #6
    question, can this be implented in the code above so that it displays this each time the script chooses an image? right now the script is in rotate.php and my mainpage is index.php where i have <img src="http://example.com/rotate.php"> so just putting the ecco in my index file wont work :/
     
    izlik, Jan 7, 2010 IP
  7. luckymurari

    luckymurari Active Member

    Messages:
    629
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    90
    #7
    I don't think, the script works that way. Its usign readfile to display the contents.. so, I think <img src="http://example.com/rotate.php"> will display some stary characters and image.. somehtign like <img src=" and your image file and ">
    If you have a sample, show it so that I can udnerstand.
     
    luckymurari, Jan 7, 2010 IP
  8. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #8
    heres an example. check the output http://rizzler.se/rand/index.php
     
    izlik, Jan 7, 2010 IP
  9. luckymurari

    luckymurari Active Member

    Messages:
    629
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    90
    #9
    U r giving a wrong link. Its showing some flash and no images


    Sorry, Goty it.. was assuming u were using image.

    Edit:Checked it. Yes its dumping binary like I said. In that script its not possible to do that. You need to modify your script in a different way.Rewriting is better IMO
     
    luckymurari, Jan 7, 2010 IP
  10. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #10
    ok thank you. i think i found a way to do it! :) so i have another question for you. is it possible to somehow strip text from an output somehow? if for example i echo out someting let's say public_html/www/32.swf is it possible to remove "public_html/www/" so only "32.swf" remains ?
     
    izlik, Jan 7, 2010 IP
  11. iAreCow

    iAreCow Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #11
    $path = "/home/user/public_html/file.swf";
    echo basename($path);
     
    iAreCow, Jan 7, 2010 IP