Jump to content

BLOB fialed to display


Joseph_J

Recommended Posts

Hello,

Code works great across site except for this modification I am trying to make:

I want to save the images to the db which I have successfully done.

However, I can not get the image to display.   Any suggestions would be helpful!

btw:  The binary code does display in the site where the picture is suppose to be.


file.php:


<div class="widget">
         <h2>Hello, <?php echo $user_data['first_name']?>!</h2>
         <div class="inner">
        
                <div class="profile">
            
    <?php
            
    
            
            if (isset($_FILES['profile']) === true) {
                if (empty($_FILES['profile']['name']) === true) {
                    echo 'Please choose a file!';
                } else {
                                        
                        $allowed = array('jpg', 'jpeg', 'gif', 'png');
                        
                        $file_name = $_FILES['profile']['name'];
                        $file_extn = strtolower(end(explode('.', $file_name)));
                        $file_temp = $_FILES['profile']['tmp_name'];
                        $file_size = getimagesize($_FILES['profile']['tmp_name']);
                        $image = addslashes(file_get_contents($_FILES['profile']['tmp_name']));
                        
                        if (in_array($file_extn, $allowed) === false) {
                        $errors[] = 'Not a valid file type! Please enter an image with a .jpg, .jpeg, .gif, or a .png file extension!';
                        }
                        
                        if ($file_size == false) {
                        $errors[] = 'This file is not a valid image!';
                        }
                                                    
                        if(empty($errors) === true) {
                        change_profile_image($session_user_id, $image);
                        echo "<script>window.location.href='index.php';</script>";
                        header('Location: index.php');
                        exit();
                            
                        } else {
                        echo output_errors($errors);
                                                    
                            }
                        
                    }
            }
            
        
            
            if (empty($user_data['profile']) === false) {
            echo '<img src="', $user_data['profile'], '" alt="', $user_data['first_name'], '\'s Profile Image">';                
            }
            
    ?>
            <form action="" method="post" enctype="multipart/form-data">
                <input type="file" name="profile"> <input type="submit" value="Upload">
            </form>
        </div>
                
            <ul>
            
                <li>
                    <a href="logout.php">Log out</a>
                </li>
                
                <li>
                    <a href="/<?php echo $user_data['username']; ?>">Profile</a>
                </li>
                                            
                <li>
                    <a href="changepassword.php">Change password</a>
                </li>
                
                <li>
                    <a href="settings.php">Settings</a>
                </li>
            
            </ul>
            
            
        </div>
</div>

called function:  $user_id is contained in $session_user_id
<?php
function change_profile_image($user_id, $image) {
        if (!$insert = mysql_query("UPDATE `users` SET `profile` = '$image' WHERE `user_id` = $user_id")) {
                echo "Problem uploading image!";
            }
    
    
}
?>



Thanks for your time!

 ~A noob

Link to comment
Share on other sites

you are trying to output the binary image data in the <img tag.

 

the src=' ... ' attribute is a URL to the image. it's the browser that fetches the image and renders/displays the image on the web page.

 

to dynamically output an image, you would put the url of a .php script into the the src=' ... ' attribute. the url would have the image's database id on the end of it - ?id=123. the .php script would take that id, perform any validation, permissions check, sql injection protection, then retrieve the image mime type and image data for the requested image, output a content-type header using the mime type and output the binary image data.

Link to comment
Share on other sites

To get you script to display the uploaded profile picture properly you'll need to do as max_qyver suggested,

 

There is another approach to this and that's to not to store actual raw image in the database. When the image is uploaded save the image to servers file system, for example http://mysite.com/images/profiles/<users-profile-image-name>.jpg. Store only this file path to the database. Your existing code for displaying the image should work.

 

So you should be able to change this line

 $image = addslashes(file_get_contents($_FILES['profile']['tmp_name']));

to

 

// where to save the uploaded image to
$image = './images/profiles/' . $_FILES['profile']['name'];

// save the uploaded file to ./images/profiles foler
move_uploaded_file($_FILES["file"]["tmp_name"], $SERVER['DOCUMENT_ROOT'] . $image);

$image will not contain a file path to the uploaded image. You save this path to the database.

 

This line 

echo '<img src="', $user_data['profile'], '" alt="', $user_data['first_name'], '\'s Profile Image">';    

should work as expected

Link to comment
Share on other sites

 


This kind of headache illustrates exactly why it's better to store files in the file system than the database. 

 

+42. If you're not doing anything with processing or distribution then files go in the filesystem.

 

That said, you might be able to put the binary data of the image in the tag itself, which would reduce the number of hits your server takes (but if you want to reduce hits you have a busy server and a busy server shouldn't have a huge datafile for the database unles it's a big server...)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.