Posted By

smilylion on 09/09/06


Tagged

upload


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

blakeb
skywalker


upload php auto rename


 / Published in: PHP
 

  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  

You need to login to post a comment.