-1

Hello I want to change files name by adding 3 random letters to it.

$target_dir = "/var/www/html/uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$kiekis = htmlspecialchars($_POST['laikotarpis']);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$rpavadinimas = htmlspecialchars($_POST['rpavadinimas']);
$datanuo =  $datanuo = date("Y-m-d");
$time = strtotime($datanuo);
$dataiki = date("Y-m-d", strtotime("+$kiekis month", $time));
$Failo_vardas = htmlspecialchars(basename($_FILES["fileToUpload"]["name"]));

if ($imageFileType != "mp4" && $imageFileType != "avi" && $imageFileType != "mov" && $imageFileType != "3gp" && $imageFileType != "mpeg"){
    $error_message = "<span style='color:red';><b>Failo ". basename( $_FILES["fileToUpload"]["name"]). " Netinkamas failo formatas</b></span>";
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)){
        echo "success";
    }
}

As example I want to upload file "test.mp4", but I want to save it as "testA2Q.mp4" Any solution how to solve it?

EDIT: I want to save it with the changed name as well

2 Answers2

0

You can do something like this

    $info = pathinfo($_FILES['userFile']['name']);
    $ext = $info['extension']; // get the extension of the file
    $newname = "newname.".rand(0,999).$ext;
    $target = 'images/'.$newname;
    move_uploaded_file( $_FILES['userFile']['tmp_name'], $target);

Here

$newname = "newname.".rand(0,999).$ext;

rand(0,999)will make a name with three random number between 0-999. I thing this will help you.

You can check details about pathinfo and rand within official documentation.

Bhavik Shah
  • 2,300
  • 1
  • 17
  • 32
Bibhudatta Sahoo
  • 4,808
  • 2
  • 27
  • 51
-1

Try this way

$target_dir = "/var/www/html/uploads/";

$old_file = 'test.mp4'; //sample
$new_name = 'testA2Q'; //sample
$type = substr(strrchr($old_file, "."), 1);
$new_file_name = $new_name.'.'.$type;

rename ($target_dir.$old_file, $target_dir.$new_file_name); //rename a file
Abdullah Al Shakib
  • 2,034
  • 2
  • 15
  • 16