I would like to rename the filename with a hidden additional form field followed by a small unique random number while uploading the photo. The file name should look like this: hidden_form_field-random_number.jpg = hello_Dj84lx.jpg
I have found solutions to do random names and so on but not with an inserted form. I have tried to use $file->title from the examples but without success.
I managed to solve the problem.
First of I added this function to the upload/server/php/index.php file
protected function generate_unique_filename($filename = ""){
$extension = "";
if ( $filename != "" ){
$extension = pathinfo($filename , PATHINFO_EXTENSION);
if ( $extension != "" ){
$extension = "." . $extension;
}
}
$characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$charactersLength = strlen($characters);
$randomString = "";
for ($i = 0; $i < 5 ; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $filename.'_'.date('siHdmy').$randomString.$extension;
}
Then I edited the UploadHandler.php file. I added a public variable $mytitle and replaced this code:
$file->name = $this->get_file_name($uploaded_file, $name, $size, $type, $error, $index, $content_range);
with this
$this->mytitle='what-ever-text-filled-in-the-title-form-field';
$file->name = $this->generate_unique_filename($this->mytitle);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.