首页 > 解决方案 > 使用 this.form.submit() 上传图片的问题;

问题描述

我正在使用 this.form.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>';
        
        }
    }
}

标签: phphtmlsql

解决方案


正如我在上面的评论中提到的那样,这确实是一些愚蠢的事情...如果您仔细检查 if(file_exists) ,您会发现 stmt 在其中,而且...没有其他上传所以如果图像是唯一的,什么都没有发生,所以我只是复制了 move_uploaded_file 和其余部分并添加了一个 else{ 并解决了这个问题。

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>';
    
    }
}

推荐阅读