简体   繁体   中英

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.

If you want to combine the messages, count success and errors and then, if either count is above 0, print a corresponding error message.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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