Reputation: 2031
in my form I'm trying to upload multiple or single file using php. So my html table is look like this :
<tr>
<td width="142"><b>Docs</b>
<td width="142"><input type="file" name="files[]" id="project_docs1" class="docfile" /></td>
<td width="142"><input type="file" name="files[]" id="project_docs2" class="docfile" /></td>
<td width="142"><input type="file" name="files[]" id="project_docs3" class="docfile" /></td>
</td>
Now when I upload only one file it's showing me error message like : Invalid Format but it's should be accept one file. it's not require to must be upload all 3 files. Can you tell me why it's showing this error message called Invalid Format ? If upload all 3 files then it's working fine.
and when I press the upload button without upload any file it's showing me value 1 for $noOfUpload
variable. why ?
$valid_formats = array("jpg", "png", "gif", "txt", "bmp");
$max_file_size = 1024*100; //100 kb
$path = "project_docs"; // Upload directory
$error = array(); // load error message
$files_name = array(); // get uploaded file name
foreach ($_FILES['files']['name'] as $key => $name) {
$size = $_FILES['files']['size'][$key]. "<br>";
$noOfUpload = count($name);
if($noOfUpload <= 0){
$error[] = "Upload your document/s<br>";
}elseif(!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){
$error[] = "invalid file formate : $name<br>";
}
//$name = md5(uniqid()) . '-' . htmlspecialchars_decode($name);
$files_name[] = "$name";
}
if(!empty($error)){
foreach ($error as $e) {
echo "<div class='error'>$e</div>";
}
}else{
foreach ($files_name as $fn) {
echo "$fn<br>";
}
}
You help is more appreciate. :)
Upvotes: 0
Views: 269
Reputation: 2128
Try this
if (isset($_FILES))
{
$valid_formats = array("jpg", "png", "gif", "txt", "bmp");
for ($i = 0; $i < count($_FILES['files']['name']); $i++)
{
if (in_array(pathinfo($FILES['files']['name'][$i], PATHINFO_EXTENSION), $valid_formats))
{
$tmp_path = $_FILES['files']['tmp_name'][$i];
if ($tmp_path != "")
{
if (move_uploaded_file($tmp_path, $new_path))
{
//Handle other code here
}
else
{
$error[] = ""; //your error handling
}
}
else
{
$error[] = ""; //your error handling
}
}
else
{
$error[] = "invalid file formate : $name<br>";
}
}
}
Upvotes: 1
Reputation: 473
<tr>
<td width="142"><b>Docs</b>
<td width="142"><input type="file" name="files[]" id="project_docs1" class="docfile" /></td>
</tr>
PHP Code:
$valid_formats = array("jpg", "png", "gif", "txt", "bmp");
$max_file_size = 1024*100; //100 kb
$path = "project_docs"; // Upload directory
$error = array(); // load error message
$files_name = array(); // get uploaded file name
foreach ($_FILES['files']['name'] as $key => $name) {
$size = $_FILES['files']['size'][$key]. "<br>";
$noOfUpload = count($name);
if($noOfUpload <= 0){
$error[] = "Upload your document/s<br>";
}elseif(!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){
$error[] = "invalid file format : $name<br>";
}
//$name = md5(uniqid()) . '-' . htmlspecialchars_decode($name);
$files_name[] = "$name";
}
if(!empty($error)){
foreach ($error as $e) {
echo "<div class='error'>$e</div>";
}
}else{
foreach ($files_name as $fn) {
echo "$fn<br>";
}
}
Upvotes: 0
Reputation: 146
Your code is run for whole array either it's blank or not. First, count your array then run your code upto that count.
Upvotes: 0