Page 1 of 1
rename uploaded file if has a space in it
Posted: Mon Jan 26, 2009 11:51 am
by Blondy
hi guys I has written a script to resize uploaded images
but I've a problem with images has a space in it's name
how I can detect if the file has a space in it's name or not
Re: rename uploaded file if has a space in it
Posted: Mon Jan 26, 2009 11:54 am
by John Cartwright
Code: Select all
if (strstr('does this have a space in it?', ' ')) {
echo 'yes';
}
Otherwise, you can simple pass all your filenames through a str_replace(),
Code: Select all
$filename = str_replace(' ', '', $filename);
Re: rename uploaded file if has a space in it
Posted: Mon Jan 26, 2009 12:41 pm
by Blondy
thanks,
can you help me create a loop to find an available name for saving afile
something like this
Code: Select all
if(file_exist($filename){
while (file_exist("$filename){
$count=$count+1;
$filename="".$filename."".$count."";
}}
Re: rename uploaded file if has a space in it
Posted: Mon Jan 26, 2009 1:01 pm
by John Cartwright
It is usually better to store the md5 hash of the filename, to greatly reduce the number of namespace collissions. I then store the filename in the database relating to the md5 hash, which then allows us to have multiple files with the same (original) filename.
If you insist though, completely untested.
Code: Select all
$filename = 'checkexists.jpg'; //file we want to save with unique name
$extension = array_pop(explode('.', $filename)); //get the extension
$basefilename = basename($filename, '.'. $extension); //get filename without extension
$count = 0;
do
{
$count += 1;
$checkfilename = $basefilename . $count .'.'. $extension //reassemble the filename
}
while (file_exist($checkfilename));
echo $checkfilename;