Tina
Tina

Reputation: 9

PHP no image display from directory

When I run the PHP file, the image is not show, instead just the altattribute value shown which is 14.JPG . The page source is also accurate and the image is also exist in that folder.

page source

Note - there are also some echo statements before this code. they are for other purposes.

Do I need to use headers also when displaying image this way?

$dir = "C:/xampp/htdocs/PHP";
$file = "14.JPG";

if ( file_exists($dir) == false ){
    echo 'Directory \''. $dir. '\' not found!';
}
else{
    echo '<img src="'. $dir. '/'. $file. '" alt="'. $file. '"/>';
}

Upvotes: 0

Views: 83

Answers (3)

KAREN
KAREN

Reputation: 386

Change This

 $dir = "C:/xampp/htdocs/PHP";

To This

 $dir = "PHP/";

Upvotes: 0

abhayendra
abhayendra

Reputation: 187

You apply wrong approach please use relative url make uploads folder in your PHP folder

$dir = "uploads/";
        $file = "14.JPG";

        if ( file_exists($dir) == false ){
            echo $dir .'not found!';
        }
        else{
            echo '<img src="'. $dir. '/'. $file. '" alt="'. $file. '"/>';
        }

Upvotes: 0

Styphon
Styphon

Reputation: 10447

The problem is you're getting the file path mixed up with the URL. The file path is C:/xampp/htdocs/PHP and the URL is http://localhost/PHP. You need to use the URL when setting links to file and images. Your image source should be http://localhost/PHP/14.JPG but you've set it to C:/xampp/htdocs/PHP/14.JPG. Change it and it'll work for you.

Upvotes: 4

Related Questions