I am using the following code to upload set of images into the server, I am renaming the files while uploading, but when I am trying to uploading multiple files its automatically replacing the files, instead I need the other files to be named with 2, 3, 4, etc. in the end of the file name.
<input type="file" name="upl" />
<input type="submit" value="submit" />
$_SESSION['imgrand']
is the random string generated and passed over session.
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
$ext = findexts ($_FILES['upl']['name']) ;
$ran = $_SESSION['imgrand'];
$ran2 = $ran.".";
$target = "uploads/";
$target = $target . $ran2.$ext;
if(move_uploaded_file($_FILES['upl']['tmp_name'], $target))
{
echo "The file has been upl as ".$ran2.$ext;
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
For example if the random string is jahkhakshdkjshdjksahds I need the images should be named as jahkhakshdkjshdjksahds1.ext, jahkhakshdkjshdjksahds2.ext, jahkhakshdkjshdjksahds3.ext, jahkhakshdkjshdjksahds4.ext, jahkhakshdkjshdjksahds5.ext likewise?
test for a existing file before overright.
...
$ext = findexts ($_FILES['upl']['name']) ;
$ran = $_SESSION['imgrand'];
$target = "uploads/".$ran;
for($i=1;$i < 100;$i++) {
$filename = $target.$i.".".$ext;
if (file_exists($filename)) {
continue;
} else {
if(move_uploaded_file($_FILES['upl']['tmp_name'], $filename))
{
echo "The file has been upl as : ".$filename;
break;
}
else
{
echo "Sorry, there was a problem uploading your file: ".$filename;
break;
}
} // not file_ex
} // for
...
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments