jQuery File Upload. Upload and add pictures to the database

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

Lyrical digression:
In creating the site, I needed the function of uploading images to the site, and an individual user had to upload photos so that the photo was saved and was attached precisely for this user.
In general, googling I came across jQuery File Upload .
The site has documentation in which I apparently did not understand, but still decided to figure out the scripts themselves.

1. And so. I downloaded the script , installed it on the server.
And immediately ran into the following problem:
After loading the images, I refresh the page and the list of pictures that I have already downloaded is displayed again. For someone, maybe this is necessary, but not for me.
I began to look for how to fix this problem.
It will be solved in the file server / php / upload.class.php. Line approximately 640 + -5.
Need fix
public function get($print_response = true)

on the
public function get($print_response = false)
.

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


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


Then I found that when the picture was uploaded, the record in the database was added, but if it was deleted immediately, the picture is deleted and the record in the database is not. Again I began to study the code. in the same file you need to add about 715 lines after:

 unlink($file);


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

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


Well, already good.
Then I gasped like lightning. And why am I adding pictures to the database simply by name not attached to a particular author ?!
I began to search and think how can I bring more information than

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


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

and add to


Conditionally, the ID of the user who uploads the pictures.
approximately looks like this:


I think you’ll think of how to solder it here in your own way.

Next, open the main.js file the
22nd line

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


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 the user ID.
Now once again we change the approximately 500th line
with

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


on the

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


And voila. When the user uploads pictures, what we get is:
1. The photo is uploaded to the server.
2. An entry 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!

Also popular now: