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';
}
}
Don't use physical location /var/www...
in src
, use URL instead (which would depend on your http server configuration). But you can try something like http://your.domain/1.jpg
or localhost/i.jpg or you could even try relative path src="/1.jpg"
or src="html/1.jpg"
make sure the image is uploading where you think it is suppoed to go
if(isset($_POST['addImage'])){
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
$target_dir = "$myfolder/";
$target_file = $target_dir . basename($_FILES['userfile']['name']);
move_uploaded_file($_FILES['userfile']['tmp_name'], $target_file);
} else {
die('<br>An error occurred importing the file: '.$_FILES['userfile']['error']);
}
then show image with src='$target_file'
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"/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.