I’m new php, It’s learning process.
I’ve a html upload form. I’m trying to rename the uploaded file. So Following is php code that get the file extension and after that i add a php uniqid(); to rename it.
Get file extension code:
$img = $_FILES['images']['name'];
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$extension = getExtension($img);
$id = uniqid();
$newimg = $id.'.'.$extension;
Well, this OK for me for only one upload file, BUT if I’ve multiple upload form then how do i get multiple files extension with this function?
For get the multiple files following is my php code:
$uploaded_files = array();
$upload_directory = dirname(__file__) . '/uploaded/'; //set upload directory
for ($i = 0; $i < count($_FILES['images']['name']); $i++)
{
$number_of_file_fields++;
if ($_FILES['images']['name'][$i] != '')
{ //check if file field empty or not
$number_of_uploaded_files++;
$uploaded_files[] = $_FILES['images']['name'][$i];
if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i]))
{
$number_of_moved_files++;
}
}
}
Html Form
<table width="1020" border="0" cellspacing="7" cellpadding="0">
<tr>
<td valign="top" width="250">
Upload your property picture
</td>
<td>
<div id="file_container">
<input name="images[]" type="file" class="tr2" />
<br />
</div>
<a href="javascript:void(0);" onClick="add_file_field();">Add another</a>
<input type="submit" value="Save & Continue" name="Submit" class="submit" />
</td>
</tr>
</table>
Here is a demonstration: http://ideone.com/gPi61D