gpt4 book ai didi

php - 多图上传如何制作呢?

转载 作者:行者123 更新时间:2023-11-29 19:15:10 25 4
gpt4 key购买 nike

如果我想将一张图像上传到我的 MySQL 和服务器,我有一个可以正常工作的代码。

PHP 代码:

if(isset($_POST['btnsave']))
{ foreach($_FILES['user_image']['tmp_name'] as $key => $tmp_name ){

$username = $_POST['user_name'];
$userjob = $_POST['user_job'];

$imgFile = $key.$_FILES['user_image']['name'][$key];
$tmp_dir = $_FILES['user_image']['tmp_name'][$key];
$imgSize = $_FILES['user_image']['size'][$key];
}

if(empty($username)){
$errMSG = "Please Enter Username.";
}
else if(empty($userjob)){
$errMSG = "Please Enter Your Job Work.";
}
else if(empty($imgFile)){
$errMSG = "Please Select Image File.";
}
else
{
$upload_dir = 'user_images/';
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
$userpic = rand(1000,1000000).".".$imgExt;
if(in_array($imgExt, $valid_extensions)){
if($imgSize < 5000000) {
move_uploaded_file($tmp_dir,$upload_dir.$userpic);
}
else{
$errMSG = "Sorry, your file is too large.";
}
}
else{
$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
}
if(!isset($errMSG))
{
$stmt = $DB_con->prepare('INSERT INTO tbl_users(userName,userProfession,userPic) VALUES(:uname, :ujob, :upic)');
$stmt->bindParam(':uname',$username);
$stmt->bindParam(':ujob',$userjob);
$stmt->bindParam(':upic',$userpic);

if($stmt->execute())
{
$successMSG = "new record successfully inserted ...";
header("refresh:5;index.php");
}
else
{
$errMSG = "error while inserting....";
}
}
}

HTML 代码:

    <?php
if(isset($errMSG)){
?>
<div class="alert alert-danger">
<span class="glyphicon glyphicon-info-sign"></span> <strong><?php echo $errMSG; ?></strong>
</div>
<?php
}
else if(isset($successMSG)){
?>
<div class="alert alert-success">
<strong><span class="glyphicon glyphicon-info-sign"></span> <?php echo $successMSG; ?></strong>
</div>
<?php
}
?>

<form method="post" enctype="multipart/form-data" class="form-horizontal">

<table class="table table-bordered table-responsive">

<tr>
<td><label class="control-label">Username.</label></td>
<td><input class="form-control" type="text" name="user_name" placeholder="Enter Username" value="<?php echo $username; ?>" /></td>
</tr>

<tr>
<td><label class="control-label">Profession(Job).</label></td>
<td><input class="form-control" type="text" name="user_job" placeholder="Your Profession" value="<?php echo $userjob; ?>" /></td>
</tr>

<tr>
<td><label class="control-label">Profile Img.</label></td>
<td><input class="input-group" type="file" name="user_image[]" accept="image/*" multiple /></td>
</tr>

<tr>
<td colspan="2"><button type="submit" name="btnsave" class="btn btn-default">
<span class="glyphicon glyphicon-save"></span> &nbsp; save
</button>
</td>
</tr>

</table>

</form>

如何编写代码以用于多次上传?对于我要上传的所有图像,我将使用相同的值,但“userPic”除外 - 与我的服务器上的文件名称相同。你能帮我一下吗?

<小时/>

我又发现一个问题!也许你们可以帮助我?

如果我尝试上传例如 6 张图像,其中一个主题大于 maxsize,其弹出窗口会出现错误,但在进入上传文件夹之前上传的所有其他文件都会出现错误。如果出现错误,如何删除该文件?

第二个问题是如何调整图像大小?有代码吗?

最佳答案

将所有内容放入 foreach block 中。

if(isset($_POST['btnsave']))
{ foreach($_FILES['user_image']['tmp_name'] as $key => $tmp_name ){

$username = $_POST['user_name'];
$userjob = $_POST['user_job'];

$imgFile = $key.$_FILES['user_image']['name'][$key];
$tmp_dir = $_FILES['user_image']['tmp_name'][$key];
$imgSize = $_FILES['user_image']['size'][$key];


if(empty($username)){
$errMSG = "Please Enter Username.";
}
else if(empty($userjob)){
$errMSG = "Please Enter Your Job Work.";
}
else if(empty($imgFile)){
$errMSG = "Please Select Image File.";
}
else
{
$upload_dir = 'user_images/';
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
$userpic = rand(1000,1000000).".".$imgExt;
if(in_array($imgExt, $valid_extensions)){
if($imgSize < 5000000) {
move_uploaded_file($tmp_dir,$upload_dir.$userpic);
}
else{
$errMSG = "Sorry, your file is too large.";
}
}
else{
$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
}
if(!isset($errMSG))
{
$stmt = $DB_con->prepare('INSERT INTO tbl_users(userName,userProfession,userPic) VALUES(:uname, :ujob, :upic)');
$stmt->bindParam(':uname',$username);
$stmt->bindParam(':ujob',$userjob);
$stmt->bindParam(':upic',$userpic);

if($stmt->execute())
{
$successMSG = "new record succesfully inserted ...";
header("refresh:5;index.php");
}
else
{
$errMSG = "error while inserting....";
}
}
}}

关于php - 多图上传如何制作呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42771391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com