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.
Related
I am developing a chat application for iOS.
In this app, the users can set up an image as their profile picture.
So my question is, how can i be able to store images in mysql ?
I have seen that many people say, just store the link to the image(on device) in mysql, but how will the images be available on different iOS Devices, from a database right ?
I have also tried using BLOB, but when the table rows are displayed(json encoded), the value for BLOB field comes out to be NULL.
Please answer in brief.
Thanks,
This is fairly simple :) The physical image will be stored on a server and you will store in your DB only the image name, or the relative path to the image, or however you want it. So, if you like, you will have to store in your DB a "pointer" to that image.
So:
- Image in folder on the server
- In DB -> path/to/file or file_name.format OR if you know you path, and you know your format just file_name
Hope this helps! :D
May be late But I just found way to do that.
Way 1: Get image from user and rename with the id (Pref. Primary key) and in Your public_html create folder for images. Set proper permissions for that folder. and save image in that folder and the url of that image will be stored DB column . Like
https://www.example.com/images/user1.png do in that style.
Way 2: use other things to rename that image like user's email,username etc
way 3: In above 2 cases it may happen that other users,hackers may try to download images by using IDs,emails etc.
That's why , another way you can do is you can generate a hash for profile and check if the hash already exists in column if exits then generate another one. Likely I don't think there is possibility of generating similar hashes.but you may check to avoid error in future. and now rename image using that generated hash.
You may also reduce size of that link column using only storing generated hash and in your app declare some variables and achieve
https://www.example.com/images/generated_hash.png
here the url will be same in all columns excluding that hash key.
you may only store that hashes.
Hope it will help
Reference from : https://www.techupdates.live/how-to-save-profile-picture-in-php-in-5-simple-steps/
What is the best technique to save images in the mysql database.Should I save images as blob data or save it in directories.The images will be showed every time the user visits his or her profile.
-Thanks in advance.
The best technique would be to save them in the file-system and save their paths in the database.
The database is meant for data, the file-system for files.
The technique I used in the past to make sure there are no duplicates was to hash the contents of the file and save it as the result, so I get something like:
42efb15825666918118ba72128195246dbae4976.jpg
The actual name is saved in the database. This was, the chance of having duplicates is negligible.
The best technique is what Truth said, in addition, to guarantee that your images will have a unique name, use the current timestamp to rename them in your directory.
I want to let my users upload an avatar on their profile. My first idea was to name the avatar file like this: [user_id].jpg. So even if a user updates its avatar, it keeps the same name.
The problem with that is that if I use caching on the server (or even if it's used on the client) the new avatar won't show up.
My new solution is to name the file like this:
[user_id]_[random_number].jpg
and store the random number in the Users table. How would you generate this number in the most efficient way? Or maybe there is a better solution?
You should be able to invalidate the cache when the user uploads a new avatar.
If this is not possible you could just store it as [uid]_[YYYYMMDDhhmmss].jpg or something. No need to generate anything random...
I would do something like:
$avatarName = $userId . uniqid();
// add extension if needed, store it
It will be fast and do what you want. uniqid()
EDIT
As suggested by other users, you should drop the userId from the image name. Having a public userId may lead to problems in the future.
Also, uniqid() alone should work.
$avatarName = uniqid();
// add extension if needed, store it
Have you thought about configuring ETags in your .htaccess?
See:
http://developer.yahoo.com/blogs/ydn/posts/2007/07/high_performanc_11/
Though you can change filenames, you will need to manage the cleanup and pointing operations (remove/rename the old file, tell your app the new file). If you are happy to do this, you can simply append the users id with the unix timestamp at the point of upload, its unlikely they will be able to upload the same file to the same second. If you want to make it even more unique, append a random number/uniquid.
With a random number there is a miniscule (negligible) chance of a collision, but why not start at 1 and just increase the number each time since you are storing this number.
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 want users to be able to upload their CV in PDF, .txt, .doc, .docx; and allow potential employers to download the file.
What data type should I use? In MS MSQL, I would use varbinary(max), right? But since I'm new to MySQL I'm a bit confused. :)
You have to use the BLOB type
You should use varchar holding a path to the file. File itself should not be stored in database.
no one should mention the whole don't store files in a db to flickr. :)
Storing files, even large ones, in the DB is a question about your hardware more than a programming practice. if your server (or server farm) can handle the business there's no real disadvantage to it, and there are advantages. the principal one being that files in a db aren't "stuck" to the server. files go wherever the database goes, and are replicated as required.
as with everything, no one answer is correct.. you have to make your best decision based on your actual project requirements and future plans.
Its usually a bad idea to store binary documents like DOC in MySQL. I would just upload them to the filesystem or convert to plaintext before saving to MySQL as BLOB or varchar.
I agree with most of them above. Is is generally not a good idea to store files in the database unlike Facebook which stores thumbnails in the database.
At this stage both options above has its pros and cons. the questions is,
does the size of the file increase when stored as a blob and
does the performance suffer,
relative to storing and downloading it from a server.
filename.php using form post method in this file
Upload File<?php echo $upload_file ?> <input type="file" name="uploaded_file" id="uploaded_file" value="">
Get the posted file in php
$file = $_FILES['uploaded_file']; //receive the posted file
$name = $file['name'];
$path = "C:/wamp/www/mantis/uploads/" . basename($name); // 5
if (move_uploaded_file($file['tmp_name'], $path)) {
} else {
}
**mysql query**
insert into tablename(upload_file)values('".$name."');