0

I am working with php files, and trying to upload number of images at a time, with input[type=files]. The problem is I am uploading bunch of images to a folder which is dynamically made , by the php method, mkdir(). and on each submit, i want a new folder to be made, with same name "file" incremented by a digit. For example on first submit of the form, the file name will be .../file1 on second submit it will be .../file2 and so on. The code i have wrote so far is working fine, but the value isnt incremented dynamically and shows me an error that the directory already exists. The following is my code. If you check the code the directory folder is (files/file.$a) where $a=1; and then its incremented on the last line as $a++; but still it just make a folder on first submit and doesn't increment on second submit and so on..

if (isset($_POST["upl-submit"])) {

  $a = 1;
  $makeFolder = "files/file".$a; //Files Inside Directory
  mkdir($makeFolder);
  //Save The images in the folder
  if (isset($_FILES["prImgs"])) {

    $imgName = $_FILES["prImgs"]["name"];
    $imgType = $_FILES["prImgs"]["type"];
    $imgSize = $_FILES["prImgs"]["size"];
    $imgTemp = $_FILES["prImgs"]["tmp_name"];
    $imgErr = $_FILES["prImgs"]["error"];
    $pathsArr = array();
    $all_ext = array("jpg", "jpeg", "png", "bmp");


    for ($i = 0; $i < count($imgName); $i++) {
      $ext = explode(".", $imgName[$i]); //To get the extensions of all files
      if (in_array(end($ext), $all_ext)) { //To check if the extensions meet the allowed ones or not
        $newName = "img".($i + 1).
        ".".end($ext); //New name selected
        $totPath = $makeFolder.
        "/".$newName;
        $pathsArr[]. = $totPath;
        if (move_uploaded_file($imgTemp[$i], $totPath)) {
          echo "<script>console.log('Uploaded');</script>";
        } else {
          echo "<script>console.log('File Uploading Error');</script>";
        }
      } else {
        echo "<script> alert('Unaccepted Format'); </script>";
      }
    }
  }

  //Save the pdf in the same folder
  if (isset($_FILES["prPdf"])) {
    $pdfName = $_FILES["prPdf"]["name"];
    $pdfType = $_FILES["prPdf"]["type"];
    $pdfSize = $_FILES["prPdf"]["size"];
    $pdfTemp = $_FILES["prPdf"]["tmp_name"];
    $pdfErr = $_FILES["prPdf"]["error"];
    $pdfPath;
    $pdf_ext = "pdf";


    $pdfExt = explode(".", $pdfName); //To get the extension of the pdf file
    if (end($pdfExt) == $pdf_ext) {
      $pdfNew = "pdfFile".
      ".".end($pdfExt); //New name selected
      $totPdfPath = $makeFolder.
      "/".$pdfNew;
      $pdfPath = $totPdfPath;

      if (move_uploaded_file($pdfTemp, $totPdfPath)) {
        echo "<script>console.log('pdf Uploaded');</script>";
      } else {
        echo "<script>console.log('pdf Uploading Error');</script>";
      }
    } else {
      echo "<script> alert('Unaccepted Format'); </script>";
    }

  }

  $a++;

}

1 Answer 1

0

With each upload your php script is started over, so it has no memory of $a and starts with 1 again. So you could either check for the existence of $makeFolder and increase $a as necessary until the resulting name does not exist. Or you have to store the last value of $a (in a file or database).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.