doutu2017 2017-12-03 10:47
浏览 50
已采纳

生成多个输入类型文件并上传并存储到db

Hi... i am trying to upload the image from the number of i choose to generate and save the image name into database. For example, i choose to generate 3 input type "file", than the 3 input will automatic generated, so i can upload it into folder and save the name into database. i have done to other type of input such as "text". but still stuck at the uploading image part. The form is look like this (if i choose to generate 3 times): enter image description here This is the example of my form code:

<?php
if(isset($_POST['btn-gen-form']))
{
?>
<form method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="total" value="<?php echo $_POST["no_of_rec"]; ?>" 
/>
 <input type="hidden" name="vendorid" value="<?php echo $id;?>">
<table class='table table-bordered'>

<tr>
<th>##</th>
<th>Name</th>
<th>Description</th>
<th>Image</th> 
</tr>
<?php
for($i=1; $i<=$_POST["no_of_rec"]; $i++) 
{
    ?>
    <tr>
    <td><?php echo $i; ?></td>
    <td><input type="text" name="item_name<?php echo $i; ?>" 
placeholder="Name" class='form-control' required/></td>
    <td><input type="text" name="item_desc<?php echo $i; ?>" 
placeholder="description" class='form-control' required/></td>      
    <td><input type='file' name="file" id="file"  value=''></td>        
    </tr>
    <?php
}
?>
<tr>
<td colspan="6">    
<button type="submit" name="save_mul" class="btn btn-primary"><i 
class="glyphicon glyphicon-plus"></i> &nbsp; Insert all Records</button> 
<a href="additem.php" class="btn btn-large btn-success"> <i class="glyphicon 
glyphicon-fast-backward"></i> &nbsp; Back to index</a>
</td>
</tr>
</table>
</form>
<?php
}
?>

and here for backend part:

<?php
include_once 'db.php';
if(isset($_POST['save_mul']))
{       
$allow = array("jpg", "jpeg", "gif", "png");
$todir = '../../userhome/cartu/carti/images/';
if ( !!$_FILES['file']['tmp_name'] ) // is the file uploaded yet?
{
$info = explode('.', strtolower( $_FILES['file']['name']) ); // whats the 
extension of the file
if ( in_array( end($info), $allow) ) // is this file allowed
{

$newfilename = round(microtime(true)) . '.' . end($info);       
    if ( move_uploaded_file( $_FILES['file']['tmp_name'], $todir 
.$newfilename ) )
    {
        $total = $_POST['total'];
        $n_img = $newfilename;      
for($i=1; $i<=$total; $i++)
{
    $fn6 = $_POST["vendorid"];
    $fn = $_POST["item_name$i"];
    $fn1 = $_POST["item_desc$i"];                               
    $sql="INSERT INTO items(vendorid,item_name,item_desc,item_image) 
VALUES('".$fn6."','".$fn."','".$fn1."','".$n_img."')";
    $sql = $db->query($sql);        
}   
if($sql)
{
    ?>
    <script>
    alert('<?php echo $total." records was inserted !!!"; ?>');
    window.location.href='additem.php';
    </script>
    <?php
}
else
{
    ?>
    <script>
    alert('error while inserting , TRY AGAIN');
    </script>
    <?php
}
    }
}
else
{
   echo "Invalid file"; // error this file ext is not allowed
}
}               
}
?>

i am still new in web development, and dont sure if i ask silly question, but this problem i had been stuck for a long time. a had search for many forum community and mostly solved for uploaded multiple images in a input. Any sources for my reference is appreciate. Thank You.

  • 写回答

2条回答 默认 最新

  • duanlinzhen7235 2017-12-03 11:49
    关注

    each input file name must have different name.

    <input type='file' name="file<?php echo $i; ?>" id="file<?php echo $i; ?>"  value=''>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题