📜 ⬆️ ⬇️

jQuery File Upload. Upload and add images to the database

Good day to all!
In this, I think, small article I want to describe how I studied, fought and won jQuery File Upload.

Lyrical digression:
In creating the site, I needed the function of uploading images to the site, and a separate user had to upload pictures so that the picture would be saved and attached to this particular user.
In general, I stumbled on Google jQuery File Upload .
The site has documentation in which I apparently did not understand, but still decided to understand the scripts themselves.

1. And so. I downloaded the script , installed it on the server.
And immediately ran into the following problem:
After downloading the images, I update the page and the list of pictures that has already been downloaded is displayed again. For some, this may be necessary but not for me.
Began to look for how to fix this problem.
It will be decided in the file server / php / upload.class.php. Row approximately 640 + -5.
Need to fix
public function get($print_response = true) 

on
 public function get($print_response = false) 
.

One less problem.
Then I wondered how to add a picture to the MySQL database.
The decision was again in this file. (Upload.class.php)
approximately in the 500th line after the line:
 move_uploaded_file($uploaded_file, $file_path); 

')
I added my own script for adding photos to the database
 mysql_query("INSERT INTO img SET name='".$file->name."'"); 


Then I discovered that when loading a picture, the record in the database was added, but if you delete it immediately, then the picture is deleted and the record in the database is not. Again I began to study the code. In the same file, add about 715 lines after:

  unlink($file); 


command to delete the record in the database. In my case it is:

 mysql_query("DELETE FROM img WHERE name='".$file_name."'"); 


Well already good.
Then I zakhnulo like lightning. And why do I add pictures to the database just by name not attached to a specific author?
I began to search and think how can I bring in more information than

 mysql_query("INSERT INTO img SET name='".$file->name."'"); 


The solution was again found.
Open the main Index.html file (which we open to run the script)

and add to
 <form id="fileupload" action="//jquery-file-upload.appspot.com/" method="POST" enctype="multipart/form-data"> 


Conventionally, the ID of the user who loads the pictures.
something like this:

 <form id="fileupload" userId="1234567890" action="//jquery-file-upload.appspot.com/" method="POST" enctype="multipart/form-data"> 

I think you will think up how to solder him here in your own way.

Next, open the main.js file
22nd line

  url: 'server/php/index.php?' 


We change it as follows

 url: 'server/php/index.php?id='+$('#fileupload').attr('userId') 


Now when downloading a file, we will send a GET request with a user ID.
Now, once again, we are changing the approximately 500th line.
with

 mysql_query("INSERT INTO img SET name='".$file->name."'"); 


on

 mysql_query("INSERT INTO img SET name='".$file->name.$_GET['id']."'"); 


And voila. When uploading pictures by the user, what we get is:
1. The photo is uploaded to the server.
2. A record is added to the database with the name of the photo and the ID of the user who uploaded it.

I hope that someone helped!

Source: https://habr.com/ru/post/158167/


All Articles