2014-12-27 7 views
2

PHP에서 여러 파일 업로드 양식이 있습니다. 내 문제는 두 번째 이미지가 이전 이미지로 대체 된 동일한 이름의 두 이미지가 업로드 될 때입니다. 내가 무엇을 할 수 있을지?같은 이름의 여러 파일 업로드 문제.

if(isset($_POST['submit'])){ 
    if(count($_FILES['upload']['name']) > 0){ 
     //Loop through each file 
     for($i=0; $i<count($_FILES['upload']['name']); $i++) { 
      //Get the temp file path 
      $tmpFilePath = $_FILES['upload']['tmp_name'][$i]; 

      //Make sure we have a filepath 
      if($tmpFilePath != ""){ 

       //save the filename 
       $shortname = $_FILES['upload']['name'][$i]; 

       //save the url and the file 
       $filePath = "store/".$_FILES['upload']['name'][$i]; 

       //Upload the file into the temp dir 
       if(move_uploaded_file($tmpFilePath, $filePath)) { 

        $files[] = $shortname; 

        $name = 'Admin'; 

        $qry="INSERT INTO test(Name,Image) VALUES('$name','$filePath')"; 

        mysqli_query($link,$qry) or die(mysqli_error($link)); 




        //use $shortname for the filename 
        //use $filePath for the relative url to the file 

       } 
      } 
      echo '<p style="color:#000;">SUCCESS!!</p>'; 

     } 
    } 

나 제안

<form action="" enctype="multipart/form-data" method="post"> 

    <div> 
     <label for='upload'>Add Attachments:</label> 
     <input id='upload' name="upload[]" onChange="read_url(this)" type="file" multiple="multiple" id="imgInp" /> 
    </div> 

    <p><input type="submit" name="submit" value="Submit"></p> 

</form> 

PHP 코드

HTML ....

+1

파일 이름 바꾸기? – t3chb0t

+1

코드가 SQL 삽입에 취약합니다. – Boann

답변

3

는 각 파일의 파일 이름을 바꿀 수 있습니다 : 이것은 내 코드입니다. 그것을 수행하는 가장 간단한 방법은 예를 들어 그것을 연결하는 것입니다.

$filename = $_FILES['myfilename']['name']; 
$filename = time().$filename; 
+0

당신이 견적을 놓친 – DarkBee

+0

나는 그것을 놓쳤습니다 :-( –

+1

글쎄 대답을 편집하고 그것을 추가해야합니다 :-) – DarkBee