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 ?

图片转代码服务由CSDN问答提供 功能建议

美好的一天,这里我有一个页面存储数据,其中包含一些图像,准确地说是3个图像,< / p>

 &lt;?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))。  ''  。 结束($ temp); 
 $ new1 = round(microtime(true))。  ''  。 结束($ temp1); 
 $ new2 = round(microtime(true))。  ''  。 结束($ 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 =“插入imgstuff值('$ brg  ”, '$ NAMA', '$新', '$名new1', '$ NEW2');“; 
 
 $的插件= mysqli_query($ CON,$ C); 
 
如果($ INS){  
header('location:test.php?success ='。base64_encode('success')); 
} else {
header('location:test.php?error ='。base64_encode('failed')); \  n} 
?&gt; 
   
 
 
 
 

以上代码完美无缺,但正如您所看到的那样,它最终会变得非常丑陋并且会产生相同的三个文件名 图片,

我的问题是,有没有办法让我的代码更清晰,易于维护,我的命名文件方法有什么问题?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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 ) );
                }
            }
        }
    ?>
    
    打赏 评论
  • doulu1914 2017-12-06 08:42

    Maybe

    round(microtime(true)) . '.' . end($temp);
    

    return the same things because it's done almost at the same time. Can you try something like uuid or even a simple function such as

    function getUniqFileName($end) {
        $token = md5(sha1(rand()));
    
        return sprintf($token%s, $end);
    }
    

    and then in your code

    $new = getUniqFileName(end($temp));
    

    That will give you a bit more uniq name very quickly ?

    (Edit : removed the round which is quite useless in this case)

    打赏 评论

相关推荐 更多相似问题