1

I am trying to display the image that i have uploaded and moved to the desired location. Here is the code below.

if(isset($_FILES['image']))

// image upload from upload.html 

  {

      session_start();
      $_SESSION['str'];
      $_SESSION['img'];
      $image = basename($_FILES["image"]["name"]);
move_uploaded_file($_FILES['image']['tmp_name'], $_SESSION['str'].'_5'.$_SESSION['img']);
//I am trying to display the uploaded pic
      echo '<img src= "$image"/>';
  }

The image is stored at the location $_SESSION['str']. How can i display this uploaded image.

Anurag Lal
  • 33
  • 5

4 Answers4

0

iam assuming that $_SESSION['str'] session variable is the path till the folder where the image is stored Use the below code, give the complete url of the image:

echo "<img src = '".$_SESSION['str'].DIRECTORY_SEPARATOR.$image."'"." />";
Sourabh Kumar Sharma
  • 2,864
  • 3
  • 25
  • 33
0

First of you say the location is stored in $_SESSION['str'] but you try to place an image with the value of basename($_FILES["image"][""name]).

Second; you should be using session_start() at the top of your page.

Third; in order to use a variable in a string, you need to use double quotes (") in stead of single quotes ('). Like so:

echo "<img src='$_SESSION['str']">;

But I'd use this:

echo '<img src="'. $_SESSION['str'] .'" >';

Also, are you sure you're not getting any errors? If you don't see any try placing this at the top of your file:

error_reporting(E_ALL);
ini_set('display_errors', 1);
Bono
  • 4,757
  • 6
  • 48
  • 77
0

You're using the wrong path to show the image. You're using the orginal name of the image $_FILES["image"]["name"] that was uploaded and then you use the move_uploaded_file function to move and save the file as $_SESSION['str'].'_5'.$_SESSION['img'] so that doesn't match (can't see how your session variables are created).

Also, is the location where you save the uploaded file to accessable by the client side? Move the file in the public area of your web application.

Update

I now understand from your comment that you want to save the file in a private location and then show that file in a <img> element in some HTML template. I changed the example code to embed the uploaded image into the HTML with base64.

You can use this function for creating the embed code. I took it from the answer from this question How to embed images...

function dataUri($file, $mime) 
{  
    $contents = file_get_contents($file);
    $base64   = base64_encode($contents);

    return 'data:' . $mime . ';base64,' . $base64;
}

So then you can use it like:

session_start();

// absolute path including the path to the public folder
$image_dest_path = './public/img/' . $_SESSION['str'] . '_5' . $_SESSION['img'];

// move file to server location
move_uploaded_file($_FILES['image']['tmp_name'], $image_dest_path);


// imbed the image into the HTML.
echo '<img src= "' . dataUri($image_dest_path, 'image/jpg') . '"/>';
Community
  • 1
  • 1
Sven van Zoelen
  • 6,989
  • 5
  • 37
  • 48
  • Thanks for the valuable input. But I dont want to move the image to the public location. I want to retrieve the image from the location where i stored. – Anurag Lal May 22 '15 at 08:45
0

how to call the node js method in angular js?

sriram
  • 3
  • 1
  • 3
  • In programming languages, there's a thing known as indentation. It is used to format program source code to improve readability. – mrid Oct 09 '17 at 11:53
  • example:- updateData(data){ var headers=new Headers(); headers.append("Content-Type","application/json"); return this.http.post("http://localhost:8080/update",JSON.stringify(data),{ headers:headers }).map(res => res.json()); } – sriram Jan 18 '18 at 09:40