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.
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 should begin by adding that i'm not very familiar with SQL and database structures, i know the basics but don't think that's more than that. I am trying to build a functionality which is supposed to add an article with unknown amount of images. Each article will consist of 4 parts: title, image or multiple images, description and price. The confusing part here is that one article may have more than 1 image, as so far i've only known how to upload single image at a time.
How do i upload multiple images into database at a time?
As for the database i was thinking of going for the following structure: 2 tables, 1st - 'articles' and 2nd - 'images'. the idea was to first upload the image into 'images' with random 16 character hash name and then post in 'articles' all article fields with a hash reference on the end of the row.
Is this a valid solution or are there any other, more efficient ways?
You will want to place a reference to the article that the images belong to in the images table. Ensure they are the same data type. The article_id will need to be unique (primary key)
When it comes time to retrieve the data in your app you can simply pull the images out that correspond to that article.
Article
--------------------
article_id
article_title
article_descr
article_price
Image
--------------------
image_id
image_article (foreign key links to article_id)
image_path
...
You can get creative and use a image_seq_no to indicate what sequence the image would appear in the article or some reference to paragraph or location.
I have a photo upload script. In general everything is similar to general concepts. Also it is similar to Facebook's profile image logic. Differently I set user's profile picture by latest uploaded image. More clearly;
Steps:
user uploads profile picture
he/she can see the picture in some criteria comes from Mysql : ORDER BY add_date DESC so newest picture can fetched by php
Also I have a delete image button. If I use Mysql's DELETE keyword because of ORDER BY DESC newest picture is changing.
How can I set default picture after I click delete image button
as you can see from picture user 5 has added 3 different images in three different time.
mysql query is : SELECT path FROM profile_image WHERE user_id = ".$_SESSION['SES_USER_ID']." ORDER BY add_date DESC;" showing picture to user and for fetching. earliest date is user's active profile image. (June 28 )
if I delete June 28, June 27's photo will be active profile image, not the default profile image.
How can I set default image?
Have another table with fields userid, isset - ( with default value of 1 for all users)
If user clicks the remove profile pic( wishes to have the default pic) , change the value in isset to 0 for the respective user.
Check this table everytime before fetching the latest image from the profile pics table.
Alternatively you can have the isset field in the profile pics table itself. This would require you to change the isset value of every occurrence of the user's profile pic if he wishes to have the default pic.
I have a table in which I storing files uploaded by users and a php page which is showing all the files uploaded by a particular user. There is a download link when user clicks on that link I am passing the file id to my download.php page then in download.php page I am retrieving file data using that id. I want to generate a random key each time a user click a download link for a particular file and using this key and file id I want to retrieve file data from database.
How can I do it.
Create a second table where you store your random keys and link them to the files you want to have downloaded.
Example:
|-------|-------------------------|
| key | file |
|-------|-------------------------|
| jfdhj | /downloads/somefile.txt |
|-------|-------------------------|
Of course, you could use the ID of the file in the file column, and join it. The reason to do this, is that you can create several keys for one file.
Also: why not just use the ID from the database?
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.