如何从存储图像的数据库's表's路径显示图像?


How do I get to display an image from a database's table's path where it is stored?

我正在使用会话从我的数据库表接收我的信息…但我不太确定如何获取图像因为如果我只输入this

echo '$_SESSION[pic_location]';

我不知道是否可以以某种方式使用img标签…

它给我一个字符串的图像的路径,而不是图像本身。我该如何解决这个问题?

这是我的picUpload.php文档

<?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
                    }
                }
            }
?> 

和我想要显示图像的profile.php

<?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';
?>

谢谢!

:)

试试这个

<?php echo '<img src="$_SESSION[pic_location]"; /> ?> 
//Considering $_SESSION[pic_location] giving you the full image path
echo "<img src='" . $_SESSION['pic_location'] . "' />";

使用HTML:

<img src="<?php echo $_SESSION['pic_location']; ?>" alt="image" />

但是要小心XSS和错误的路径

我通过修改

$target=$_SERVER['DOCUMENT_ROOT'] . "/avatars/" . basename( $_FILES['file']['name']);

:

$target = "avatars/" . $_FILES['file']['name'];

然后当我想要显示它时,我使用

echo '<img width="50" height="50" src="' . $_SESSION['pic_location'] . '">';

谢谢你的帮助!