web 2.0

How to use PHP & Multiple Upload File To MySQL

How to use PHP & Multiple Upload File To MySQL This the guideline/example a php scripts how to use PHP and Multiple Upload file and Insert to MySQL Database.

ShotDev Focus:
- PHP & Multiple Upload file
- PHP & Multiple Upload file and Insert to MySQL

Example 1

php_multiple_upload1.php


<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<form action="php_multiple_upload2.php" method="post" name="form1" enctype="multipart/form-data">
<input type="text" name="txtGalleryName1"><input type="file" name="fileUpload1"><br>
<input type="text" name="txtGalleryName2"><input type="file" name="fileUpload2"><br>
<input type="text" name="txtGalleryName3"><input type="file" name="fileUpload3"><br>
<input type="text" name="txtGalleryName4"><input type="file" name="fileUpload4"><br>
<input name="hdnLine" type="hidden" value="4">
<input name="btnSubmit" type="submit" value="Submit">
</form>
</body>
</html>

php_multiple_upload2.php


<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<?
mysql_connect("localhost","root","root") or die (mysql_error());
mysql_select_db("mydatabase");

for($i=1;$i<=(int)($_POST["hdnLine"]);$i++)
{
if($_FILES["fileUpload".$i]["name"] != "")
{
if(copy($_FILES["fileUpload".$i]["tmp_name"],"shotdev/".$_FILES["fileUpload".$i]["name"]))
{
$strSQL = "INSERT INTO gallery ";
$strSQL .="(GalleryName,Picture) VALUES ('".$_POST["txtGalleryName".$i]."','".$_FILES["fileUpload".$i]["name"]."')";
mysql_query($strSQL);
echo "Copy/Upload ".$_FILES["fileUpload".$i]["name"]." completed.<br>";
}
}
}

echo "<br><a href='php_multiple_upload3.php'>View file</a>";

mysql_close();
?>
</body>
</html>

php_multiple_upload3.php


<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<?
mysql_connect("localhost","root","root") or die(mysql_error());
mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM gallery";
$objQuery = mysql_query($strSQL);
echo"<table border=\"0\"  cellspacing=\"1\" cellpadding=\"1\"><tr>";
$intRows = 0;
while($objResult = mysql_fetch_array($objQuery))
{
echo "<td>";
$intRows++;
?>
<center>
<img src="shotdev/<?=$objResult["Picture"];?>"><br>
<?=$objResult["GalleryName"];?>
<br>
</center>
<?
echo"</td>";
if(($intRows)%2==0)
{
echo"</tr>";
}
}
echo"</tr></table>";
?>
</body>
</html>
<?
mysql_close();
?>

Create a php file and save to path root-path/myphp/

Run
http://localhost/myphp/php_multiple_upload1.php

Screenshot

PHP Multiple Upload file to MySQL

PHP Multiple Upload file to MySQL

PHP Multiple Upload file to MySQL
.

Example 2 (Auto Create Element and Display of Pagination)

php_multiple_upload4.php


<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<script language="javascript">
function fncCreateElement(){

var mySpan = document.getElementById('mySpan');

var myLine = document.getElementById('hdnLine');
myLine.value++;

// Create input text
var myElement1 = document.createElement('input');
myElement1.setAttribute('type',"text");
myElement1.setAttribute('name',"txtGalleryName"+myLine.value);
myElement1.setAttribute('id',"txt"+myLine.value);
mySpan.appendChild(myElement1);

// Create input file
var myElement2 = document.createElement('input');
myElement2.setAttribute('type',"file");
myElement2.setAttribute('name',"fileUpload"+myLine.value);
myElement2.setAttribute('id',"fil"+myLine.value);
mySpan.appendChild(myElement2);

// Create <br>
var myElement3 = document.createElement('<br>');
myElement3.setAttribute('id',"br"+myLine.value);
mySpan.appendChild(myElement3);
}

