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.
Related
I have a form for users to sign up. This form has a file input which will upload images in a directory in server by ajax instantly as user selects a file. There's also a button which will submit the form and saves user info in user table in database.
Each user can have multiple images. Ajax also save address of images in photos table when file uploads. this table has user_id column. but since the user_id will be generated when submit button is hit and user is not registered yet, I can't fill user_id column in photos table. The id of user is generated when whole form is submitted.
so should I update photos table when form submits by adding id of user to user_id column?
or I must save photos with ajax in temporary_photos table and then when submit is hit save them in main photos table and delete them from temporary table?
or is there another approach?
what if user uploads files, but closes the window without hitting submit button? how I find out the window is close and registration is canceled so i will delete images?
You are trying to solve a problem that you created. If user_id is a dependency for the photo to be saved, let the user be saved first.If you do it the other way , it you will over complicate the workflow.
So, i suggest you register the user first, save the user id in session (or log the user in) and then upload the photo.
If you must do it together, then let the ajax request register the user and then save the photo and save the user_id in a session variable. Next time you upload a photo you could get the user from session variable and save the photo.
function savePhoto(Request $input){
if(Session::has('user_id'){
$user = User::find(Session::get('user_id'));
}
else{
$user = User::create($input->all());
}
$user->photo->save($input->all());
}
You can try this way-
Since an user can have multiple images that means the relation is one to many between. So better if you use a images table.
Table: Images
Column(s): id, user_id, image, uploaded_at
Make user_id field is nullable and image column will save the name of uploaded images. Upon successful uploading and adding iamges record to database save last inserted ids in a session array. Once the user submit the form, create a record to user table, grab the last insert it, check if there are any data in session array, if found, loop through the array and select those images record by id and update user_id field. This will solve your problem.
You also asked for a way to delete images for which users didn't submit the form. To do this, you can run a cronjob/laravel task schedular. A script that will remove images and delete records for those images which were uploaded 5 mins (for example) ago and user_id is still null.
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'];
I don't really understand properly the concept behind uploading multiple files with preview. I checked DropzoneJS and it looks nice and I feel I can do a lot of stuff with it.
I have this example: I have a user (id: 801) that creates a new post (id: TBD since the post is not yet created). I have a table image_post which holds the images of the specific post, and an image table which holds the image details. Now, I have the Dropzone form which is supposed to upload images to a folder that I specify in the .php file that is implemented to deal with the ajax request coming from Dropzone. Let's say the user uploads 2 images and I store them into an temp folder. The user submits the creating of the new post, it receives an ID: 10001. The temporary files are on the disk (but should they be stored in the DB as well?) but don't see how to link the post to the images.
What are the exact steps that are required to be able to (after the files were uploaded with Dropzone ajax request) link those images to the actual post id?
Your system will need to create a database record for the post prior to them uploading images. Consider the following table structure, where an i prefix indicates an INT column, s a VARCHAR/TEXT column, and dt a DATETIME.
Table posts: iPostId | iUserId | iStatus | sComments | dtCreated
Table photos: iPhotoId | iPostId | dtUploaded
When a user begins to start a new post, create a record in the posts table with their user id, an empty comments, a datetime such as '0000-00-00 00:00:00', and a status of 0, and then add a hidden form input to the photo upload form so that the assigned post id is known to your photo upload script, e.g.:
<input type="hidden" name="iPostId" value="22" />
The photo upload script could create a new record in the photos table using the post id and then save the photo in a particular folder using the post id in the filename, for example if the photo_id for the record created was 2821 you would save the file as 2821.jpg in your chosen folder.
When the user finishes their post and clicks to save/submit, you could use an UPDATE query to modify the original record on the posts table, assigning their entered text to the sComments column, the current date and time to the dtCreated column, and changing the iStatus to 1.
On your pages where you display a list of posts simply modify your query to something like this to exclude those currently in a draft status (0): SELECT * FROM posts WHERE status=1;, and then when viewing the post you can run a query such as SELECT * FROM photos WHERE post_id=22; to get an array of the photos to be displayed.
Obviously this can be expanded upon but hopefully gives you a good starting point.
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
}
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 ...