Currently i have system that's do resizing and creating as many images as needed and storing them on web servers file system. Also i make and entry in database of image basename in mysql.
For example i upload 5 images the basename would be 372FDSC and then it will add _0, _1 etc. to each image in filesystem so it makes image look like 372FDSC_0.jpeg and so forth.
So where is my problem? It's that i cannot delete or upload new images since i don't know image name in code.
I wonder is there is some better solution to save image names for article? Like mysql row that holds all names (372FDSC_0.jpgeg;372FDSC_01.jpeg etc.) So when i delete or upload new ones i know what filename i can use.
I hope this all makes some sense to anyone if not please say and i'll add required details.
You could use a timestamp at the end of the image name, so you're sure it's unique.
$new_name = $basename . "_" . time() . $extension;
You could use timestamp as stated above or you could also give your images descriptive names based on what the image contains instead of the 372FDSC naming.
Have 3 columns in your database table titled 'basename', 'no of images', 'article name'.
Now make a loop to get all the images.
eg if you have 5 images in your article 'abc'. Then you will use
for($i=0;$i<5;$i++)
$images[]=$basename.$imageno;
keeping in mind that $basename is the basename retrieved from database and same applies to $imageno.
You can use your article id field to rename images. For example, if your article id is 237, the images will be 237_1.jpg, 237_2.jpg etc.
In that case, you will not need to store image name in db also.
Related
I have posts system where users can upload images inside a post, I intend to build the images paths url and not store the uploaded images paths in database.
I will simply use unique Post ID to build the url path for post images like:
$path = "images/{$postID}/";
Then when rendering the post I will use
$images = glob($path);
When post is deleted I will simply:
unlink($path);
Why all implementations for php image uploads that I read about contains table to store images paths? Why not this method are there any disadvantages that I am not aware about?
I think that this one can go with a little discussion. Although the image location is recommended to be stored in the database, but it is not always the best way. If you according to your logic find some algorithm to uniquely name images based on blog id or something that would save you database latency cost.
For the same i would like to give example of a sample use case. You are either way fetching blog text from database. Now you can add another field to store the number of images that you have associated with the same. Now take the unique id of blog suppose uq1. If that blog has 6 images , you can name the images as
$path = $uq1.$imageindex
remember that you will also have to store the image in same path. You can always get fancy and add simple XOR encrypt. Although the use of XOR is mainly when you are also associating sensitive data to the same. This will save you some database latency.
Hope this helps
its a good idea doing it in this way.
unless you don't need duplicates of a lot of images
for example if you have image apple.jpg with size 500K,
and you have 2000 posts that should have apple image in them.
so you will save 2000 x 500k of apple image.
but if you have it in the database and you just select images from
something as gallery so you will have only 1 image for apple.jpg
but it linked for 2000 posts using the database table.
but if you dont care! about duplicates and data size, or you are sure that there is no any post have any image like the other! so its ok.
summary: saving all post images in folder with its id is the easy way
but it will cost you space ( duplicates and so on )
the other way, saving the image link in database and just link it with every post need it. its better.
I am reading a lot about uploading images with PHP. I have come to the conclusion that it's best to have a folder /images to keep the files. And have a db table that holds the path to the file and i'd also like to keep track of what it is an image of (in my case houses).
I would have 3 fields in the table:
id
unit
image
Does this sound like the proper way to handle this? I am unable to find any definitive article on how to do this.
Also, when it comes to uploads, are there any recommended articles on how to accomplish this in the manner I want?
Yes it is.
Although mysql lets you store images into records just as strings or numbers it is generally preferred to keep separated "records" from "files".
I'd suggest to avoid recording the path to the image.
Instead, as the image is associated with a record, give the image a name that links it to the record. Assuming your table have an unique, primary key, auto-increment, integer field named id each image will have filename id.jpg
After the upload you'll move the image from the php upload directory to the images directory renaming the file.
For information on how manage file uploads with php google for php upload files
I need to store images data in a database. I'm uploading images to a server and then, of course, I need to retrieve them from the database. I know that in the database I should only store just datas leaving the actual files to the filesystem so I'm trying to think which one may be the best way to save those datas.
What's bothering me the most is choosing a naming scheme. In the database I'll just have a filename column, where to store just the filename plus the extension. No path, so I can config it in my application. So: what shall I use for the filename? I'd like to keep a meaningful name, so it would be cool to keep the photo title, or at least a part of it. But to ensure uniqueness which one could be the best approach?
I know Codeigniter has a string helper, but I also read about using the php function uniqid. Then I can attach the id to the picture name.
What do you suggest?
Feel free to store the entire image in your database, as long as there won't be hundreds of thousands of them (or you just like paying for expensive SCSI drives :) ). If you're unsure, the file system is fine too.
For the image's unique id, the best approach is probably to just use the DB insert id (guaranteed unique, no threading/clashing/crying). So here's a good approach, assuming you want to keep the images in the file system:
create table images (
image_id int(20) primary key auto_increment, /* assumes mysql */
file_path varchar(2500) not null, /* the actual file name and full path */
file_label varchar(255), /** user's memorable name for the file */
description varchar(2500), /** user provided description of contents */
media_type varchar(100), /** something like image/jpeg; privided by php in upload */
);
Then you'll probably want to connect image_id with some sort of categorization or tags, a user_id, etc.
EDIT
Added for comments:
You can always build your links using the real file name if you like, even if you store them as simply the image id in your file system. You just need to utilize search friendly urls to do something like this (where 1234 is the image id and /images/ redirects to your php script):
mysite.com/images/db/1234/picture_of_a_cat.jpg)
Then you just redirect /images/db/ to your php script. Alternately, if you don't want to try rewriting them, you can just use $PATH_INFO.
For example, if you have mysite.com/images/render.php/1234/picture_of_cat.jpg:
<?php
$parts = explode("/",$PATH_INFO);
$image_id = $parts[3];
$image_from_db = null; //fetch the row from your dabatase here!
header("Content-type: ".$image_from_db['media_type']);
echo file_get_contents($image_from_db['file_path']);
Good luck! ;)
Well, I prefer that you use a hash in the name of the image, as can happen in case there are two images of the same name, and will generate a future problem.
Another tip, never want to stores the contents of a file in the database, the cost is much higher than just store the path to that file.
Storing images in a database is not a good idea, instead store the links to the images in the database.
For example;
create a table with the following fields
image_id //unique id of the image
image_type // type of image("png","jpg","gif".....)
image_link // path to folder where the image is located
upload date // date of upload of image(usefull for ordering in database queries)
You can add any other field you want
I have a form where an admin will upload three pictures with different dimensions to three different designated directories. now to make sure that i don't get into the problem of duplicate file names i implemented something like the php will compare the uploaded file name and it will check if that file name exist in the designated directory if yes then it will echo an error and stop the script execution.
Now one of my friend suggested me that it is very bad asking the admin to manually rename the picture file and asking them to take care of the file duplication problem. the solution he suggested was to rename the file automatically and then store it in the database and then direct it to directory.
I am confused about what combination should i give to the renamed file and also make sure it will remain unique file name to be more precise i would like you to understand my directory structure
as i said there will be three pictures files the admin will be uploading namely
a) Title Picture b) Brief Picture c)
Detail Picture
and all the three picture files will be moved to the different respective directory, like title picture goes to title directory and so on.
i am using to script below currently just to move and store the file name with path using varchar in the database.
$ns_pic_title_loc= $_FILES["ns_pic_title"]["tmp_name"];
$ns_pic_title_name = $_FILES["ns_pic_title"]["name"];
move_uploaded_file($ns_pic_title_loc, $ns_title_target.$ns_pic_title_name) or die(mysql_error());
that is just the sample code i havent included the validation function which i am using. i was thinking like i want to rename all the files like
a) In title directory the file should be stored as.
title_1.jpg
title_2.jpg
title_3.jpg
title_4.jpg
and so on
and the same way to rest of the pictures. how do i do that? what function do i use to achieve my target. and if this is not the good way to rename the file i would appreciate any suggestion followed to rename the file.
thanks in advance
Well, here's a possible solution:
Get uploaded filename from $_FILES["ns_pic_title"]["name"] and separate extension OR if we are only talking about image files get the image type with getimagesize($_FILES["ns_pic_title"]["tmp_name"]);
Check your database for the maximum id of the image records and make the the $file_name variable 'title_'.($max_id + 1)
At this point you should have $file_name and $file_extension so do move_uploaded_file($_FILES["ns_pic_title"]["tmp_name"], $ns_title_target.$file_name.'.'.$file_extension)
Hopefully this makes sense and helps.
There are a couple of good options with various pros and cons.
Use php's tempnam when moving the file, and store the path in your mysql database. tempnam generates a unique filename.
Use mysql to store the image content in a blob. This way you will access the image content via an id instead of a pathname.
Instead of having logic to figure out what the latest picture name is and calculate the next number increment, why not just use PHP's tempnam() function? It generates an unique name with a prefix of your choice (i.e., "title", "brief", "detail"). You could also simply prepend a timestamp to the file name -- if you don't have a whole lot of admins uploading pictures at the same time, that should handle most name conflicts.
Since your pictures are going to be sorted into title, brief and detail directories already, it's not really necessary to name each picture title_*, brief_*, and detail_*, right? If it's in the title directory, then it's obviously a title picture.
Also, you're going to be putting the file names in the database. Then elsewhere in the app, when you want to display a picture, I assume you are getting the correct file name from the database. So it isn't really important what the actual file name is as long as the application knows where to find it. If that's correct, it's not necessary to have a very friendly name, thus a tempnam() file name or a timestamp plus the original file name would be acceptable.
Because you are storing references into the DB, I would prefer to just md5 the datetime and use that for the filename and store the disk filename to the DB also. It doesn't matter what name it is written to disk with as long as you can point to it with the unique name into the DB.
I use this methodology, and in none of my testing does the disk name (md5 from the datetime) ever require multiple tries.
I'm working on an iPhone app that will upload images to a web server. I was wondering if anyone had any tips on how to generate unique names for each image file that gets uploaded.
I'm sure there are a million ways to do this, but if anyone has any suggestions I'd really appreciate it! Thanks.
you could create a GUID and save the image as that name ..
Thanks
The simplest solution (assuming you're storing these in a database) is to have an auto-increment field in the database, and use that to rename the file as it's uploaded. That way you'll end up with image00000001.jpg, image00000002.jpg, etc.
The simplest would be to convert the current time into a string and use that as a name. will always be unique :)
Or if you have a private key in your database, use it with a generic string to generate a unique name for each image.
You could use the unix timestamp. This would allow you to update a record with a new file while still keeping the same id, instead of having to create a new record each time a file is changed. Some like:
$uploadData = pathinfo($_FILES['file']['tmp_name']);
move_uploaded_file($_FILES['file']['tmp_name'], time() . '.' . $uploadData['extension']);
I'd recommend a lot more checking to ensure the file is what you are looking for, such as mime type/extension checking, max size, and ensure the file is an uploaded file using is_uploaded_file().
You may want to consider using the device id of the generating phone to insure uniqueness across the universe of iPhones.