note 93658 added to function.move-uploaded-file

From: Date: Mon, 21 Sep 2009 22:27:43 +0000
Subject: note 93658 added to function.move-uploaded-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-160906@lists.php.net to get a copy of this message
Using the function Zarel posted, if the user downloads and uploads a file repeatedly the filename will look something like filename[1][1][1][1].ext This function renames them with a space and round brackets around the number, the same as renaming files on Windows. If the number is already there, it will increment it instead of adding another pair of brackets. You could quite easily change it to have square brackets and no space if you like. <?php function filename_increment($fname) { //Check there is an extension if (strstr($fname, '.')) { $ext=substr($fname,strrpos($fname,'.')); $filename=substr($fname,0,strrpos($fname,'.')); } else $filename=$fname; //Increment the number, or add (2) onto it if there is no number $filename= ((preg_match("/( \([0-9]+\))$/",$filename)) ? preg_replace("/( \(([0-9]+)\))$/e","' ('.('$2'+1).')'",$filename) : $filename." (2)" ); return $filename.$ext; } //Can be used something like this: while (file_exists($dir.$filename)) { $filename=filename_increment($filename); } ?> ---- Server IP: 87.124.86.41 Probable Submitter: 87.112.68.15 (proxied: 87.112.68.15) ---- Manual Page -- http://www.php.net/manual/en/function.move-uploaded-file.php Edit -- https://master.php.net/note/edit/93658 Del: integrated -- https://master.php.net/note/delete/93658/integrated Del: useless -- https://master.php.net/note/delete/93658/useless Del: bad code -- https://master.php.net/note/delete/93658/bad+code Del: spam -- https://master.php.net/note/delete/93658/spam Del: non-english -- https://master.php.net/note/delete/93658/non-english Del: in docs -- https://master.php.net/note/delete/93658/in+docs Del: other reasons-- https://master.php.net/note/delete/93658 Reject -- https://master.php.net/note/reject/93658 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#160906) next »