JasonLewis Posted June 26, 2010 Share Posted June 26, 2010 Here's a bit of history first: Recently finished an application that allows me to upload images and store them in a directory, it also stores the information of that file in a database. Database stores the location, name and gives it an ID (auto_increment). Okay, so what I'm doing now is allowing people to insert images into posts. Throwing a few ideas around on the best way to do this, as the application I designed allows people to move files around, and I don't want images in posts to break if an image is moved to a different directory (hence the storing of IDs). What I'm thinking of doing is when linking to images, instead of linking to the file directly, I link it like so: <img src="/path/to/functions.php?method=media&id=<IMG_ID_HERE>" alt="" /> So it takes the ID, searches the database, then from there determines the mime type and what not, then spits out the image. So really, my question is: Is this the most efficient way? Note that on a single page there could be from 3 to 30 images, all making a call to this function. Some other ideas I've come up with is when the script runs, load all the images in the database into an array, and make that array available to the functions.php file. Then, instead of running 30 MySQL queries, it just finds the image in the array. Again, there could only be 30 images on the page, but 130 in the database. And there could eventually be more. Regards. Quote Link to comment https://forums.phpfreaks.com/topic/205911-using-a-php-file-for-an-image-src/ Share on other sites More sharing options...
Alex Posted June 26, 2010 Share Posted June 26, 2010 Why don't you add a "post id" column to the media table to specify which post it was uploaded in. Then when you're fetching the posts you can use a simple join query to fetch all the media in that post as well. Quote Link to comment https://forums.phpfreaks.com/topic/205911-using-a-php-file-for-an-image-src/#findComment-1077539 Share on other sites More sharing options...
JasonLewis Posted June 26, 2010 Author Share Posted June 26, 2010 Media can be re-inserted into multiple posts. Quote Link to comment https://forums.phpfreaks.com/topic/205911-using-a-php-file-for-an-image-src/#findComment-1077557 Share on other sites More sharing options...
Alex Posted June 26, 2010 Share Posted June 26, 2010 You could create third table to link the posts and media tables multiple times. Such a table could contain id, post_id, and media_id. That way you could link media to multiple posts. Quote Link to comment https://forums.phpfreaks.com/topic/205911-using-a-php-file-for-an-image-src/#findComment-1077667 Share on other sites More sharing options...
JasonLewis Posted June 27, 2010 Author Share Posted June 27, 2010 You could create third table to link the posts and media tables multiple times. Such a table could contain id, post_id, and media_id. That way you could link media to multiple posts. Was thinking along the same lines as well. Possibly going to do it like that, and when the post is submitted and parsed, image tags that are from inserted media will be replaced with something like: [img=ID] Then when parsing the post, it'll just replace all those tags with their correct image tag. Quote Link to comment https://forums.phpfreaks.com/topic/205911-using-a-php-file-for-an-image-src/#findComment-1077758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.