Reputation: 71
I have uploaded the image into the folder and stored the path into the MySQL Database. The path has been stored and the image has been inserted into the folder successfully. But my problem is when i display the image from the path which is stored in db. It is not displaying. When i echo the image path, it displays the image path. I checked my browser settings, it's all ok. Here is my code. Please anybody help.
$up=move_uploaded_file($_FILES['profile']['tmp_name'],dirname($_SERVER['DOCUMENT_ROOT']).'/htdocs/upload/image/'.$name);
$path = dirname($_SERVER['DOCUMENT_ROOT']).'/htdocs/upload/image/';
$location = $path . $_FILES['profile']['name'];
$ins=mysqli_query($con, "INSERT into image (url) values ('$location')");
echo 'image uploaded and stored';
echo "$location"; //It displays D:/xampp/htdocs/upload/image/Chrysanthemum.jpg
echo '<img width="250" height="250" src= "'.$location.'"/>';//It doesn't display anything.
Upvotes: 4
Views: 4309
Reputation: 140
hey you can use below code to get path of uploaded image in dir this code will show your image
$location ='/upload/image/'. $_FILES['profile']['name']; // write your project dir name before upload if "upload is not your project dir"
$ins=mysqli_query($con, "INSERT into image (url) values ('$location')");
echo 'image uploaded and stored';
echo '<img width="250" height="250" src= "'.$location.'"/>';//It doesn't display anything.
Upvotes: 0
Reputation: 9583
Try this:
Try to use relative path
not absolute path
. The relative path helps you to run the script from any server, any directory.
$path = 'upload/image/';
$location = $path . $_FILES['profile']['name'];
move_uploaded_file($_FILES['profile']['tmp_name'], $location);
$ins = mysqli_query($con, "INSERT into image (url) values ('$location')");
echo 'image uploaded and stored';
echo '<img width="250" height="250" src= "'.$location.'"/>';
Note: you need to have a folder name
upload/image
in the same directory from where the script run. As your requirement, you want to show image from mysql, but did't do it, for that you need to query the table again.
Upvotes: 5