2016-06-26 126 views
0

我正在上傳時嘗試重命名文件,但並未按照我的預期重命名。只是想添加名稱的日期和時間。
附上下面的代碼。在上傳時重命名文件

if(isset($_FILES['img_ct_1'])){ 

$today = date("Ymd"); 

//prepare url 
$temp_path = CDN_URL.'photos/'; 

$name_array = $_FILES['img_ct_1']['name']; 
$tmp_name_array = $_FILES['img_ct_1']['tmp_name']; 
$type_array = $_FILES['img_ct_1']['type']; 
$size_array = $_FILES['img_ct_1']['size']; 
$error_array = $_FILES['img_ct_1']['error']; 
$upload_dir = $_SERVER['DOCUMENT_ROOT'].'/photos/'; 


for($i = 0; $i < count($tmp_name_array); $i++){ 
    if(move_uploaded_file($tmp_name_array[$i],$upload_dir.time().$name_array[$i])){ 
     print_r ($name_array[$i]); 
     $array['path'] = $temp_path.$newfilename; 
     $array['success'] = true ; 
    } else { 
     echo "move_uploaded_file function failed for ".$name_array[$i]."<br>"; 
    } 
} 
} 

回答

0

試試這個方法:

for($i = 0; $i < count($tmp_name_array); $i++){ 
    $file = $today.time().$name_array[$i]; 
    if(move_uploaded_file($tmp_name_array[$i], $upload_dir . $file)){ 
     print_r ($name_array[$i]); 
     $array['path'] = $temp_path.$newfilename; 
     $array['success'] = true ; 
    } else { 
     echo "move_uploaded_file function failed for ".$name_array[$i]."<br>"; 
    } 
} 
+0

謝謝工程:)。你能幫我把這個放在一個函數裏面嗎?大幫忙 – arshad

+0

如果有幫助,請提出答案。你想把哪個部分放入函數中? –

+0

我做了,但我想我沒有足夠的分數。我想要訪問像下面的函數,並返回數組$ image_path $ image_path = upload_patient_doc($ _ FILES,'img_ct_1'); – arshad