Ahmed Shabanah Ahmed Shabanah - 2 months ago 18
PHP Question

Showing images uploaded on php page

I Have a problem while uploading image to my php page .. i tried this code and its worked and the image uploaded but i can't show it on the page ..

please help ^^

if ($_FILES["img"]["name"]) {

$name = $_FILES["img"]["name"];
$tmp_name = $_FILES['img']['tmp_name'];
$error = $_FILES['img']['error'];

if (!empty($name)) {
$location = '/var/www/html/1.jpg';

if (move_uploaded_file($tmp_name, $location.$name)){
echo 'Image Uploaded';
echo nl2br("\n");
echo nl2br("\n");

echo <<<HEREDOC

<div style="float:left;margin-right:10px">
<img src="{$location}" alt = "Ur Image" width="400" height="400"/>
</div>
HEREDOC;

}

}
else {
echo 'please choose a file';
}

}



Answer Source

I have a feeling that the issue is related to your $location variable. When you save the file, you save it to /var/www/html/1.jpg1.jpg since you do $location.$name, but when you try and display it, you only use the $location, which is /var/www/html/1.jpg.

Essentially, change:

<img src="{$location}" alt = "Ur Image" width="400" height="400"/>

to

<img src="{$location.$name}" alt = "Ur Image" width="400" height="400"/>

EDIT And as meta pointed out, don't use /var/www/html/... in the image source. If /var/www/html is the root of the website where the PHP file lives, change yout $location variable to

$location = '/var/www/html/';

And change

<img src="{$location}" alt = "Ur Image" width="400" height="400"/>

to

<img src="{$name}" alt = "Ur Image" width="400" height="400"/>