upload php auto rename


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. // Usage: uploadfile($_FILE['file']['name'],'temp/',$_FILE['file']['tmp_name'])
  3. function uploadfile($origin, $dest, $tmp_name)
  4. {
  5. $origin = strtolower(basename($origin));
  6. $fulldest = $dest.$origin;
  7. $filename = $origin;
  8. for ($i=1; file_exists($fulldest); $i++)
  9. {
  10. $fileext = (strpos($origin,'.')===false?'':'.'.substr(strrchr($origin, "."), 1));
  11. $filename = substr($origin, 0, strlen($origin)-strlen($fileext)).'['.$i.']'.$fileext;
  12. $fulldest = $dest.$newfilename;
  13. }
  14.  
  15. if (move_uploaded_file($tmp_name, $fulldest))
  16. return $filename;
  17. return false;
  18. }
  19. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.