Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End How To Display Stored Image Using Path?

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 36 total)
  • Author
    Posts
  • #163221
    MBM
    Participant

    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

    #163249
    chrisburton
    Participant

    You would need a select statement.

    #163253
    MBM
    Participant

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

    #163256
    __
    Participant

    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?

    #163259
    MBM
    Participant

    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="" />';
    
    #163260
    chrisburton
    Participant

    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="" />';
    
    #163261
    MBM
    Participant

    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.

    #163262
    __
    Participant

    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?

    #163264
    MBM
    Participant

    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.

    #163268
    __
    Participant

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

    #163270
    MBM
    Participant

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

    #163272
    __
    Participant

    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.

    #163274
    MBM
    Participant

    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.

    #163278
    Alen
    Participant

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

    Might be of some use. Did it while back.

    #163284
    __
    Participant

    … 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)
  • The forum ‘Back End’ is closed to new topics and replies.