使用此.form.submit();上传图像时出现问题



我正在使用this.form.submit((;以便在售出后直接上传图像,并传递一些其他信息。

我遇到了一个问题,submit可以工作并通过post传递id,但图像没有上传,名称也没有插入数据库中——它可能会添加一个类似0_的名称,但这是不正确的,而且图像名称在表中不存在,所以它应该在没有任何循环的情况下传递if(file_exists(。

对此有什么想法吗?

<form id='image_upload' name='image_upload' action='customer_logo.php' method='post'>
<input type="hidden" name='id' id='id' value='<?php echo $cust_id; ?>'>
<input type='text' style="background-color:#93CD60; border-radius: 5px; color:white; font-size:17px; width:140px; text-align:center; cursor:pointer;" id='image_text' name='image_text' value="Add Logo"> </input>
<input type='file' name='image' id='image' style='visibility: hidden'  accept="image/*" onchange="this.form.submit();" />
</form>

php代码如下:

if (isset ($_POST['image'])) {
$cust_id = $_POST['id'];
$images = $_FILES['image']['name'];
$tmp_dir = $_FILES['image']['tmp_name'];
$imageSize = $_FILES['image']['size'];
if (!is_dir('img/logotipa/'.$cust_id)){
mkdir('img/logotipa/'.$cust_id, 0777, true);
}
$upload_dir = 'img/logo/'.$cust_id.'/';
$imgExt = strtolower(pathinfo($images,PATHINFO_EXTENSION));
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
$up_image = $images;
if (file_exists($upload_dir.$images)){
$counter = 0;

while (file_exists($upload_dir.$up_image)){
$up_image = $counter.'_'.$images;
$counter++;            
}
move_uploaded_file($tmp_dir, $upload_dir.$images);
$stmt = $conn->prepare('INSERT INTO logos(customer_id, logo) VALUES (:ucid, :upic)');
$stmt->bindParam(':ucid', $cust_id);
$stmt->bindParam(':upic',  $up_image);
if ($stmt->execute()){
echo '<script>window.location.href = "customer_logo.php";</script>';

}
}
}

这真的很愚蠢,我在上面的评论中没有看到。。。如果你仔细检查If(file_exists(,你可以看到stmt在里面,嗯。。。没有其他文件可以上传,所以如果图像是唯一的,就不会发生任何事情,所以我只复制了move_uploaded_file和其他文件,并添加了一个其他文件,从而解决了问题。

if (isset ($_POST['image'])) {
$cust_id = $_POST['id'];
$images = $_FILES['image']['name'];
$tmp_dir = $_FILES['image']['tmp_name'];
$imageSize = $_FILES['image']['size'];
if (!is_dir('img/logotipa/'.$cust_id)){
mkdir('img/logotipa/'.$cust_id, 0777, true);
}
$upload_dir = 'img/logo/'.$cust_id.'/';
$imgExt = strtolower(pathinfo($images,PATHINFO_EXTENSION));
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
$up_image = $images;
if (file_exists($upload_dir.$images)){
$counter = 0;

while (file_exists($upload_dir.$up_image)){
$up_image = $counter.'_'.$images;
$counter++;            
}
move_uploaded_file($tmp_dir, $upload_dir.$images);
$stmt = $conn->prepare('INSERT INTO logos(customer_id, logo) VALUES (:ucid, :upic)');
$stmt->bindParam(':ucid', $cust_id);
$stmt->bindParam(':upic',  $up_image);
if ($stmt->execute()){
echo '<script>window.location.href = "customer_logo.php";</script>';

}
}else{
move_uploaded_file($tmp_dir, $upload_dir.$images);
$stmt = $conn->prepare('INSERT INTO logos(customer_id, logo) VALUES (:ucid, :upic)');
$stmt->bindP
aram(':ucid', $cust_id);
$stmt->bindParam(':upic',  $up_image);
if ($stmt->execute()){
echo '<script>window.location.href = "customer_logo.php";</script>';

}
}

最新更新