AVProgrammer
AVProgrammer

Reputation: 1350

PHP - multiple file upload

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?

Upvotes: 0

Views: 384

Answers (4)

George Cummins
George Cummins

Reputation: 28906

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

Upvotes: 0

Marc B
Marc B

Reputation: 360662

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.

Upvotes: 0

Aurimas Ličkus
Aurimas Ličkus

Reputation: 10074

Your solution looks good, i would add record id as index, for ex:

image[<?=$record_id?>];

You can then correlate by array index

Upvotes: 1

Peter
Peter

Reputation: 16923

instead of

<input name="image[]">

use assocative array, for ex.:

<input name="image[id_<?=$record_id?>]">

Upvotes: 0

Related Questions