0

I tried my best researching on how to pass an input type file array to another php page. But I get stuck everytime I post the value from one page to the other.

I have this code written on the page cityaddinfo.php which on submits directs to the page cityadded.php

    <form method="POST" action="cityadded.php?city=<?php echo $city?>">

        <tr>
            <td>
            <?php
                $quecityimage=mysql_query("SELECT * FROM `city_image` WHERE city_id=$city");
                echo "<b>city images</b><br>";

                while($cityimage=mysql_fetch_assoc($quecityimage))
                {
                    $pathcity=$cityimage['path'];
                    echo "<img src='$pathcity' width=\"400px\" height=\"200px\"/><br><br>";

                }
            ?>

            </td>
        </tr>
        <tr>            
             <td>
            <p class="clone">  File:
            <input type="file" name="f_name[]" size="10" style="font-family: arial; font-size: 22px;"/>
            <p>
            <a href="#" class="add" rel=".clone">Add More</a>
            </p>
            </td>
        </tr>

        <tr>
            <td>
            <input type="submit" value="submit" name="submit">  
            </td>
        </tr>

cityadded.php code:

    <?php
     echo $_GET['city'];
   if(isset($_POST['submit']))
   {
   $a= $_POST['f_name'];
     foreach($a as $img)
     {
       echo $img;
     }
   }
   ?>

The code echo's the name of only one input type file name. Even if I add more than one files. Can anybody help me out on how to post this input file array on other page? Thanks in advance :)

4 Answers 4

1

You have lot of errors,

  1. $a= $_POST['f_name']; you can not access file field like this you should use $_FILES instead.

  2. When you want to upload file using form you should have enctype="multipart/form-data" in form attribute I don't see that too. I think you need to learn some basics.

Sign up to request clarification or add additional context in comments.

2 Comments

I corrected my errors, and actually they were not errors. but instead I removed it purposely to check diff methods. Since using $_FILES instead of $_POST gives me an error of 'Undefined Index: f_name'
@IshqMehta you would get that error if enctype="mutlipart/form-data" is not part of the initial <form tag.
0

you need to have enctype = "multipart/form-data" set in your form tag. You can access the file on your PHP script using $_FILES and not $_POST.

HTML:

<input type="file" name="file_sent" size="10" style="font-family: arial; font-size: 22px;"/>

on PHP Page you can access the same using the following

 $_FILES["file_sent"]["name"] - the name of the uploaded file
    $_FILES["file_sent"]["type"] - the type of the uploaded file
    $_FILES["file_sent"]["size"] - the size in kilobytes of the uploaded file
    $_FILES["file_sent"]["tmp_name"] - the name of the temporary copy of the file stored on the server
    $_FILES["file_sent"]["error"] - the error code resulting from the file upload

EDIT: I dont see why you would want to have the other attributes on the file field in the HTML page. I dont think these attributes would work on a file field. Even if the they do, move them to a stylesheet.

Refer to this. http://php.net/manual/en/features.file-upload.multiple.php you need to access them using indexes since you are passing an array of file elements.

7 Comments

Rahul: the input type file is sent to other page on 'submit', I tried using enctype and also accessing the file using $_FILES. Name of my input type file is 'f_name'. On accessing the file using $_FILES i get an error of 'Undefined Index: f_name'
do you still have square brackets in your field name - f_name[]?? Remove them. <input type="file" name="f_name" /> would do
@RahulNanwani Do you see the add more in the code that clones the file input? Using the brackets turns it in to an array so that is possible.
Yes I am still using brackets in f_name[], Because I just dont have one input file to send. I would have some few files from 5-10 with same input file name to send. thats why I have kept the title of the question 'Passing input type file array'
ok then, I didn't know that. But then shouldn't it be accessed using an index then on the server side. like f_name[0], f_name[1]?
|
0

Use $_FILES instead of $_POST for files.

Comments

0

Add this to your form :

enctype="multipart/form-data"

Example:

<form method="POST" action="cityadded.php?city=<?php echo $city?>" enctype="multipart/form-data">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.