Jess McKenzie
Jess McKenzie

Reputation: 8385

Changing file names on upload

I currently have a form with 2x name=userfile[] attributes in the inputs that is handled within the code below. What would be the best way to enable me to rename the filenames foreach file on upload - I want them to be specific to the input

What I am after:

$imageOneName = img1.$var;
$imageTwoName = img2.$var;

Code:

for($i=0; $i<count($_FILES['userfile']['name']); $i++) {
  //Get the temp file path
  $tmpFilePath = $_FILES['userfile']['tmp_name'][$i];

  //Make sure we have a filepath
  if ($tmpFilePath != ""){
    //Setup our new file path
    $newFilePath = $local_path .'images/' . $_FILES['userfile']['name'][$i];


    //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {

      //Handle other code here

    }
  }
}

Upvotes: 0

Views: 72

Answers (3)

user5051310
user5051310

Reputation:

Instead of

<input type="file" name="userfile[]" id="input1">
<input type="file" name="userfile[]" id="input2">

You can do the following to distinguish between the two

<input type="file" name="userfile[desiredNameOfFile1]" id="input1">
<input type="file" name="userfile[desiredNameOfFile2]" id="input2">

With PHP handling it like this:

foreach($_FILES['userFile']['name'] AS $desiredNameOfFile => $fileInfo) {
  //Get the temp file path
  $tmpFilePath = $_FILES['userfile']['tmp_name'][$desiredNameOfFile];

  //Make sure we have a filepath
  if ($tmpFilePath != ""){
    //Setup our new file path
    $newFilePath = $local_path .'images/' . $desiredNameOfFile . pathInfo($_FILES['userfile']['tmp_name'][$desiredNameOfFile],PATHINFO_EXTENSION);


    //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {

      //Handle other code here

    }
  }
}

Be aware: this code will overwrite files that already have that name

Edit

If you want multiple file selects

<input type="file" name="userfile[desiredNameOfFile1][]" id="input1" multiple>
<input type="file" name="userfile[desiredNameOfFile2][]" id="input2" multiple>

Php

foreach($_FILES['userfile']['name'] AS $desiredNameOfFile => $fileInfo) {
    for($i = 0; $i < count($fileInfo); $i++) {
    //Get the temp file path
        $tmpFilePath = $_FILES['userfile']['tmp_name'][$desiredNameOfFile][$i];

        // Make sure we have a filepath
        if ($tmpFilePath != ""){
            // Setup our new file path
            $newFilePath = $local_path .'images/' . $desiredNameOfFile . $i . pathInfo($_FILES['userfile']['tmp_name'][$desiredNameOfFile][$i],PATHINFO_EXTENSION);


            // Upload the file into the temp dir
            if(move_uploaded_file($tmpFilePath, $newFilePath)) {

                // Handle other code here

            }
        }
    }
}
}

Upvotes: 1

Hussy Borad
Hussy Borad

Reputation: 583

With below code generate unique file name for each file.

$file_name = preg_replace('/\s+/', '', $_FILES['userfile']['name'][$i]); /// remove unexpected symbols , number 
$path[$i]="image/".time().$i.$file_name; /// generate unique name
move_uploaded_file($file_tmp[$i],$path[$i]); /// move that file on your path folder

Upvotes: 1

Abhishek Sharma
Abhishek Sharma

Reputation: 6661

try this code :-

$extension = pathinfo($_FILES['userfile']['name'][$i], PATHINFO_EXTENSION); //Get extension of image
$new= rand(0000,9999); //creat random name
$file_name=$new.'.'.$extension; //create file name with extension
$newFilePath = $local_path .'images/' . $file_name;

Upvotes: 1

Related Questions