- This topic is empty.
-
AuthorPosts
-
February 18, 2014 at 8:50 am #163221
MBM
ParticipantI 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"];
February 18, 2014 at 12:53 pm #163249chrisburton
ParticipantYou would need a select statement.
February 18, 2014 at 1:11 pm #163253MBM
ParticipantI want to retrieve and display the image as soon as it has been uploaded not specific images.
February 18, 2014 at 1:26 pm #163256__
ParticipantI 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?
February 18, 2014 at 1:33 pm #163259MBM
ParticipantEvery 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 #163260chrisburton
ParticipantThere 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="" />';
February 18, 2014 at 1:51 pm #163261MBM
ParticipantThanks 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 #163262__
ParticipantEvery 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?
February 18, 2014 at 2:37 pm #163264MBM
ParticipantI’m developing a trading card generator. I want to :
- Capture data and an image in a form
- Retrieve that data so the user that uploaded it can edit/delete it
- 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 #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.
February 18, 2014 at 4:23 pm #163270MBM
ParticipantSo store the image in the same table as the other data?
February 18, 2014 at 4:34 pm #163272__
ParticipantThis 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.
February 18, 2014 at 4:50 pm #163274MBM
ParticipantA 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 #163278Alen
ParticipantI 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 #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 }
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.