I am using the Amazon S3 API to upload files and I am changing the name of the file each time I upload.
So for example:
Dog.png > 3Sf5f.png
Now I got the random part working as such:
function rand_string( $length ) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$size = strlen( $chars );
for( $i = 0; $i < $length; $i++ ) {
$str .= $chars[ rand( 0, $size - 1 ) ];
}
return $str;
}
So I set the random_string to the name parameter as such:
$params->key = rand_string(5);
Now my problem is that this wont show any extension. So the file will upload as 3Sf5f
instead of 3Sf5f.png
.
The variable $filename gives me the full name of the file with its extension.
If I use $params->key = rand_string(5).'${filename}';
I get:
3Sf5fDog.png
So I tried to retrieve the $filename extension and apply it. I tried more than 30 methods without any positive one.
For example I tried the $path_info(), I tried substr(strrchr($file_name,'.'),1); any many more. All of them give me either 3Sf5fDog.png
or just 3Sf5f
.
An example of what I tried:
// As @jcinacio pointed out. Change this to:
//
// $file_name = "${filename}";
//
$file_name = '${filename}' // Is is wrong, since '..' does not evaluate
$params->key = rand_string(5).$file_name;
=
3Sf5fDog.png
.
$file_name = substr(strrchr('${filename}', '.'), 1);
$params->key = rand_string(5).$file_name;
=
3Sf5f
.
$filename = "example.png" // If I declare my own the filename it works.
$file_name = substr(strrchr('${filename}', '.'), 1);
$params->key = rand_string(5).$file_name;
=
3Sf5f.png
The entire class file: http://pastebin.com/QAwJphmW (there are no other files for the entire script).
What I'm I doing wrong? This is really frustrating.