1

I'm trying to upload multi images to server and store their names into the mysql db. I can't figure why the script is appending 0, 1, 2... at the beginning of each image name. And why renaming doesn't work, if I upload a image with the same name it replaces is.

$images = array();
if(isset($_FILES['files'])){
    $errors= array();
  foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
    $file_name = $key.$_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';
        } 
        $desired_dir="images";
        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0700);    // Create directory if it does not exist
            }
            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"images/".$file_name);
            }else{                  //rename the file if another one exist
                $new_dir="images/".$file_name.time();
                 rename($file_tmp,$new_dir) ;       
            }
            $images[] = $file_name;     
        }else{
                print_r($errors);
        }
    }
  if(empty($error)){
    $imglinks = implode(" | ", $images);
  }
  echo $imglinks;
}
1
  • 1
    This line cause $file_name = $key.$_FILES['files']['name'][$key]; appending 0, 1, 2... at the beginning Commented Mar 28, 2016 at 8:16

1 Answer 1

2

$key was concatenated to the filename

$images = array();
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';
        } 
        $desired_dir="images";
        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0700);    // Create directory if it does not exist
            }
            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"images/".$file_name);
            }else{                  //rename the file if another one exist
                $new_dir="images/".$file_name.time();
                 rename($file_tmp,$new_dir) ;       
            }
            $images[] = $file_name;     
        }else{
                print_r($errors);
        }
    }
  if(empty($error)){
    $imglinks = implode(" | ", $images);
  }
  echo $imglinks;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.