php - 使用 this.form.submit() 上传图片时出现问题;

标签 php html sql

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

最佳答案

正如我在上面的评论中提到的那样,这真的是我没有看到的愚蠢的东西......如果你仔细检查 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>';
    
    }
}

关于php - 使用 this.form.submit() 上传图片时出现问题;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64846700/

相关文章:

php - PHPUnit生成clover.xml时定义相对路径?

javascript - innerText和变量不起作用的比较

mysql - 表格中的电子邮件验证存在问题

javascript - css 同步关键帧动画

计数子句中的 SQL CASE 语句

sql - 多个表上的多个 LEFT OUTER JOIN

php - Facebook 风格封面照片上传、重新定位、裁剪和保存在 bootstrap 3 中使用 php 和 jquery

javascript - ajax post 的原始 javascript eq 无法正常工作

php - Quickbooks Windows 与 PHP MySQL 镜像同步

python - 如何使用 App Engine Python 处理 HTML 数组?