How in PHP to change the name of the uploaded file to random so that it does not repeat?
|
2 answers
You can do this: hash (for example, md5) from the current time ( год.день.неделя.минута.секунда
).
- fourThe probability of repetition will still be. It may well be a situation where several users simultaneously upload files. I usually do md5 hash from the value of microtime () and user ID, so safer. - Pavel Vershinin
- the probability of repetition will still be, for this is the essence of md5, I usually store real file names in the database, and on the disk they are stored under the name coinciding with the
id
from the same database. - FLK - @Pavel Vershinin, I agree that you propose this way - more reliably. - vv2cc
- 3I usually used something like: $ randValue = md5 (rand (0,10000). Time ()); // ... - Zowie
|
It is enough to recursively check whether a file with the same name exists and, if it exists, add some nonsense to the file name. something like that
$filename = check($name); function check($filename){ if( file_exists($filename) ){ $filename = check($hash.$filename); }else{ return $filename; } }
- Although such an algorithm makes sense, but definitely not to solve this problem, although if you need to be absolutely sure that there will be no repetition, you can. - Zowie
- depends on $ hash. if you make it good, then the number of recursions will be 0. - Mars Yuldashev
|