Welcome Guest, Not a member yet? Register   Sign In
Storing an image in a database/table field
#1

[eluser]JPrieto[/eluser]
Hi, everyone -- me again

ok....im duplicating tutorial video #2

but was thinking of uploading and storing a jpg image
in a field within the entries table (I would call it photo)

how can this be done .. using the steps in tutorial video # 2
#2

[eluser]SpooF[/eluser]
It really can't be done using what you learned in the second video tutorial. There are a few ways you can go about doing this, one is to simple store a URL to an image file that is hosted remotely. The second way is to upload the image file to the server and store the path to the image file. The third way is to upload the file and store the binary data into the mysql database. Each of these options is a a little more complex than the one before it.

The first one is pretty simple, all your going to do is create a varchar or text field in the database and store the URL to the image. Then you can simple just take the stored value in the database and put it in your image tag.

The second one is basically the same as the first but your going to need to read up on the File Uploading Class. Once you upload the file you would just store the path to the file in the database.

The first option, which I would not really recommend doing, is to upload the file and read the binary data. Then store the file in a blob field in your database. To get access to the file you would then need to create a function that would read the binary data out of the database and recreate the image using headers.

I would suggest doing the first options for now.
#3

[eluser]JPrieto[/eluser]
wow -- thanks for taking time to explain it in such detail

i like option two - since i know how to upload and store in an image folder
and with your tip now i will be able to do it

now i just want to be able to (edit) upload a new image, at anytime, to replace the one uploaded

and...

when an entry is deleted, i want the image to also be deleted

this makes me happy -- i'm almost getting it
just need the bits of tech info described above

again, thanks
#4

[eluser]ray73864[/eluser]
deleting a file is pretty simple, what i would do is have 2 fields in the database (you don't need to, but it makes your system more flexible as it allows you to upload files to anywhere on the system).

then, when they delete the virtual link from the database, you first want to grab the physical location from the database and do an 'unlink(/path/to/the/file)' which if i recall returns a true/false as to whether it did it, if you do the unlinking first, then depending on if the unlink failed or succeeded you could then proceed to actually delete the record.

Reason i say that is you don't want to delete the record only to find the file couldn't be deleted, as that would leave dead files on your system.
#5

[eluser]Evil Wizard[/eluser]
the unlink will generate php warnings and notices if the file/path supplied to it doesn't exist or the permissions are wrong.
Code:
function deleteFile($strFile)
{
    $blSuccess = FALSE;
    clearstatcache();
    if (is_readable($strFile) && !is_dir($strFile)) {
        if(@unlink($strFile)) {
            $blSuccess = TRUE;
        }
    }
    return $blSuccess;
}
When you use the is_dir() and is_readable() functions the results are cached for faster future use which is why I use the clearstatcach(0 function to ensure that the checks are current. Prefixing the unlink() function call with "@" suppresses the warnings and notices should any be generated.
#6

[eluser]Natebot[/eluser]
JPrieto, you'll also want to read up on best practices on upload security. Be sure to run the uploaded image through the xss_clean() helper. And be mindful of what permissions and file ownership are given to the file. It's downright frightening, the business with file uploads.




Theme © iAndrew 2016 - Forum software by © MyBB