arshad arshad - 5 months ago 24
PHP Question

Rename the files while uploading

I am trying to rename the files while uploading, but it's not renaming as I expected. Just want to add the date and the time front of the name.

Attaching the code below.

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>";
}
}
}

Answer

Try this way:

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>";
    }
}
Comments