Storing 4000 image files per directory - php

So I want to store about 4k images in one directory, like this: uploads/uniqid()./file.jpg and uploads/uniqid()./thumb/file.jpg
Now, the problem is the way I want to do it, I don't thinks is the best way.
I want to have a table, where I store the active directory name that has less then 4k images in it.
So when a user uploads an image, I will go in that directory count the files, and if it'S less the 4k I will upload the image there, if the are 4000 images in the directory I will create a new directory name using uniqid() and put the file in the new directory, I will also update the table with the new active directory name.
I think this solution is ok, except that I will have to count the files each time a user uploads an image, what do you think guys?

An alternative to counting the files would be to add a counter (ie: autoincrement) field to a table, and insert a row in the table when a user uploads a file. You could then use mysql_insert_id() to get the id for the row you just inserted.
Then simply add the following to see if you need to create a new directory:
if((mysql_insert_id() % 4000) === 0) {
//create new dir name
//insert new dir name into table as active dirname
//add image to new folder
}
else {
//add image to active directory
}

Related

how to mass Upload user photos to mysql database

I'm working on a user management system that uses PHP and mysql.(XAMPP)
it has around 1000+ user records in A table called "users" with "id" as primary key.
one of the columns is "avatar"
I have a folder with 1000+ jpg images. all user images are named by their ids. for example if a user named Jon with id=222 his photo will be 222.jpg
what I want to do is upload all photos to their users using the image name to be put to the correct user id in the table
what's the best approach I can take?
hope its clear and thanks in advance
I have some steps that you might do :
Upload file as usual, but in this step you must include user id as parameter
Query user by id
Rename file before save image, example :
$user->id . time() . '.' . $file->getClientExtension();
Upload it to folder
Save to your table

Removing data held in a table row and assigning it to a PHP variable

I have a table called user_photos which has the following structure:
id
img_url
uploaded_on
username
I also have a table called users, which has a column called profile_pic.
I have a PHP script which is called change_dp.php. The basic idea is that a user can upload photos and then on hover, they have a button called Make display Picture which when clicked, calls change_dp.php.
However, photos which users upload are stored in the user_photos table and the actual photo location is stored in this format (img_url):
user_data/user_photos/ronda2.jpg
user_data folder > user_photos > and then the file name.
However, in my users table, which is where profile_pic data is held, images are stored in this format in the profile_pic column:
ronda_rousey.jpg
Issue: The issue is that I need to execute an UPDATE query in order to allow a user to change their profile picture (What I need is to get the file name, and just the file name from user_photos and SET that to the profile_pic in the users table). But since the images are stored in different folders, I also need a way to copy the image from user_photos to profile_pics when the Make Display Picture button is clicked.
How I think I should tackle this issue:
My thinking was to get the data from user_photos, i.e. get the whole img_url - user_data/user_photos/ronda2.jpg - Obtain just the file name from this column (ronda2.jpg) and somehow find a way to copy that image to the profile_pics folder and store the file name (which would be assigned to a PHP var) in the in the profile_pic column in table users.
But I don't know how to remove the user_data/user_photos/ from before a file name, and if this is even the best way to go about this issue.
Summary:
profile pictures are found in user_data folder > profile_pics > file name.
user uploaded photos are found in user_data > user_photos > and then the whole path to the image followed by image name.
If you just want the filename from a fullpath then something like this will work
$file = "user_data/user_photos/ronda2.jpg";
$arrPathInfo = pathinfo($file);
echo $arrPathInfo['basename'];

Filtering the images from source folder to another folder

I have one folder named SOURCE. In that folder around 10,000 images are there. The folder containing images of various category. For a particular category the image names are stored in a table. Now i want to copy all the images which belongs to particular category from that folder and then copy all those images into another folder.
Say there is a table like this,
id imagename
1 abc_1
2 abc_2
3 abc_3
4 abc_4
5 abc_5
All those images are in the same source folder. I need to run a query to filter this category images and copy all those to another folder. In that particular category i have around 1500 Images. I am working in PHP & MYSQL Environment. How to do this? Hope my question is clear.
Thanks in Advance !!
To clarify after reading all the comments, this is what I understand: You would like to move specific image files on the file system to another folder, filtering those files by the imageNames in the database.
Retrieve the imageNames from the category table that you need (select imageNames from myCategory) and store the result set in a collection.
Iterate over the collection
using the known directory structure match the file name and copy it over to the new directory. (take a look at http://php.net/manual/en/function.copy.php)
For copying from one table value to another table
insert into table2 values (select imagename from table1)
or
insert into tablename2 (column names)
values (select * from tablename1 where... );

How to generate image path by image ID

before uploading an image, system has to know uploading path.
Image path is generated by image ID.
for example: image 123456 has this path images/123/456
Of course, image ID will be = the last inserted image ID + 1
How to get the ID of the last uploaded image (data is stored in database)
I could do
select max(image_id) from images
or
select image_id FROM images order by image_id desc limit 1
but them seems to be a lot of work every time some user wants to upload an image.
Is there some simpler solution to get last ID
You need to do it in a few steps.
user uploads the image
you insert a skeleton record in the database, so you can get this new record's ID
process/move the uploaded file, using this new id
update the skeleton record to 'activate' it
You cannot do this BEFORE the upload, because PHP does not start executing until AFTER the upload has been completed.
For maximum safety, you do the database operations in a transaction. If there's any problems with the upload (wrong file type, upload failed, can't process file, etc...) you simply roll back the transaction and everything's undone.

Generating photo album using php and mysql

i have a mysql database storing uploaded photos , i am trying to generate a photo album using php , assuming i have 20 photos i need to put every 5 images in one row then start a new row with the 6th image .
i succesfully did it but i also need to attach the image id from the mysql database to the image displayed in the album so when i click the image the image id should be forwarded along with the url so i can allow users to comment to the picture and store the comments in database along wih the image id , how can i do it please ?
u need 2 scripts: to send list of images and to send the image data
i need to put every 5 images in one row then start a new row with the 6th image
u can use something like
for($i=0;$i<count($images);$i++)
{
echo '<img src="/myLinkTogetImagedata?id='.$i.'"/>';
if($i%5==0)echo '<br/>';
}
to send image data u need to send mime type in header
http://php.net/manual/en/function.header.php
and data from DB
think you can save image path in db
yeh, saving filename match better then saving image data in DB
when you generate album you must generate link with get
for example you have address kind /showimage.php?id=21
and then in show image you can give comment and other things
.ps : i think you can save image path in db and then select 10 last image ...

Categories