2017-05-27 57 views
0

我正在使用php文件,並試圖一次性上載多個圖像,並輸入[type = files]。問題是我將一堆圖像上傳到一個由php方法mkdir()動態創建的文件夾中。並在每次提交時,我想要一個新的文件夾,以相同的名稱「文件」增加一個數字。例如,在第一次提交表單時,文件名將是.../file1,第二次提交它將是.../file2等等。我寫到目前爲止的代碼工作正常,但價值沒有動態增加,並向我顯示該目錄已存在的錯誤。以下是我的代碼。如果您檢查代碼,目錄文件夾是(files/file。$ a),其中$ a = 1;然後在最後一行加上$ a ++;但它仍然只是做一個文件夾,先提交併不會增加在第二提交等..php多個文件上傳到動態製作的目錄

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++; 
 

 
}

回答

0

隨着每次上傳你的PHP腳本開始了,所以它沒有$ a的記憶並從1開始。因此,您可以檢查$ makeFolder的存在並根據需要增加$ a,直到生成的名稱不存在。或者你必須存儲$ a的最後一個值(在文件或數據庫中)。

+1

謝謝你,php cookie爲我工作:) –