Uploading image with form to related table - php

I can get my head around the best way to sort this problem.
I have a form that creates a new record in the database (using php and MySQL) this is all fine, but as part of this form I want the user to be able to upload some files. These files would be stored in another table within the DB (allowing multiple entries) and linking back to the original record.
I could upload the images on the next page, after the main record has been created, but really want this nice and simple, all on one form.
Another issue is that if the images are loaded into a temp place, before form submission, I don't want this images staying there forever if the form is left, or cancelled.
I am using Fine uploader to handle the file management, and php to save and place.

Why don't you use a temporary table / directory with a peridodic check-and-delete-old script?
Create a temp table with (id, identifier_key, data, timestamp), and delete old records from it periodiaccly using a cron script (or from your main web script, using a probability, like php's default session handler works - once in a 1000 request or something similar.)

Related

Need to update data from my database to a php variable

i'm facing an issue that i'm unable to solve. I've changed my coding approach 3 times in a row. It's basically like this:
A user uploads images, these images get uploaded to a local folder + their names in a database where i then fetch them and load them.
A user may edit his images, I've done this through checkbox selection and a button called "Delete selected images". When the button is pressed, an post request through ajax is sent and the new images list is updated into the database and in the folder too without refreshing the page.
My issue is here: Say i had in the database these images: 0.png,1.png,2.png and i deleted the third image 2.png. The database field will be updated to : 0.png,1.png but then if i delete one more image let's say the first image, the database field should now be: 1.png but instead it becomes 1.png, 2.png,this is happening because the variable containing the images is not being updated from the database after every delete. Let's say $row['images'] was fetched from the database but it is not getting updated.
I need help on how to update my php variables from my database after sending the post request with ajax.

Functions and parameters - is there a better method?

I'm looking at neatening my file structures and code. At the moment I'm making an image gallery, it has a management page which allows the user to upload, edit and delete images/information.
To upload an image, the user would select their files, sumbit the form and arrive at uploaded.php.
To delete an image, the user would click a link taking them to delete.php?id=IMAGEID.
To edit an image, the user would be taken to a page with an editor. Upon making any revisions they'd then be taken to edit.php?id=IMAGEID.
Each page runs either a function. I feel it's unecessary to have three different pages to run three different functions. Is there any way I could neaten this process?
Well, it just depends on how you like to manage your files and how you are using your IDE when you've to edit something.
If you're working alone, it's clearly up to you. If you know someone will, maybe, have to rework your code someday, having three files could be a plus, somehow.
I guess you're not concerned about SEO for these pages, but just in case the problem appears again, it could be better to have actions in parameters, rather than in files name.
/manage.php?edit or /manage.php?delete or /manage.php?upload
But, if your code is really really big, using three files could still result in a lower use of memory (Well, very slight improvement, but still)
Exactly you don't need 3 pages for each function
1)Delete ,instead of redirection the user to a different page and delete it ,you can add an image button which delete icon and when user click on it ,it call a specific function in your php and delete it then refresh your page.
2)Edit and Insert : these are 1 page ,like make your insert page if it doesnt have a query string with the Image ID then it's insert function ,else it contains and ID then load the content of this Image inside your inputs and while saving check if there is ID then update else insert new record
So,you moved from 3 pages to 1 page only.Hope this helped you.

Keeping tmp uploaded file

