1
Hello, I’m trying to generate the filename upado in md5();
, but my file extension does not work at the time of upload
, follow my class:
<?php
class Upload {
private $_SupportedFormats = ['image/jpg', 'image/jpeg', 'image/png'];
private function encryption($str) {
return md5($str);
}
private function verify_file($file) {
if (is_array($file)) {
return true;
} else {
return false;
}
}
private function verify_format($file) {
if ($this->verify_file($file)) {
if (in_array($file['type'], $this->_SupportedFormats)) {
return true;
}
} else {
return false;
}
}
public function upload_file($file) {
if ($this->verify_format($file)) {
move_uploaded_file($file['tmp_name'], FOLDER_UPLOADS . '/' . $this->encryption($file['name']));
return true;
} else {
return false;
}
}
}
This is the name of the file being uploaded: d26f85295e5c2da4634ee2bff0c83e1e
.
I would like to keep it that way, (with the extension)d26f85295e5c2da4634ee2bff0c83e1e.png
.
What’s wrong with my class? I don’t understand if I’m setting $this->encryption($file['name']);
Why is the extension disappearing? I’m in over my head.
The name refers to both the file name and its extension. So you’re turning everything into MD5. You have to go to MD5, then add the extension manually. To know the extension you can explode in the original name and take what is after the last point, or simply give explode, pass the name to MD5 and then implode in the name that turned MD5 + the extension that was separated in the explode... anyway. I think I’ve figured out where the mistake is.
– Clayderson Ferreira
@Claydersonferreira, thanks for the comment, marked my question as duplicate, I’ve reviewed the internet and can not find a solution. Your comment I understood, I tried to blow up but it still didn’t work.
– Guilherme SpinXO
@Guilhermespinxo the solution is exactly what I answered in what was indicated in the lock, just add the extension after the MD5. You have the code line practically ready there. Only use MD5 in place of uniqid. By the way, I see no advantage in you using MD5.
– Bacco
@Bacco, I got it,
move_uploaded_file($file['tmp_name'], FOLDER_UPLOADS . '/' . $this->encryption('img_').pathinfo($file['name'], PATHINFO_EXTENSION
my problem now is to add the point, I used your example exactly 20 minutes ago and I can’t find a place to concatenate the.
but thanks as soon as I get I update the question. my file now has this namede73ef9cd7306f6fff741643eb3423e8png
– Guilherme SpinXO
IMG_ you can take too. Test this:
move_uploaded_file( $file['tmp_name'], FOLDER_UPLOADS . '/' . $this->encryption($file['name']) . '.' . pathinfo($file['name'], PATHINFO_EXTENSION)
- Note that I added the same stitch you made with the bar:. '.' .
– Bacco
Thanks @Bacco worked out... you’re the guy (y ...
– Guilherme SpinXO
@Guilhermespinxo having doubts, ask that we help as much as we can. The important thing is that you understand that they closed as duplicate, is to have the solution in the other, because no one in conscience closes to disturb the user. Mistakes sometimes happen, of course, but in this case, it made sense. Then think about whether you really need to use MD5. The
uniqid()
never repeats (and you don’t need to use the original name to generate the hash, if you want to save the hash in DB).– Bacco
So man I’m using md5 just to leave the names almost standard so let’s just say... not to be that ugly thing. Ah and I was wrong even to say that marked as duplicate, and ended up helping me... :)
– Guilherme SpinXO