0

Well, my page contains an img like this:

<img id=".."class=".." src="setPhoto/second_photo.php">

Now, as you can see in src I have put a .php file:

<?php
$id = $_SESSION['u_id'];
$sql = "SELECT * FROM users WHERE id=$id";
$result = mysql_query("$sql");

if ($row = mysqli_fetch_assoc($result)) {
    mysql_close($link);
    header("Content-type: image/jpeg");
    echo $row['profile_front_photo'];
} else {
    // no result from database
    mysql_close($link);
    header("Content-type: image/jpeg");
    echo '../../../_images/default.jpg'; //Here is the promblem
}

?>

This code with the path, is not working. I want to set a photo to img by using a path.

Is it possible?

Alan H.
  • 16,219
  • 17
  • 80
  • 113
JexSrs
  • 155
  • 6
  • 18
  • first you have to retrieve photo from db & then set to src... In code you are going with opposite direction... do not add .php file in src – Ashu Jun 21 '18 at 06:17
  • can you paste your output – Arsalan Akhtar Jun 21 '18 at 06:36
  • You dont know what is path of default.jpg. First check what is the absolute or relative path of default.jpg according to your folder structure, then just form that path into a variable and pass it to the echo statement or image SRC – Mangesh Sathe Jun 21 '18 at 06:54

3 Answers3

2

The browser asked for an image. Instead of getting an image back, it got back a string "../../../_images/default.jpg".

Instead, you want to open that file and pass its contents through. You also need to set the correct MIME type in the response.

You should be able to find simple tutorials for this online, or take a look at e.g. https://secure.php.net/manual/en/function.fpassthru.php

Alan H.
  • 16,219
  • 17
  • 80
  • 113
-1

You can try this:

dirname(__FILE__)

it will get the root folder your project and then you specified your path like this

dirname(__FILE__). '/imagefoldername/_images/default.jpg';
Chaitanya Desai
  • 333
  • 3
  • 17
-1

Make it

$path='../../../_images/default.jpg';
echo "<img src='.$path.'>";