Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Before reading: The first few paragraphs of this question are me explaining the context of the situation in detail. Please skip to "The Question" at the bottom if you would just like to answer the question.

I'm currently working on a feature for a website that allows users to upload and share professional documents. These include documents like .doc,.txt,.xlsx,.pdf,ect...

Before uploading the documents, the user will be able to rename them if they so choose to. This is meant to help other users find the file so that it will be renamed to something like "psychologyStatistics.docx" instead of something confusing like "Johns_File.docx".

I have form setup...

    <form name="uploadFiles" action="submitFiles.php">
        <input type="file" name="file_input" multiple>
        <input type="submit">
    </form>

Notice the multiple.
I also have a dynamic list of text fields that the user will use to rename the files. This is done using a bit of javascript but I'm not going to post it here because its rather confusing and not helpful to the problem.

Unfortunately, i cannot simply rename the files in javascript like so:

object.files[].name = newName; // cannot be done because .name is read-only

The issue: I need to move the array of files to PHP WITH another two dimensional array of user-input file names. PHP somehow needs to know what files are connected to what file names. Therefore i will connect them using PHP's $_FILES['file']['name'] with javascript's object.files[].name. I will do this by passing a two dimensional array with the object.files.name as field 1 and newNames as field 2.

The Question: How can i pass a two-dimensional string array in a form input field using JavaScript or other necessary tools? Something like <input type="text" name="names[][]" multiple> would work perfectly however this is obviously not possible. If a 2-dimensional array is 100% impossible, then a one-dimensional array might work too.

Thanks guys. If anything needs clarifying then go ahead and ask.

EDIT: Javascript is going to grab text fields from a separate form and add them into a new input field inside the primary upload form. The issue is actually getting them into the primary upload form in an organized and relatively clean manner. Therefore I was looking at doing a 2-dimensional array.

share|improve this question
3  
PHP automatically converts to arrays for you. its nothing inherent in the browser. If you make a form field like name="foo[bar]" in PHP you can get it as $_POST['foo']['bar']. In Javascript you're still setting a field name so you would set the field name the same way. Maybe that will point you in the right direction, as it stands I'm not clear on how the users are renaming files. Try showing some more code. –  Cfreak 2 days ago
    
Javascript will grab the new names in a separate field and then set the set them in a new field like the one you are suggesting. –  user3786717 2 days ago
    
Additional code would only complicate the problem because the entire form is very dynamic that allows users to remove elements and add elements ect and uses a lot of javascript. At the end of the day i'm just going to use JS to move the names from a separate field into this primary form field. –  user3786717 2 days ago
    
Are you suggesting that i'm going to have to add a new <input> field for every file? –  user3786717 2 days ago
    
Yes. I'm pretty sure you can't change the name of the file sent unless the user changes the name on their computer. You could add it dynamically though. –  Cfreak 2 days ago

1 Answer 1

up vote 2 down vote accepted

It appears that passing an array via a single input field is not possible.

However, multiple input fields with the same name will be interpreted by PHP as a single array. Therefore, it is necessary to use multiple input fields with the same name to pass an array from an HTML form to a PHP file.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.