Jump to content

PHP image page not working


Recommended Posts

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

Link to comment
Share on other sites

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]);
    ?>

Link to comment
Share on other sites

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]);
    ?>

Link to comment
Share on other sites

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]);

Link to comment
Share on other sites

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/

 

Link to comment
Share on other sites

Guest Geoff

@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?

Link to comment
Share on other sites

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. :)

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...