Reputation: 9
When I run the PHP file, the image is not show, instead just the alt
attribute value shown which is 14.JPG
. The page source is also accurate and the image is also exist in that folder.
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
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
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