dsxay48646 2017-12-06 08:34
浏览 50
已采纳

多个图像上传到不同的列php mysql

good day , here i have a page that store data with some images in it , 3 images to be exact,

    <?php 
    $brg            = $_POST['id'];
    $nama           = $_POST['nm'];
    $img            = $_FILES['img']['name'];
    $tmp            = $_FILES['img']['tmp_name'];
    $img1           = $_FILES['img1']['name'];
    $tmp1           = $_FILES['img1']['tmp_name'];
    $img2           = $_FILES['img2']['name'];
    $tmp2           = $_FILES['img2']['tmp_name'];

    $temp           = explode(".", $img);
    $temp1          = explode(".", $img1);
    $temp2          = explode(".", $img2);
    $new            = round(microtime(true)) . '.' . end($temp);
    $new1           = round(microtime(true)) . '.' . end($temp1);
    $new2           = round(microtime(true)) . '.' . end($temp2);
    $path           = "img/photo/".$new;
    $path1          = "img/photo/".$new1;
    $path2          = "img/photo/".$new2;
    move_uploaded_file($tmp, $path);
    move_uploaded_file($tmp1, $path1);
    move_uploaded_file($tmp2, $path2);

$c  = "insert into imgstuff values('$brg','$nama','$new','$new1','$new2');";

$ins=mysqli_query($con,$c);

if($ins){
header('location: test.php?success='.base64_encode('success'));
} else {
header('location: test.php?error='.base64_encode('failed'));
}
?>

code above works perfectly but as you can see it ends up horribly ugly and it produce same file name for three picture ,

my question, is there any ways to made my code cleaner and easy to maintain and whats wrong with my naming files method ?

  • 写回答

2条回答 默认 最新

  • dssnh86244 2017-12-06 09:49
    关注

    I'd be tempted to use a loop so that you do not repeat code unnecessarily and definitely to use prepared statements to avoid nasty unpleasantness from sql injection attacks

    As for the new image name - there are many ways in which you can achieve a new, unique name - the method here is not necessarily going to be unique but it does at least incorporate the original filename so of the three images they should be unique. None of the below is tested btw but it might give you some ideas.

    <?php
        if( isset( $_POST['id'], $_POST['nm'] ){
    
            $files=array();
    
            $sql='insert into `imgstuff` values (?,?,?,?,?)';
            $stmt=$con->prepare( $sql );
    
            if( $stmt ){
    
                for( $i=0; $i < 3; $i++ ){
    
                    $obj = $i==0 ? (object)$_FILES[ 'img' ] : (object)$_FILES[ 'img' . $i ];
                    $tmp=$obj->tmp_name;
                    $name=$obj->name;
    
                    $ext = pathinfo( $name,PATHINFO_EXTENSION );
                    $new = round( microtime( true ) ) . '_' . $name . '.' . $ext;
    
                    $path = "img/photo/$new";
                    $status = is_uploaded_file( $tmp ) & move_uploaded_file( $tmp, $path );
                    if( $status ) $files[] = $path;
                }
    
                if( count( $files )==count( $imgs ) ){
    
                    $brg  = $_POST['id'];
                    $nama = $_POST['nm'];
    
                    $stmt->bind_param( 'issss', $brg, $nama, $files[0], $files[1], $files[2] );
                    $stmt->execute();
    
                    $rows=$stmt->affected_rows;
                    $message=$rows!=0 ? 'success' : 'failed';
    
                    header('location: test.php?success=' . base64_encode( $message ) );
                }
            }
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败