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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料