I am developing a PHP application in which I have to upload multiple files on the server, what I want is that when a user uploads multiple files the system should check if any of the files already exists, if yes then it should ask the user whether to rename the file or replace the older file,if user selects the rename option it should rename the file and if the user asks to replace the file it should do accordingly.
Untill Now I am using the following Code to upload the files:
<?php
if(isset($_FILES['files'])){
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_name = $_FILES['files']['name'][$key];
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
// $query="INSERT into upload_data (`USER_ID`,`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`) VALUES('$user_id','$file_name','$file_size','$file_type'); ";
$desired_dir="../reztrictedfolder/dir1/";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(file_exists("$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"../reztrictedfolder/dir1/".$file_name);
}else{ //rename the file if another one exist
//rename($file_tmp,$new_dir) ;
list($txt,$ext) = explode(".", $file_name);
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$new_dir="../reztrictedfolder/dir1/".$actual_image_name."_copy";
move_uploaded_file($file_tmp,$new_dir);
}
// mysql_query($query);
}else{
// print_r($errors);
}
But it only checks if the file already exists and rename it, but I want to let the user know that file already exists and if he wants to rename the file or replace the existing file..?