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.
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.