简体   繁体   English

在php和sql数据库中上传5张具有多个扩展名的图片

[英]Upload 5 picture with multiple extension in php and sql database

I went to upload picture with any or multiple extension this code work with multiple but give multiple success message 我上传了具有任意扩展名或多个扩展名的图片,此代码可以使用多个扩展名,但会给出多个成功消息

<?php
if (isset($_POST['submit'])) {
    $j = 0; //Variable for indexing uploaded image 

    $target_path = "uploads/"; //Declaring Path for uploaded images
    for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array

        $validextensions = array("jpeg", "jpg", "png");  //Extensions which are allowed
        $ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.) 
        $file_extension = end($ext); //store extensions in the variable

        $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];//set the target path with a new name of image
        $j = $j + 1;//increment the number of uploaded images according to the files in array       

      if (($_FILES["file"]["size"][$i] < 2097152) //Approx. 100kb files can be uploaded.
                && in_array($file_extension, $validextensions)) {
            if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {//if file moved to uploads folder
                echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
            } else {//if file was not moved.
                echo $j. ').<span id="error">please try again!.</span><br/><br/>';
            }
        } else {//if file size and file type was incorrect.
            echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
        }
    }
}
?>

The success message span is within the for loop, so it prints every time an image is uploaded. 成功消息范围在for循环内,因此每次上载图像时都会打印。

If you want to combine the messages, count success and errors and then, if either count is above 0, print a corresponding error message. 如果要合并消息,请计算成功和错误,然后,如果其中一个计数大于0,则打印相应的错误消息。

Try to replace this part. 尝试更换这部分。

       if (($_FILES["file"]["size"][$i] < 2097152) //Approx. 100kb files can be uploaded.
                && in_array($file_extension, $validextensions)) {
            if (!move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {//if file moved to uploads folder
                exit($j. ').<span id="error">please try again!.</span><br/><br/>');
            }
       } else {//if file size and file type was incorrect.
                exit($j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>');
       }
    }
    echo $j.' files were uploaded.';
}
 I would try another wat to insert multiple picture extension in my database.



   insert.php
        ------------
            <?php 
                 require "functions.php";

                if (isset($_POST['submit'])) {

                $ext =""; 
                for($i=0;$i<count($_FILES['pic']['name']);$i++){
                if ($ext) {
                $ext .= "|";
                 }
                $ext .= Extension($_FILES['pic']['name'][$i]);
                }



                $data = array(
               "picture"=>$ext,
                 );

            if ($id =insert("fm",$data)) {
            if($ext){
            $ext = explode("|", $ext);
            $i = 1;
            foreach ($ext as $e) {
            $dest = "images/".md5($id."-$i-tui-akta-kufa").".$e";
            move_uploaded_file($_FILES['pic']['tmp_name'][$i-1], $dest);
            $i++;
             }
            }

            echo "Insert successfully";
           }
          else{
          echo "Server too busy";
         }


           }
            else{
       echo "invalid process";
             }

          ?>    




       functions.php
       -----------------
    function Extension($field){
    $mofiz = "";
    if ($field) {
    $mofiz = pathinfo($field);
    $mofiz = strtolower($mofiz['extension']);
        if ($mofiz != "jpg" && $mofiz != "png" && $mofiz != "jpeg" && $mofiz != "gif" 
          ) {
            $mofiz = "";
        }
       }
        return $mofiz;
        }


          form.php
         --------------
              <tr>
            <td>Picture1</td>
            <td><input type="file" name="pic[]" multiple></td>
            </tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM