savetree Posted May 25, 2011 Posted May 25, 2011 I have a php page that is supposed to load a random image from the folder it is in <?php error_reporting(0); header("Content-Type: image/jpg"); $d = scandir('.'); foreach($d as $f) if(substr($f,-4)==".jpg") $fs[] = $f; readFile($fs[rand(1,count($fs))-1]); ?> Why is this not working. I have checked that there are valid images, and i have seen this exact same script work on a personal server of the person who made it my domain name is savetree.heliohost.org this php is called avatarscript.php and is in the public_html directory
Guest Geoff Posted May 25, 2011 Posted May 25, 2011 Moving thread... Put header("Content-Type: image/jpg"); at the top.
savetree Posted May 25, 2011 Author Posted May 25, 2011 where do I put it? It is already in the php script is it out of the php braces? EDIT:This is the error Warning: Cannot modify header information - headers already sent by (output started at /home/savetree/public_html/avatarscript.php:1) in /home/savetree/public_html/avatarscript.php on line 2 from this code <?php header("Content-Type: image/jpg"); $d = scandir('.'); foreach($d as $f) if(substr($f,-4)==".jpg") $fs[] = $f; readFile($fs[rand(1,count($fs))-1]); ?>
Guest Geoff Posted May 25, 2011 Posted May 25, 2011 Before error_reporting(0); In fact, I would recommend removing that error_reporting(0); line
savetree Posted May 25, 2011 Author Posted May 25, 2011 I did. See my edit for my new code. Its still not working EDIT: This is what I added to the above post: This is the error Warning: Cannot modify header information - headers already sent by (output started at /home/savetree/public_html/avatarscript.php:1) in /home/savetree/public_html/avatarscript.php on line 2 from this code <?php header("Content-Type: image/jpg"); $d = scandir('.'); foreach($d as $f) if(substr($f,-4)==".jpg") $fs[] = $f; readFile($fs[rand(1,count($fs))-1]); ?>
Guest Geoff Posted May 25, 2011 Posted May 25, 2011 You have spaces before the opening PHP tag (<?php), and it is good practice to omit the closing tag (?>).
savetree Posted May 25, 2011 Author Posted May 25, 2011 Thank you it now works with this code <?php header("Content-Type: image/jpg"); $d = scandir('.'); foreach($d as $f) if(substr($f,-4)==".jpg") $fs[] = $f; readFile($fs[rand(1,count($fs))-1]);
Byron Posted May 25, 2011 Posted May 25, 2011 Here's a better way to do it: <?php $images = glob("{*.jpg,*.JPG}", GLOB_BRACE); # shuffle array shuffle($images); # take one from the top $image = array_shift($images); # redirect to browser header("location:$image"); exit; ?> without redirecting to the browser (using imagick): <?php $images = glob("{*.jpg,*.JPG}", GLOB_BRACE); # shuffle array shuffle($images); # take one from the top $image = array_shift($images); header("Content-type: image/jpeg"); $img = new Imagick($image); echo $img; ?> btw, you code works fine if you remove all of that space. I tried this on my site without any errors: <?php error_reporting(0); header("Content-Type: image/jpg"); $d = scandir('.'); foreach($d as $f) if(substr($f,-4)==".jpg") $fs[] = $f; readFile($fs[rand(1,count($fs))-1]); ?> @Geoff: I've never read anywhere that it is good practice to omit the php closing tag. I'm not saying your wrong, but could you provide documentation to this? EDIT: Nevermind Geoff, I did a search and found some arguments about leaving off the php closing tag. I myself would rather just remove the white space to keep from getting the dreaded "headers already sent" error myself. http://activeblogging.com/info/can-you-lea...ur-source-code/
Guest Geoff Posted May 25, 2011 Posted May 25, 2011 @Byron php.net itself said they omitted the closing tag in their PHP code. Reasons: - Extra typing - Risk of extra white space It's not really an issue when doing simple scripts, but when getting into advanced programming (ex: Symfony, CakePHP, Zend Framwork) worrying about people putting extra space after that PHP tag becomes a real problem. It's not a big issue; the reasoning here is why create extra trouble when you can just omit it and not worry?
Byron Posted May 26, 2011 Posted May 26, 2011 It's not really an issue when doing simple scripts, but when getting into advanced programming (ex: Symfony, CakePHP, Zend Framwork) worrying about people putting extra space after that PHP tag becomes a real problem. Since I've never really gotten into any advanced php programming, that's probably why I never heard about this.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now