PHP - multiple file upload - php

I am building a simple CMS for a client. They need the ability to manage employee profiles.
Each profile needs an image.
Currently I have a form for adding employees, and associating an uploaded image with a single record is easy.
Below the record add form, I have a list of existing records (and a thumbnail of the image). These records are printed between form tags, I've got a checkbox next to each record (marking it will delete it when the form submits).
I want to use this form to also UPDATE records; deletions occur first, then $_POST data is parsed and records updated.
When a record has no image associated with it, instead of a thumbnail, a file input tag is printed. Because there is a variable number of records, the file tags are all named image[] so I can easily loop through them.
Question: how do I correlate $_FILES data with $_POST data? Do I have to name each file input to image_<?=$record_id?> to determine which record the file belongs to?

Your solution looks good, i would add record id as index, for ex:
image[<?=$record_id?>];
You can then correlate by array index

instead of
<input name="image[]">
use assocative array, for ex.:
<input name="image[id_<?=$record_id?>]">

Using the default fieldname[] notation, you can't control what indexes PHP assigns to the server-side representations in POST/GET/REQUEST/FILES. PHP'll just add them sequentially and if there's a gap in your form, it'll be gone once the data hits the server.
You can, hoever, FORCE indexes, so that fieldname[7] and newimage[7] all relate to the same fieldset in your form.

You will need to name each file input, and access it via $FILES['unique_name'].

Related

How do I upload images in php after I have already saved data from the form1 and images are uploaded in form2

I have a html page, with a form and image upload,
I want to update the database with the images once uploaded, the two form work well individually, but I need to link the form-data to the uploaded images in the database.
kindly help with logic, I am considering using sessions
You can use session as you mentioned or you can updated the relevant record with the next form details.
Say for example, once first form is filled and submited by the user, add those details in database and get the ID of that record.
When the second form is submited by the user, update the same record which you have already added for form 1.
So in database you will have only one record for both the forms with all relavent detials.
You could save your images in the session if you don't want to save them in the database (which, by the way, might not be a good idea), this avoids putting stuff in the database that is not yet finally processed (as I understand: this happens when the other form is submitted?)
You could put them in a table for "temporary images" which are not yet processed finally and just keep track of the IDs through the session

Solution to form insertion dilemma

I'm building a virtual tour application and have hit a bit of a stumbling block during the setup process. Currently, the administrator is the one responsible for determining which stop belongs to which panorama as well as the choices for the next move. In order to complete this, I need to be able to insert the photo name into the database along with the other information of the current stop.
My form solution was to add the photo names of a certain directory into an array, count the number of images in the folder, and create the corresponding number of mini-forms on the page. Each form has its own save button that works via ajax, so all information is updated as the user works through the stops.
My issue has to do with adding the photo name via the submit process. The photo itself is not a user-defined field in the form, and I'm using POST to pass the variables to the insert function. Is there a way to include the filename in the $_POST array just before submission?
what you have to do its use a hidden input with the filename in the value attribute.
<input type="hidden" name="filename" value="the filename" />
also, you can add the filename as a parameter in the ajax call, but i think that the input its cleaner.

How to retrieve HTML table data in PHP?

I'm creating a dynamic table where a user can add records(rows) and enter data. Table cells consists listboxes, text input, file input fields. When a user submits this data I'm getting only the last record(row) data through POST variable in PHP file. Even I tried to get the data using a hidden variable but no use. Any help is appreciated.
You need to make sure the names of your inputs are unique. In your situation where you have more than one set of inputs that are the same I would use HTML arrays.
<input name="my_input[]" />
The PHP manual has a set of examples where this technique is put into action.

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.

Unchecked checkboxes and hidden input problem

We have an edit form:we populate it with a variable number of checkboxes (it all depends,let's say, on how many pictures we have on the article we're about to edit).
All the checkboxes are per default "checked" and if nothing happens the data associated(our pictures in this case) remains untouched.
Altough if we uncheck one we actually want to not keep the picture in our edited post and delete it from our database.
I know unchecked checkboxes dont get Posted,and that I have one work-around:
place right before the checkbox an hidden field with the same name..in this way with the checkbox unchecked can still post the hidden input.
but i have this situation:
/*i'm querying the database to load all the article's data and I check if */
/* there's any picture so I build, with a while loop, an associated checkbox*/
<input type="checkbox" name="picture[]" value="$row['pic_id]"/>$row['pic_name']
When I submit our edit form I check if there's any data inside our array picture and if positive i want to delete it from database.
Now if I simply add an hidden field at the top of each checkbox (so same name and same value..cause it's the value i need to use in the next query) I fall into the problem:
how to distinguish if the posted data comes from the actual checkbox (if eventually checked) or from the hidden input??
thanks again
Luca
Why don't you check it on the server side?
You know what was the original list, you have your new list. Those that are not in the new list, but were in the original list, should be deleted.
So you don't need to add any javascript hacks, or hidden inputs.
On the server side you can use the following approach:
$array_of_ids_to_delete=array_diff($array_with_all_ids, $posted_ids);
The $array_with_all_ids must be already available, because I guess you use it to output all the checkboxes. $posted_ids represents your picture[] array.
After that you only have to run a foreach on $array_of_ids_to_delete and get rid of those images.

Categories