简体   繁体   English

传递输入类型文件数组

[英]Passing input type file array

I tried my best researching on how to pass an input type file array to another php page. 我尽力研究如何将输入类型的文件数组传递到另一个php页面。 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 我将这段代码写在页面cityaddinfo.php上,该代码直接提交到页面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: cityadded.php代码:

    <?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 :) 提前致谢 :)

You have lot of errors, 你有很多错误

  1. $a= $_POST['f_name']; you can not access file field like this you should use $_FILES instead. 您无法访问此类文件字段,而应使用$_FILES

  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. 当您要使用表格上传文件时,您应该在表格属性中使用enctype="multipart/form-data" ,我也没有看到。 I think you need to learn some basics. 我认为您需要学习一些基础知识。

使用$ _FILES代替$ _POST来存储文件。

Add this to your form : 将此添加到您的表单:

enctype="multipart/form-data"

Example: 例:

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

you need to have enctype = "multipart/form-data" set in your form tag. 您需要在表单标签中设置enctype =“ multipart / form-data”。 You can access the file on your PHP script using $_FILES and not $_POST. 您可以使用$ _FILES而非$ _POST访问PHP脚本上的文件。

HTML: 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 在PHP页面上,您可以使用以下命令进行访问

 $_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. 编辑:我不明白为什么您要在HTML页的文件字段中具有其他属性。 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. 您需要使用索引http://php.net/manual/zh-cn/features.file-upload.multiple.php来访问它们,因为您要传递文件元素数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM