生成多输入类型文件并上传并存储到数据库中
Generating multiple input type file and upload and store into db
嗨...我正在尝试从我选择生成的图像数量上传图像并将图像名称保存到数据库中。例如,我选择生成 3 个输入类型 "file",然后 3 个输入将自动生成,因此我可以将其上传到文件夹并将名称保存到数据库中。我已经完成了其他类型的输入,例如 "text"。但仍然停留在上传图片部分。
表格看起来像这样(如果我选择生成 3 次):
这是我的表单代码示例:
<?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> Insert all Records</button>
<a href="additem.php" class="btn btn-large btn-success"> <i class="glyphicon
glyphicon-fast-backward"></i> Back to index</a>
</td>
</tr>
</table>
</form>
<?php
}
?>
这里是后端部分:
<?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
}
}
}
?>
我在网络开发方面仍然是新手,不确定我是否问了愚蠢的问题,但这个问题我已经被困了很长时间。 a 搜索了许多论坛社区,主要解决了在输入中上传多张图片的问题。
感谢任何供我参考的资源。谢谢你。
每个输入文件名必须有不同的名称。
<input type='file' name="file<?php echo $i; ?>" id="file<?php echo $i; ?>" value=''>
这对我来说很完美,我只是按照建议在输入类型文件的名称上添加了 <?php echo $i>
,并修改了后端的一些安排,如下所示。
<?php
include_once 'db.php';
if(isset($_POST['save_mul']))
{
$allow = array("jpg", "jpeg", "gif", "png");
$todir = '../../userhome/cartu/carti/images/';
$total = $_POST['total'];
for($i=1; $i<=$total; $i++)
{
$imgupload = $_FILES["file$i"];
if ( !!$imgupload['tmp_name'] ) // is the file uploaded yet?
{
$info = explode('.', strtolower( $imgupload['name']) ); // whats the
extension of the file
if ( in_array( end($info), $allow) ) // is this file allowed
{
if ( move_uploaded_file( $imgupload['tmp_name'], $todir
.$imgupload["name"] ) )
{
$fn6 = $_POST["vendorid"];
$fn = $_POST["item_name$i"];
$fn1 = $_POST["item_desc$i"];
$n_img = $imgupload["name"];
$sql="INSERT INTO
shopping_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
}
}
}
}
?>
嗨...我正在尝试从我选择生成的图像数量上传图像并将图像名称保存到数据库中。例如,我选择生成 3 个输入类型 "file",然后 3 个输入将自动生成,因此我可以将其上传到文件夹并将名称保存到数据库中。我已经完成了其他类型的输入,例如 "text"。但仍然停留在上传图片部分。
表格看起来像这样(如果我选择生成 3 次):
<?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> Insert all Records</button>
<a href="additem.php" class="btn btn-large btn-success"> <i class="glyphicon
glyphicon-fast-backward"></i> Back to index</a>
</td>
</tr>
</table>
</form>
<?php
}
?>
这里是后端部分:
<?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
}
}
}
?>
我在网络开发方面仍然是新手,不确定我是否问了愚蠢的问题,但这个问题我已经被困了很长时间。 a 搜索了许多论坛社区,主要解决了在输入中上传多张图片的问题。 感谢任何供我参考的资源。谢谢你。
每个输入文件名必须有不同的名称。
<input type='file' name="file<?php echo $i; ?>" id="file<?php echo $i; ?>" value=''>
这对我来说很完美,我只是按照建议在输入类型文件的名称上添加了 <?php echo $i>
,并修改了后端的一些安排,如下所示。
<?php
include_once 'db.php';
if(isset($_POST['save_mul']))
{
$allow = array("jpg", "jpeg", "gif", "png");
$todir = '../../userhome/cartu/carti/images/';
$total = $_POST['total'];
for($i=1; $i<=$total; $i++)
{
$imgupload = $_FILES["file$i"];
if ( !!$imgupload['tmp_name'] ) // is the file uploaded yet?
{
$info = explode('.', strtolower( $imgupload['name']) ); // whats the
extension of the file
if ( in_array( end($info), $allow) ) // is this file allowed
{
if ( move_uploaded_file( $imgupload['tmp_name'], $todir
.$imgupload["name"] ) )
{
$fn6 = $_POST["vendorid"];
$fn = $_POST["item_name$i"];
$fn1 = $_POST["item_desc$i"];
$n_img = $imgupload["name"];
$sql="INSERT INTO
shopping_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
}
}
}
}
?>