Jump to content

Using a PHP file for an image src.


JasonLewis

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.