understanding blobs and $_FILES - php

I've read many articles on stackoverflow about this particular question, but I haven't seen one that explains it like I need it to be explained:
So I'm just trying to get a better understanding of using blobs and the $_FILES to insert and retrieve image files from a MySQLI database.
So far, I know that blobs are used for images, that they can be tiny/regular/medium/and large depending on the size.
I also know that $_FILES is used to get user file input like $_POST can be used to grab a username from a form.
I eventually want to get to the point where I can store a user-uploaded images into a database, and then display those images on the users profile.
Can someone give me any information on as how to do this?

Related

Saving image information in database or not?

Lets say I'm building a image gallery using PHP, where users would be able to upload their photos.
Every user would have 1 folder on server side with all their images there.
Now lets say I need to provide information in browser. Users would be able to browse images and should see lots of information about them, like image size, image dimensions, even EXIF information etc.
I could do this in 2 ways:
Save all information about image into database when uploading image.
Use PHP functions to browse through folder, and get information from every image.
I have something like file manager class, that can do all manipulations with files on server side, like deleteDir, deleteFile, countItems, getFileSize, getDirSize.
And it would be easy to only write one more class that would inspect images, and then I could just upload images, and get their information right from the folders without a need for relation database.
And now the question you all been waiting for is: ... :)
What would be faster, first or second solution? Lets say that site gets loads of traffic.
What solution would be better if I want it to be fast, and not to stress server to much?
actually, I got this situation like yours, this is my solution:
Save all information about image into database when uploading image.
Why?
I tested 2 ways:
Using php to get the image info for 1000 times.
Getting image info from database for 1000 times.
And the result is :
Getting image info from database is faster and faster.
Last but not least:
What would you do if you want to do a image info analystics?
If you save all info in database ,you can easily get them and analyse them ,but if you using php to get the info? it's hard to image.
So, just save all information about image into database when uploading image.
Good luck.
storing it in the database once
reading the data from the database and store it in cache,
redoing things always costs especially if it happens all the time
Depending on the size of these images, you probably want to show thumbnails instead of the original when people are browsing, which means you need to generate them. I would generate the thumbnail on upload and grab all the file info. Then save the file info in the database and put the original and thumbnail in the file system. If you get a lot of traffic, throw memcache on there too.
Storing data in separate places has a way of creating maintenance headache. I would just serialize the metadata for images in each folder and dump it to a file there. If you use gzip compression on the file, retrieval and storage should be very fast.

Preventing duplicate enteries in database

I am using a form to upload files(images) to my server. How can I possibly prevent the same image from being uploaded twice?? I cannot possibly look wether the a image by same title exists as same images can have different titles and different images can have same title.
Any help is appreciated.
Create a hash like ZombieHunter suggested. Why? Because is easy and fast to search and check through a big table of hashes if the image already exists. Unfortunately all this hash metdods like md5 or md5_file work on existing files not on remote ones. So you will have to upload the file anyway. What you can do is then decide if you want to keep or not the file. If you are fetching the files from an online resource, maybe there are ways to detect from headers the file size and run a hash without downloading it, but this is a special case.
Also if you have other business logic attached to those images, with concepts like userHasImages or companyHasImages you can organize them in namespaces/folders/tags so you can speed the search even further.
In terms of database strictly speaking prevention of duplicate entries, use an unique index for the column that contains the hash.

What's the best way of cataloging large number of images?

I'm looking at setting a site up with a large number of searchable images, organised by keywords, tags etc. I'm planning to get the image information from the IPTC data. The images will be uploaded by FTP then added into a database. My question is this:
Is there any advantage to storing the IPTC data in the database record for each image or just getting it from the file as and when it's needed?
My gut feeling is that it would be more efficient and easier to work with to have it all in the database but I'd like to make sure before I start to build anything.
I'd certainly put the data into database in some form. You can then ensure that it is indexed and is much faster to search. To get maximum benefit, you would need to preprocess the data into a format that is easy for the database to search. EG keywords should be stored individualy or coded as a many to many relationship (picture_ref, keyword_ref pairs) between the picture and a central list of keywords.

How to add pictures into a blob field using php

I have an SQL DB that comes with a BLOB field that needs to be fill with pictures.
The pictures are in a folder.
How can I use PHP to import the pictures into the blob field automatically if the pictures have the name of the ID field?
edit:
I am not creating the database, just filling it.
The program which is using the database is a merchandise management system, which only allows to store a blob.
That way I have to get the pictures into the SQL and cannot only store the filepath.
Database is DBISAM.
So far I tried to to store the pictures using SQL commands, but cannot see how it would work, so that I hoped someone could help me solving this using PHP.
Best Kurt
I'm not sure if this is helpful, but can I assume that the point of storing the pictures within a BLOB field is simply to associate the image with a specific record in the database? If this is the case, it might be more appropriate to simply reference the filepath of the appropriate image in the DB, rather than trying to store the actual file within the DB. This is more common practice than storing actual resources in the DB, which would take up way too much space for an application of any size.
For example, if you're creating a "users" table and you want to associate a profile picture with an individual user, rather than trying to store a raw .jpg inside the database, instead store /path/to/image/from/webroot.jpg in a field called something like "profile_pic_path" for each user. Then you can reference this path whenever you want to use the image.
You can use
$data = file_get_contents($_FILES['photo']['tmp_name']);
to get your imagedata into a variable (assuming that the file is uploaded and its form element is called 'photo', otherwise change the path). The content of this variable can be stored within a blob field of your database.

What is the best way to keep track of images uploaded by an user using PHP

I am planning to do a photo album website, So each user may upload as many number of images. What is the best way to keep track of images for an individual user. What should be the server configuration to handle this part.
-Lokesh
Depending on the amount of images, you will probably want to store them on a static domain. Then, have a table in whatever database you are using to store the paths to each of the images for each user.
Well like many design topics there are lots of different ways to go about it. Two ways that come to mind right now are as follows.
you could simply have a directory created on the server for each user and then have the images each use uploads saved into that directory. Ofcourse you'd want to make sure they didn't over write any existing images with images of the same name. You could do this by warning them about conflicting names or by adding some sort of noce string (like a time stamp) to the end of of the file name. This is a pretty straight forward solution and means that you can login to your server and see all the images each user has uploaded right there for you to do anything you like with.
Another idea would be to save the images in a database. This can be done by serializing the images to a string and storing it in a database. This is nice becaues it means you don't have to worry about handling directories and duplicate file names. You will have to deserialize each image when you want to display it which will put your DB under load so for a very high traffic volume site this might not really be the way to go.
There are ofcourse combinations of these ideas and many others. It really comes down to working out which solution best fits your exact needs.

Categories