Sunday, May 15, 2011

upload multiple using php script

<title>Untitled Document</title>

<script language="javascript" >

function addFile()

{

table =document.getElementById("tabFileup");

var count=table.rows.length;

var row=table.insertRow(count-1);

var cell1=row.insertCell(0);

cell1.innerHTML = "<div align='center'>Upload file</div>";

/*var cell2=row.insertCell(1);

var ele=document.createElement("input");

ele.type="file";

ele.name="fileImage[]";

cell2.appendChild(ele);*/

var cell2=row.insertCell(1);

cell2.innerHTML = " <td colspan='2'><input name='fileImage[]' type='file' /><a href='#' onclick='addFile();'>add another</a></td>";





alert(count);



}

</script>

</head>
<body>

<form action="uploadFileAction.php" method="post" enctype="multipart/form-data" name="frmUpload">

<table id="tabFileup" align="center" width="80%" border="0" cellpadding="0" cellspacing="0">

<tr>

<td width="45%"><div align="center">Server</div></td>

<td width="55%"><input name="txtServer" type="text" /></td>

</tr>

<tr>

<td><div align="center">UserName</div></td>

<td colspan="2"><input name="txtUser" type="text" /></td>

</tr>

<tr>

<td><div align="center">Password</div></td>

<td colspan="2"><input name="txtPass" type="password" /></td>

</tr>

<tr>

<td><div align="center">Upload file</div></td>

<td colspan="2"><input name="fileImage[]" type="file" /><a href="#" onclick="addFile();">add another</a></td>

</tr>

<tr>

<td>

<div align="center">

<input name="btnSubmit" type="submit" value="submit" />

</div></td>

<td colspan="2"><input name="btnReset" type="reset" value="Reset" /></td>

</tr>

</table>
</form>

</body>

<?php
    if(isset($_FILES['file']['tmp_name']))
    {
        // Number of uploaded files
        $num_files = count($_FILES['file']['tmp_name']);

        /** loop through the array of files ***/
        for($i=0; $i < $num_files;$i++)
        {
            // check if there is a file in the array
            if(!is_uploaded_file($_FILES['file']['tmp_name'][$i]))
            {
                $messages[] = 'No file uploaded';
            }
            else
            {
                // copy the file to the specified dir 
                if(@copy($_FILES['file']['tmp_name'][$i],$upload_dir.'/'.$_FILES['file']['name'][$i]))
                {
                    /*** give praise and thanks to the php gods ***/
                    $messages[] = $_FILES['file']['name'][$i].' uploaded';
                }
                else
                {
                    /*** an error message ***/
                    $messages[] = 'Uploading '.$_FILES['file']['name'][$i].' Failed';
                }
            }
        }
    }
?>

Bind Some Event to Element and trigger that function when click the element

Scenario: Suppose if we used same click event function in various web pages. if you want do some logic some page button for that need to re...