Grow your CSS skills. Land your dream job.

How To Display Stored Image Using Path?

  • MBM
    # February 18, 2014 at 8:50 am

    I have a script that uploads an image into a folder then stores the path into a database. How do I then retrieve and display the image?

    The path is stored in a variable :

    $path = "uploads/" . $_FILES["file"]["name"];
    

    https://gist.github.com/gyprosetti/74c30c10f164c55914c2

    # February 18, 2014 at 12:53 pm

    You would need a select statement.

    MBM
    # February 18, 2014 at 1:11 pm

    I want to retrieve and display the image as soon as it has been uploaded not specific images.

    __
    # February 18, 2014 at 1:26 pm

    I want to retrieve and display the image as soon as it has been uploaded not specific images.

    Then why save the path in the database at all?

    The path is stored in a variable

    Then you already have your answer… don’t you?

    MBM
    # February 18, 2014 at 1:33 pm

    Every tutorial I read suggested it’s best to store images in a folder and store the path into a database as opposed to storing images into a database. I want to allow users to store data and an image then retrieve and display the data and image, I know how to display the data but not the uploaded image.

    I tried :

    echo '<img src="'$path.'" alt="" />';
    
    # February 18, 2014 at 1:36 pm

    There are many ways to do this. Even the one I suggested above.

    I would need to know how you’re structuring your content and what your table looks like.

    I tried:

    echo '<img src="'$path.'" alt="" />';
    

    Try this:

    echo '<img src="'.$path.'" alt="" />';
    
    MBM
    # February 18, 2014 at 1:51 pm

    Thanks Chris that worked!

    FWIW the table structure :

    picid – auto incrementing integer
    path – varchar(60)

    The table stores the path so I only have two fields.

    __
    # February 18, 2014 at 2:04 pm

    Every tutorial I read suggested it’s best to store images in a folder and store the path into a database as opposed to storing images into a database.

    What I was getting at is that you save no identifying information with each path – just an arbitrary id. How will you select particular paths later? And if you’re not selecting them later, why save them at all?

    MBM
    # February 18, 2014 at 2:37 pm

    I’m developing a trading card generator. I want to :

    1. Capture data and an image in a form
    2. Retrieve that data so the user that uploaded it can edit/delete it
    3. Display ALL records that ALL users have uploaded

    If I’m going about it the wrong way let me know.

    __
    # February 18, 2014 at 3:33 pm

    # 2 is going to present a problem with your current design: there’s no way to select the images according to the user that uploaded them.

    MBM
    # February 18, 2014 at 4:23 pm

    So store the image in the same table as the other data?

    __
    # February 18, 2014 at 4:34 pm

    This has nothing to do with where you store the image.

    It’s quite simple: you can’t select records from a database based on information you didn’t record.

    If you want to be able to retrieve the paths according to which user submitted them, then you need to store information about which user submitted them.

    MBM
    # February 18, 2014 at 4:50 pm

    A user id session is created once a user logs in and only logged in users can enter data. I planned on using the the logged in users id as the image id. If it easier to store the image I’ll do that instead.

    # February 18, 2014 at 5:40 pm

    I just put this up, real quick: https://gist.github.com/alenabdula/9083666

    Might be of some use. Did it while back.

    __
    # February 19, 2014 at 5:34 am

    … only logged in users can enter data.

    The code you posted doesn’t enforce that in any way.

    I planned on using the the logged in users id as the image id.

    The code you posted doesn’t do that, either (it uses the filename the user provides*). If you did do that, however, it would mean that each user could have only one image uploaded at a time.

    * I would recommend against this, since it makes overwriting an image -accidentally or not- very easy. Using a checksum as the filename is a better approach, since it will be unique per file. You could store the user’s desired “filename” separately.

    If it easier to store the image I’ll do that instead.

    Again, it has nothing to do with how you store the image: it has to do with what information you store about the image. Right now, you’re only storing the filename (the picid is useless, because it’s arbitrary and you don’t associate it with anything).

    If you want to be able to select images by which user uploaded them, then you’ll have to record which images belong to which users. That means another row in your DB that stores the user’s id/name/whatever.

    ~~~~~~~~~~~~~~~~~

    On another topic, checking file extensions to determine file type is not a good idea. Just because a file has a .png extension, for example, doesn’t mean that it’s a png image (or an image at all). Same thing goes for $_FILES[...]["type"]: it’s not checked in any way and can be forged, or simply incorrect. You would be better off using the FileInfo class -which actually examines the file- to validate the mime-type.

    $allowed_mime_types = array(
        "image/gif",
        "image/jpeg",
        "image/jpg",
        "image/x-png",
        "image/png"
    );
    
    $fileinfo = new finfo( FILEINFO_MIME );
    $mime_type = $fileinfo->file( $_FILES["file"]["tmp_name"] );
    if( in_array( $mime_type,$allowed_mime_types ) ){
        // good
    }
    else{
        // not
    }
    
Viewing 15 posts - 1 through 15 (of 36 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".