function fncDeleteElement(){

var mySpan = document.getElementById('mySpan');

var myLine = document.getElementById('hdnLine');

if(myLine.value > 1 )
{

// Remove input text
var deleteFile = document.getElementById("txt"+myLine.value);
mySpan.removeChild(deleteFile);

// Remove input file
var deleteFile = document.getElementById("fil"+myLine.value);
mySpan.removeChild(deleteFile);

// Remove <br>
var deleteBr = document.getElementById("br"+myLine.value);
mySpan.removeChild(deleteBr);

myLine.value--;
}
}
</script>
<body>
<form action="php_multiple_upload5.php" method="post" name="form1" enctype="multipart/form-data">
<input type="text" name="txtGalleryName1"><input type="file" name="fileUpload1">
<input name="btnCreate" type="button" value="+" onClick="JavaScript:fncCreateElement();">
<input name="btnDelete" type="button" value="-" onClick="JavaScript:fncDeleteElement();"><br>
<span id="mySpan"></span>
<input name="hdnLine" type="hidden" value="1">
<input name="btnSubmit" type="submit" value="Submit">
</form>
</body>
</html>

php_multiple_upload5.php


<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<?
mysql_connect("localhost","root","root") or die (mysql_error());
mysql_select_db("mydatabase");

for($i=1;$i<=(int)($_POST["hdnLine"]);$i++)
{
if($_FILES["fileUpload".$i]["name"] != "")
{
if(copy($_FILES["fileUpload".$i]["tmp_name"],"shotdev/".$_FILES["fileUpload".$i]["name"]))
{
$strSQL = "INSERT INTO gallery ";
$strSQL .="(GalleryName,Picture) VALUES ('".$_POST["txtGalleryName".$i]."','".$_FILES["fileUpload".$i]["name"]."')";
mysql_query($strSQL);
echo "Copy/Upload ".$_FILES["fileUpload".$i]["name"]." completed.<br>";
}
}
}

echo "<br><a href='php_multiple_upload6.php'>View file</a>";

mysql_close();
?>
</body>
</html>

php_multiple_upload6.php


<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<?
mysql_connect("localhost","root","root") or die(mysql_error());
mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM gallery";
$objQuery = mysql_query($strSQL);
$Num_Rows = mysql_num_rows($objQuery);

$Per_Page = 4;   // Per Page

$Page = $_GET["Page"];
if(!$_GET["Page"])
{
$Page=1;
}

$Prev_Page = $Page-1;
$Next_Page = $Page+1;

$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{
$Num_Pages =1;
}
else if(($Num_Rows % $Per_Page)==0)
{
$Num_Pages =($Num_Rows/$Per_Page) ;
}
else
{
$Num_Pages =($Num_Rows/$Per_Page)+1;
$Num_Pages = (int)$Num_Pages;
}

$strSQL .=" order  by GalleryID ASC LIMIT $Page_Start , $Per_Page";
$objQuery  = mysql_query($strSQL);

echo"<table border=\"0\"  cellspacing=\"1\" cellpadding=\"1\"><tr>";
$intRows = 0;
while($objResult = mysql_fetch_array($objQuery))
{
echo "<td>";
$intRows++;
?>
<center>
<img src="shotdev/<?=$objResult["Picture"];?>"><br>
<?=$objResult["GalleryName"];?>
<br>
</center>
<?
echo"</td>";
if(($intRows)%2==0)
{
echo"</tr>";
}
}
echo"</tr></table>";
?>

<br>
Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :
<?
if($Prev_Page)
{
echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> ";
}

for($i=1; $i<=$Num_Pages; $i++){
if($i != $Page)
{
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
}
else
{
echo "<b> $i </b>";
}
}
if($Page!=$Num_Pages)
{
echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>Next>></a> ";
}
?>
</body>
</html>
<?
mysql_close();
?>

Create a php file and save to path root-path/myphp/

Run
http://localhost/myphp/php_multiple_upload4.php

Screenshot

PHP Multiple Upload file to MySQL

PHP Multiple Upload file to MySQL

PHP Multiple Upload file to MySQL

.
.
.

Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (5 votes, average: 6.00 out of 10)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.