I’m using sessions to receive my info from my database table… But I’m not quite sure how to get a image because if I just say this
echo '$_SESSION[pic_location]';
I don’t know if one could use the img tag in a certain way or not…
it gives me a string of the image’s path and not the image itself. How can I solve this matter?
Here is my picUpload.php document
<?php
include 'connect.php';
include 'header.php';
if(isset($_SESSION['signed_in']) && $_SESSION['signed_in'] == true)
{
//This is the directory where images will be saved
$target=$_SERVER['DOCUMENT_ROOT'] . "/avatars/" . basename( $_FILES['file']['name']);
//$target = "avatars/";
//$target = $target . basename( $_FILES['file']['name']);
//This gets all the other information from the form
$pic_location=($_FILES['file']['name']);
//Writes the information to the database
$sql = "UPDATE users SET pic_location='$target' WHERE user_id=" . $_SESSION['user_id'];
$result = mysql_query($sql);
if(!$result)
{
//something went wrong, display the error
echo "Sorry, there was a problem uploading your file.";
//echo mysql_error(); //debugging purposes, uncomment when needed
}
else
{
//Writes the photo to the server
if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['file']['name']). " has been uploaded, and your information has been added to the directory";
}
else
{
//nothing
}
}
}
?>
and my profile.php where I want to display the image
<?php
include 'connect.php';
include 'header.php';
//
if(isset($_SESSION['signed_in']) == false || isset($_SESSION['user_level']) != 1 )
{
//the user is not an admin
echo '<br/>';
echo 'Sorry! You have to be <a href="/signin.php"><b>logged in</b></a> to view your profile <a href="signup.php" title="Become a registered user!"></a>.';
echo '<br/><br/>';
}
else
{
echo '<h2>Profile of </h2>'.$_SESSION['user_name'];
echo '<h2>Info about you: </h2>';
//user_pass = '" . mysql_real_escape_string($_POST['user_pass']) . "'";
$explodedPath = explode("C:/xampp/htdocs/avatars" , $_SESSION['pic_location']);
echo '<img src="http://[localhost]'.$explodedPath[1].'" />';
echo '<br/><br/>';
echo '<b>Logged in as: </b>'.$_SESSION['user_name']; echo'<pp><a href="#" title="Change your user name">edit</a></pp>';
echo '<br/><br/>';
echo '<b>Your email address: </b>'.$_SESSION['user_email']; echo'<pp><a href="#" title="Change your email address">edit</a></pp>';
echo '<br/><br/>';
echo '<b>Your user level: </b>'.$_SESSION['user_level'].' (1 = admin AND 0 = user)';
echo '<br/><br/>';
echo '<b>Your friends: </b> // FRIENDS';
//echo 'Welcome, ' . $_SESSION['user_name'] . '! <br /><br/><br/><a href="index.php"><b>Proceed to the link sharing</b></a>.';
///////////////////////////////
/// adding users as friends ///
///////////////////////////////
//while($user = mysql_fetch_array($result))
//echo $user['user_name'].'
//<a href="addfriend.php?user='.$user['id'].'">ADD TO FRIENDS</a><br/>';
echo '<br/><br/>';
echo '<br/><br/>';
echo '<b>Do you want to upload or change your profile picture?</b>';
echo '<br/><br/>';
echo '<form action="picUpload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename: </label>
<input type="file" name="file" id="file"/>
<br/><br/>
<input type="submit" name="submit" value="Upload" />
</form>';
//NOW I WANT TO MAKE A SPECIFIC "ADD AS FRIEND" LINK NEXT TO EACH USER
}
include 'footer.php';
?>
Thank you!
🙂
Try this