I need to upload 4 csv files in one go.
the user browses, selects the files, then on submit I want the code to take each of the 4 files in turn and insert them in a specific table in mysql database.
I know how to do this with one file at a time, but I need to do all 4 at once - any help would be appreciated
at the moment I have very basic code but it takes the first file, inserts it OK then stops.
I get all 4 'sucessful' messages so I know the file is moving, but it doesn;t insert files 2,3, or 4.
session_start ();
$_SESSION['username'] = $username;
require_once ('../mysql_connect.php'); //connect to the database
ini_set('memory_limit', '30M');
// 1- upload user
$uploadDir = '/home/xxx/upload/';
$uploadFile = $uploadDir . $_FILES['User']['name'];
print "<pre>";
//var_dump($_FILES['User']);
$filename = $_FILES['User']['tmp_name'];
$fp = fopen($filename,"rb") or die ('Could Not Open User File!');
while (!feof($fp))
{
$data = fgetcsv($fp,500000);
$UserRef = addslashes($data[0]);
$ConcesRef = addslashes($data[1]);
$ClientRef = addslashes($data[2]);
$CName = addslashes($data[3]);
$CLogIn = addslashes($data[4]);
$CPass = addslashes($data[5]);
$CEmail = addslashes($data[6]);
$AuthorityLevel = addslashes($data[7]);
$Removed = addslashes($data[8]);
$sql = "REPLACE INTO tblUser (UserId, ConcesRef, ClientRef, UName, ULogIn, UPassword, UEmail, AuthorityLevel, Removed)
VALUES ('$UserRef', '$ConcesRef', '$ClientRef', '$CName', '$CLogIn', '$CPass', '$CEmail', '$AuthorityLevel', '$Removed')";
$result = mysql_query($sql)
or die(mysql_error());
$query = "DELETE FROM tblUser WHERE ConcesRef = 0";
$result399 = mysql_query($query)
or die(mysql_error());
}
if (move_uploaded_file($_FILES['User']['tmp_name'], $uploadFile))
{ echo '<div align="center">
<p> </p>
<p> </p>
<p><b><font color="#FF6600" face="Arial, Helvetica, sans-serif" size="4">Thank You, <br> The new User Data has been added to the database</font></b></p>
<p></p></div>';
}
else
{
print "Not Possible To Upload User Data! Please Try Again";
}
print "</pre>";
// 2- upload client
$uploadDir_1 = '/home/xxx/upload/';
$uploadFile_1 = $uploadDir_1 . $_FILES['Client']['name'];
print "<pre>";
//var_dump($_FILES['Client']);
$filename_1 = $uploadFile_1;
$fp_1 = fopen($filename_1,'wb') or die('could not open file!');
while (!feof($fp_1))
{
$data1 = fgetcsv($fp_1,500000);
$ClientId = addslashes($data1[0]);
$ConcesRef = addslashes($data1[1]);
$CoName = addslashes($data1[2]);
$Addr1 = addslashes($data1[3]);
$Addr2 = addslashes($data1[4]);
$Addr3 = addslashes($data1[5]);
$Town = addslashes($data1[6]);
$County = addslashes($data1[7]);
$Postcode = addslashes($data1[8]);
$Removed = addslashes($data1[9]);
$sql_1 = "REPLACE INTO tblClient (ClientId, ConcesRef, CoName, Addr1, Addr2, Addr3, Town, County, Postcode, Removed)
VALUES ('$ClientId', '$ConcesRef', '$CoName', '$Addr1', '$Addr2', '$Addr3', '$Town', '$County', '$Postcode', '$Removed')";
$result1 = mysql_query($sql_1)
or die(mysql_error());
$query = "DELETE FROM tblClient WHERE ConcesRef = 0";
$result499 = mysql_query($query)
or die(mysql_error());
}
if (move_uploaded_file($_FILES['Client']['tmp_name'], $uploadFile_1))
{ echo '<div align="center">
<p><b><font color="#FF0000" face="Arial, Helvetica, sans-serif" size="4">
<p>Client Information Uploaded</p> </font></b></p>
</div>';
}
else
{
print "Not Possible To Upload Client Data! Please Try Again";
}
print "</pre>";
// 3- machine
$uploadDir_2 = '/home/xxx/upload/';
$uploadFile_2 = $uploadDir_2 . $_FILES['Machine']['name'];
print "<pre>";
//var_dump($_FILES['Machine']);
$filename_2 = $uploadFile_2;
$fp_2 = fopen($filename_2,'wb') or die('could not open file!');
while (!feof($fp_2)){
$data_2 = fgetcsv($fp_2,500000);
$MachineId = addslashes($data_2[0]);
$ConcesRef = addslashes($data_2[1]);
$ClientRef = addslashes($data_2[2]);
$ContractRef = addslashes($data_2[3]);
$ReferenceCode = addslashes($data_2[4]);
$CommonName = addslashes($data_2[5]);
$SerialNo = addslashes($data_2[6]);
$TypeRef = addslashes($data_2[7]);
$Removed = addslashes($data_2[8]);
$sql_2 = "REPLACE INTO tblMachine (MachineId, ClientRef, ConcesRef, ContractRef, ReferenceCode, CommonName, SerialNumber, TypeRef, Removed)
VALUES ('$MachineId', '$ClientRef', '$ConcesRef', '$ContractRef', '$ReferenceCode', '$CommonName', '$SerialNo', '$TypeRef', '$Removed')";
$result2 = mysql_query($sql_2)
or die(mysql_error());
$query = "DELETE FROM tblMachine WHERE ConcesRef = 0";
$result599 = mysql_query($query)
or die(mysql_error());
}
if (move_uploaded_file($_FILES['Machine']['tmp_name'], $uploadFile_2))
{ echo '<div align="center">
<b><font color="#FF0000" face="Arial, Helvetica, sans-serif" size="4">
<p> Machine Information Uploaded </P></font></b>
</div>';
}
else
{
print "Not Possible To Upload Machine Data! Please Try Again";
}
print "</pre>";
// 4 - user-machine
$uploadDir_3 = '/home/xxx/upload/';
$uploadFile_3 = $uploadDir_3 . $_FILES['User_Machine']['name'];
print "<pre>";
//var_dump($_FILES['User_Machine']);
$filename_3 = $uploadFile_3;
$fp_3 = fopen($filename_3,'wb') or die('could not open file!');
while (!feof($fp_3)){
$data_3 = fgetcsv($fp_3,500000);
$UserRef = addslashes($data_3[0]);
$ConcesRef = addslashes($data_3[1]);
$ClientRef = addslashes($data_3[2]);
$MachineRef = addslashes($data_3[3]);
$Removed = addslashes($data_3[4]);
$sql_3 = "REPLACE INTO tblUserMachine (UserRef, ClientRef, ConcesRef, MachineRef, Removed)
VALUES ('$UserRef', '$ClientRef', '$ConcesRef', '$MachineRef', '$Removed')";
$result3 = mysql_query($sql_3)
or die(mysql_error());
$query = "DELETE FROM tblUserMachine WHERE ConcesRef = 0";
$result699 = mysql_query($query)
or die(mysql_error());
}
if (move_uploaded_file($_FILES['User_Machine']['tmp_name'], $uploadFile_3))
{ echo '<div align="center">
<b><font color="#FF0000" face="Arial, Helvetica, sans-serif" size="4">
<p> User-Machine Information Uploaded </P></font></b>
</div>';
}
else
{
print "Not Possible To Upload User-Machine Data! Please Try Again";
}
print "</pre>";