Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image cannot be display after uploading

Tags:

php

image

Previously, when I tried uploading image into the database, the image won't display. When I check the path in the db and in the folder, it is correct.

Correct path in db and folder. enter image description here

enter image description here

And then when I tried to view the image that has been uploaded it says that I don't have the permission to view it.

enter image description here

I have also tried uploaded different photo extension and different photo viewer application and I still cannot view the image. Apart from that, I have tried W3School PHP5 File Upload. Again same thing happen, I cannot view my image.

This is my code :

if (!isset($_FILES['image']['tmp_name'])) 
{
    echo "";
}
else
{
    $file=$_FILES['image']['tmp_name']; 
    $location= $_SERVER['DOCUMENT_ROOT'] . '/ehars/photo/' . $_FILES["image"]["name"];
    move_uploaded_file($_FILES["image"]["tmp_name"], $_SERVER['DOCUMENT_ROOT'] . '/ehars/photo/' . $_FILES["image"]["name"]);
    mysql_query("INSERT INTO photo (location,emp_id) VALUES ('$location','$emp_id')");                  
}

Why can't I view my image? Is it because of the document root? Or is it something else? Please help me thank you.

UPDATED :

Based on the image below, my code (as shown above) is inside the admin folder. The reason why I would like to save my images in /ehars/photos so that, every level of user, admin admin2 and user can view the same photo that has been uploaded. If you could advice me what is the best way to do in order to achieve my objective above. Thanks again!

enter image description here

like image 342
amln_ndh Avatar asked Jun 01 '15 03:06

amln_ndh


2 Answers

If your URL scheme is not "file://", you should authorized your browser. I remember that you can't easily link CSS and image to the local machine due to security reasons.

like image 166
C Würtz Avatar answered Nov 02 '22 02:11

C Würtz


change your code into this

if (!isset($_FILES['image']['tmp_name'])) 
{
    echo "";
}
else
{
    $file=$_FILES['image']['tmp_name']; 
    $location='/ehars/photo/' . $_FILES["image"]["name"]; //remove $_SERVER['DOCUMENT_ROOT']
    move_uploaded_file($_FILES["image"]["tmp_name"], '/ehars/photo/' . $_FILES["image"]["name"]); // remove $_SERVER['DOCUMENT_ROOT'] . 
    mysql_query("INSERT INTO photo (location,emp_id) VALUES ('$location','$emp_id')");                  
}

why tou should change your code, because your server not gonna read windows path (c:/apache/htdocs/yourimagespath/yourimages.jpg); it should read (/images/yourimages.jpg), i asume htdocs is your root directory. and the result in your database is /ehars/photo/yourimages.jpg not c:/apache/htdoc/ehars/photo/yourimages.jpg.

hope it help you.

like image 40
cangak Avatar answered Nov 02 '22 02:11

cangak