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

时间:2022-10-06 07:27:46

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...

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

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

这是我的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
                    }
                }
            }
?> 

and my profile.php where I want to display the image

和我的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';
?>

Thank you!

:)

4 个解决方案

#1


2  

Try this

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

#2


0  

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

#3


0  

Using HTML:

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

But beware of XSS and wrong paths.

但要注意XSS和错误的路径。

#4


0  

I managed to get it working by changing

我设法让它改变了

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

to this:

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

then when I wanted to display it I used

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

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

Thanks for all the help!

感谢您的帮助!

Joe :)

#1


2  

Try this

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

#2


0  

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

#3


0  

Using HTML:

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

But beware of XSS and wrong paths.

但要注意XSS和错误的路径。

#4


0  

I managed to get it working by changing

我设法让它改变了

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

to this:

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

then when I wanted to display it I used

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

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

Thanks for all the help!

感谢您的帮助!

Joe :)