How to Rename Uploaded File Before Saving It into a Directory

How to rename uploaded file before saving it into a directory?

You can simply change the name of the file by changing the name of the file in the second parameter of move_uploaded_file.

Instead of

move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $_FILES["file"]["name"]);

Use

$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);

Changed to reflect your question, will product a random number based on the current time and append the extension from the originally uploaded file.

How to rename uploaded file before saving it into a directory in PHP?

You can do this

public function run() {
$total = count($this->files);
for($i=0; $i<$total; $i++) {
if (empty($this->files['name'][$i]) === false) {

$tempName = explode(".", $this->files['name'][$i]);
$newName = "IMAGE_NAME"; //generate unique string to avoid conflict
$newfilename = $newName . '.' . end($tempName );
if (move_uploaded_file($this->files['tmp_name'][$i], $this->store_directory.'/'.$newfilename) == false) {
$this->errors['type'] = "run";
$this->errors['file_name'] = $this->files['name'][$i];
}
}
}

return empty($this->errors);
}

In $newName you can generate new name for your file.

Update

To avoid conflict use unique string each time when you generate the name.

php rename uploaded file before upload and overwrite if already exists

To add your custom name:

if($UploadOk == true){  
$name = "foobar.csv";
move_uploaded_file($temp,$UploadFolder."/".$name);
array_push($uploadedFiles, $name);
}

For single file, remove multiple="multiple":

<input type="file" name="files[]" />

rename file to specific name before upload php

$targetfolder = $targetfolder   . basename( $_FILES['file']['name']) ;

When you are creating the $targetfolder variable, you are adding the base name to it for some reason. Thus if the file was called something.jpg and the target folder was /a/b/c/ you'll get /a/b/c/something.jpg as the value of $targetfolder.

Later in your code you add your own file name again:

if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder .$filename))

So I think you'll be happy if you remove $targetfolder = $targetfolder . basename( $_FILES['file']['name']) ; all together.

Rename file Which is already uploaded

You might want to take a different approach here: Maybe upload the files and rename them to something convenient after upload, for example: id of table's row, and store the name of the file in your MySQL table (add a filename column), which the user can change.
Then when the user downloads the file, send the name stored in the database with the Content-disposition header.

On upload:

$filename = mysqli_real_escape_string($db, basename($_FILES["pdf"]["name"]));

if (mysqli_query($db, "INSERT INTO tables (pdf) VALUES ('$filename')")) {
$id = mysqli_insert_id($db);
$folder = "files/" . $id;
move_uploaded_file($_FILES["pdf"]["tmp_name"], $folder);
}

When updating the filename, you will do something like this:

UPDATE tables SET pdf = '$newName' WHERE id = $id

(Don't forget to escape the input and sanitize the filename)

When serving the file to the user (user downloads the file)

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="' . $row["filename"] . '"');
readfile('files/' . $row["id"]);

php rename 2 uploaded files to specific names before upload

You need to use the array of $_FILES['files']['tmp_name']. For example:

if ( $UploadOk == true ) {
$name = "foobar.csv";
$name2 = "foobar2.csv";
move_uploaded_file($_FILES['files']['tmp_name'][0], $UploadFolder."/".$name);
move_uploaded_file($_FILES['files']['tmp_name'][1], $UploadFolder."/".$name2);
}


Related Topics



Leave a reply



Submit