2012-11-19 2 views
1

음, 저는 .php 파일 (uploader.php)로 처리하는 html 업로드 양식을 사용했습니다. 여러 파일 업로드 코드입니다.PHP 업로드 파일의 고유 한 이름 문제

업로드 할 때 모든 파일은 개별 파일 이름으로 디렉토리를 업로드합니다. 좋아요 : abc.jpg, abc2.jpg 등

하지만 누군가가 동일한 이름의 파일을 업로드하면 기존 파일이 ovrwrite됩니다. 업로드 된 파일에 고유 한 이름이 필요합니다. 고유 한 무작위 이름으로 파일의 이름을 바꿀 수있는 방법이 있습니까? 마찬가지로 : 12545454.jpge, 234324324.jpge, 323453253.jpg 등

의 PHP 코드 :

<?php 
if (isset($_POST['Submit'])) { 
$number_of_file_fields = 0; 
$number_of_uploaded_files = 0; 
$number_of_moved_files = 0; 
$uploaded_files = array(); 
$upload_directory = dirname(__file__) . '/uploaded/'; //set upload directory 


/** 
* we get a $_FILES['images'] array , 
* we procee this array while iterating with simple for loop 
* you can check this array by print_r($_FILES['images']); 
*/ 
for ($i = 0; $i < count($_FILES['images']['name']); $i++) { 
    $number_of_file_fields++; 
    if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not 
     $number_of_uploaded_files++; 
     $uploaded_files[] = $_FILES['images']['name'][$i]; 
     if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . 
$_FILES['images']['name'][$i])) { 
      $number_of_moved_files++; 
     } 

    } 

} 
echo "Number of File fields created $number_of_file_fields.<br/> "; 
echo "Number of files submitted $number_of_uploaded_files . <br/>"; 
echo "Number of successfully moved files $number_of_moved_files . <br/>"; 
echo "File Names are <br/>" . implode(',', $uploaded_files); 
} 
?> 
+0

업로드 한 파일 이름의 고유 한 임의 번호로 이름을 바꾸려고합니다. – Shibbir

+0

만들었지 만 $ image_named_uniq를 사용하지 마십시오 –

+0

[uniqid()] (http://php.net/manual/en/function.uniqid.php) – nkamm

답변

1

당신은 time();session_id() 또는 wormhit에 의해 제안 uniqid()도에 대한 아주 좋은 생각입니다 CONCAT 수 있습니다 각 파일의 이름. 코드 아래

따른 사용 :

<?php 
$target_path = "images/"; 

$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['uploadedfile']['name']). 
    " has been uploaded"; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
} 
//$upload_directory = ''.uniqid().'_'.dirname(__file__) . '/uploaded/'; //set upload directory 

$uploaded_file="".$upload_directory ."/".uniqid()."_".$_FILES['images']['name'][$i].""; 

if (move_uploaded_file($_FILES['images']['tmp_name'][$i],$uploaded_file)) { 
      $number_of_moved_files++; 
     } 
?> 


<?php 
if (isset($_POST['Submit'])) { 
$number_of_file_fields = 0; 
$number_of_uploaded_files = 0; 
$number_of_moved_files = 0; 
$uploaded_files = array(); 
$upload_directory = dirname(__file__) . '/uploaded/'; //set upload directory 


/** 
* we get a $_FILES['images'] array , 
* we procee this array while iterating with simple for loop 
* you can check this array by print_r($_FILES['images']); 
*/ 
for ($i = 0; $i < count($_FILES['images']['name']); $i++) { 
    $number_of_file_fields++; 
    if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not 

     $uploaded_file="".$upload_directory ."".uniqid()."_".$_FILES['images']['name'][$i].""; 

     $number_of_uploaded_files++; 
     $uploaded_files[] = $_FILES['images']['name'][$i]; 
     if (move_uploaded_file($_FILES['images']['tmp_name'][$i],$uploaded_file)) { 
      $number_of_moved_files++; 
     } 

    } 

} 
echo "Number of File fields created $number_of_file_fields.<br/> "; 
echo "Number of files submitted $number_of_uploaded_files . <br/>"; 
echo "Number of successfully moved files $number_of_moved_files . <br/>"; 
echo "File Names are <br/>" . implode(',', $uploaded_files); 
} 
?> 
+0

예! @Vikas Umrao 좋은 생각입니다. – Shibbir

+0

@Vikash Umrao 어떻게 원래 파일 이름을 time()으로 바꿉니 까? – Shibbir

+0

이 $ upload_directory = ''.uniqid() .'_ '. dirname (__ file__)을 바꿉니다. '/ 업로드 됨 /'; // 코드에서 업로드 디렉토리를 설정하십시오. –

0

당신이

$ext = pathinfo($file_name, PATHINFO_EXTENSION); 
$newfilename = substr(sha1(uniqid (rand())), 0, 10). '.'.$ext; 
3

사용 uniqid() 사용할 수 있습니다. 이것을 위해 만들어졌습니다.