I'm using pluploader with Laravel, and on my administrative the way it works when creating a new article is doing the following steps:
Type the details required to create new article
Select the photos to upload for the article
When Create button is clicked, the following actions are done:
Save the upload details for each photo in a session array. That means that if 3 files are to be uploaded, each upload has an individual POST action
Once the photos have been saved in that session array, it creates the record in the database for the article
It sends the id of the article created to a function which should get the photos and move them to their folder, and add the photo record in the database
Now, the problem is that with each POST the tmp_filename, the temporary file, is deleted so when it tries to actually move the photo... there is no photo to move.
Am I right, and if so, how can I work around that issue? Any way of retaining the tmp deletion until X function has ended?
Am I right,
Yes you are right. PHP deletes the uploaded tempfile after the request finished. This is also clearly documented in the PHP manual:
The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed.
You find that information here: POST method uploadsDocs.
and if so, how can I work around that issue?
Knowing this does suggest that you should keep a copy of or rename the file if you want to keep it.
Any way of retaining the tmp deletion until X function has ended?
Well as written, the deletion will kick in when the request finished. So in PHP normally all functions are executed before the request finishes, so even the X function if you call it in the same request.
If you don't call it in the same request, you need to introduce session management and copy or rename the tempfile before it is automatically deleted. As it is common with any other operation in PHP that should be done over multiple requests. See SessionsDocs.
See as well:
php - multi-step form with file upload
The easiest thing you can do is simply move the file somewhere else after it is uploaded.
However, I suggest you do this instead:
When a photo is uploaded, create the photo record right away and send back a record id to the browser.
Have the browser insert an input field with a reference to the photo id (e.g., <input type="hidden" name="photos[]" value="1234">.
When the document form is saved, associate the document with those photo ids.
This way you don't even need a session.

Showing Users Previous Uploads to Choose from

Not really sure where to start on this one and a day of google searches hasn't helped much.
I am working on a submission form where my logged in users can submit data in form fields and attach a number of files. These files should be available to the users across many submissions so as not to clog up my database with duplicate files and for user convenience not having to reupload files many times. The files will be mostly PDFs and word docs.
My question is, what is the most elegant solution to letting my user select a previously submitted file for their current submission and to "attach" or associate that file with multiple submissions? Some drop down box that shows previous uploads seems best and if you agree any code snippets would be super helpful.
I've decided to store the document files as blobs in their own table. I'm running php and MySQL on Linux with godaddy.
Thanks for advice on where to look for code samples in advance. Just haven't been able to find what I'm looking for. Is JavaScript necessary here?
A better way to handle file uploads is to simply store the file-name (which should be generated as unique) in the database and store the actual file in a directory. That way, you won't have this issue of storing it in a BLOB. Is there any reason you are doing that over this method?
Now, if you output a SELECT form element with a list of filenames stored in the database, a user can simply select one and all you need to do is store that filename in the new row. Then, all rows that point to that file name will point to the same file, with no need to upload it.
If you want to allow multiple file uploads or "attachments" in this case, then you are going to need to create a one-to-many relationship in the database. Each submission row will have many rows in another table that point to it, all referencing the corresponding files uploaded. Such that any submission can have zero, 1, 2, or more files attached. If they select files that already exist, great, just copy the username as stated above. IF its a new file, then you should upload that file and add its name to the database.
Javascript isn't required but it would be nice - to add interactivity to your form. If you want to allow for a user to click say, "Add New File", and have a new file upload form element appear.
Start with a a form. Add a select with the list of files associated with that user. Add another form element that allows for file upload. Have all of your other stuff as normal.
On page view, populate the select with the filenames and put the row IDs for that file in the <option value="file_id_xxx">....
In your server script, detect if a file was uploaded; if so, complete the process of storing the file and set the file reference as the associated file for the submission; if not, check that a file was selected from the dropdown and if so, save the reference from the select with previous uploaded files with the submission instead.
Javascript may help in disabling the select or file upload input, depending. But it's not necessary.

Proper way to handle uploads using AJAX form

I'm using Valum's AJAX uploader, which is quite nice. I have a form that lets you fill out some info and optionally attach files.
I have a hidden input on the form that has a randomly generated "token" (5 character alnum). The uploads are sent to a tmp folder and the info about those files (name, dir, token) are kept in a uploads_tmp table in a database.
Then, when the user successfully submits the form, those files are moved to a more permanent location and the rows from the uploads_tmp are moved to the uploads table.
If the user submits and there are errors with the form, my script knows there are uploads from a previous attempt via the token. So there's no need to re-upload files.
Is this the right way of doing it or am I going about it all wrong? I'm using PHP (CodeIgniter to be exact).
There isn't any one correct way of doing something like this. Your method seems like a good intuitive one, but really it's down to whatever works for you and your situation.

Categories