Yong Scott
asked on
I upload file from directory to database but i realise that all file i upload is the same.
How to fix this .. can upload the directory but all the csv file content i upload was the same file ..
Actually there are 30 files , but one file uploaded 30 times.
Here are my code for upload part .. upload.php
Actually there are 30 files , but one file uploaded 30 times.
Here are my code for upload part .. upload.php
<?php
include 'config.php';
if(isset($_POST['btn-upload']))
{
$log_directory = '/xampp/htdocs/mydoc';
$results_array = array();
if ($handle = opendir($log_directory))
{
while(($file = readdir($handle)) !== FALSE)
{
if ($file != "." && $file != "..") {
$results_array[] = $file;
}
}
closedir($handle);
}
//Output findings
foreach($results_array as $value)
{
$csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes)){
if(is_uploaded_file($_FILES['file']['tmp_name'])){
//open uploaded csv file with read only mode
$csvFile = fopen($_FILES['file']['tmp_name'], 'r');
//skip first line
fgetcsv($csvFile);
//parse data from csv file line by line
while(($line = fgetcsv($csvFile)) !== FALSE){
//insert member data into database
$db->query("INSERT INTO nov(f_name,name, cpu_count, memory_size, disk_space_size, nic_count, power_state) VALUES ('".$value."','".$line[0]."','".$line[1]."','".$line[2]."','".$line[3]."','".$line[4]."','".$line[5]."')");
}
}
//close opened csv file
fclose($csvFile);
if(move_uploaded_file)
{
?>
<script>
alert('successfully uploaded');
window.location.href='index.php?success';
</script>
<?php
}
else
{
?>
<script>
alert('error while uploading file');
window.location.href='index.php?fail';
</script>
<?php
}
}
}
}
?>
ASKER
<?php
include 'config.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>File Uploading With PHP and MySql</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<label>File Uploading With PHP and MySql</label>
</div>
<br /><br />
<form action="index.php" method="post" enctype="multipart/form-data">
<button type="submit" name="btn-main" style="height: 25px; width: 200px">MAIN PAGE</button>
</form>
<div id="body">
<table style = "border-collapse:collapse">
<tr>
</tr>
<tr>
<thead>
<tr>
<th style = "width:20px;border:1px solid black" align = "center">Date</th>
<th style = "width:20px;border:1px solid black" align = "center">File Name</th>
<th style = "width:20px;border:1px solid black" align = "center">Site</th>
<th style = "width:20px;border:1px solid black" align = "center">Name</th>
<th style = "width:20px;border:1px solid black" align = "center">CPU Count</th>
<th style = "width:20px;border:1px solid black" align = "center">Memory Size</th>
<th style = "width:20px;border:1px solid black" align = "center">Disk Space Size</th>
<th style = "width:20px;border:1px solid black" align = "center">NIC Count</th>
<th style = "width:20px;border:1px solid black" align = "center">Power State</th>
<th style = "width:20px;border:1px solid black" align = "center">Memory Size(inGB)</th>
<th style = "width:20px;border:1px solid black" align = "center">Disk Space Size(inGB)</th>
</tr>
</thead>
<tbody>
<?php
//get records from database
$query = $db->query("SELECT * FROM nov");
if($query->num_rows > 0){
while($row = $query->fetch_assoc()){ ?>
<tr>
<td style = "width:20px;border:1px solid black" align = "center"><?php echo date('m-d-y', strtotime($row['date'])); ?></td>
<td style = "width:20px;border:1px solid black" align = "center"><?php echo $row['f_name']; ?></td>
<td style = "width:20px;border:1px solid black" align = "center"><?php echo $row['site']; ?></td>
<td style = "width:20px;border:1px solid black" align = "center"><?php echo $row['name']; ?></td>
<td style = "width:20px;border:1px solid black" align = "center"><?php echo $row['cpu_count']; ?></td>
<td style = "width:20px;border:1px solid black" align = "center"><?php echo $row['memory_size']; ?></td>
<td style = "width:20px;border:1px solid black" align = "center"><?php echo $row['disk_space_size']; ?></td>
<td style = "width:20px;border:1px solid black" align = "center"><?php echo $row['nic_count']; ?></td>
<td style = "width:20px;border:1px solid black" align = "center"><?php echo $row['power_state']; ?></td>
<td style = "width:20px;border:1px solid black" align = "center"><?php echo $row['memory_size_ingb']; ?></td>
<td style = "width:20px;border:1px solid black" align = "center"><?php echo $row['disk_space_size_ingb']; ?></td>
</tr>
<?php } }else{ ?>
<tr><td style = "width:20px;border:1px solid black" align = "center" colspan="12">No data(s) found.....</td></tr>
<?php } ?>
</table>
</div>
</body>
</html>
ASKER
That is my html code
ASKER
Do you know how to not target directory can upload file which mean all directory can access no specific directory?
Where is your file input control?
ASKER
<font size="5">Directory Upload</font>
<br /><br />
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" webkitdirectory mozdirectory name="file" />
<button type="submit" name="btn-upload" style="height: 25px; width: 100px">UPLOAD</button>
</form>
webkitdirectory and mozdirectory are non-standard attributes - they are not part of the HTML5 standard and are not supported in all browsers.
Do you know how to not target directory can upload file which mean all directory can access no specific directory?I have no idea what this is saying
ASKER
webkitdirectory and mozdirectory are non-standard attributes - they are not part of the HTML5 standard and are not supported in all browsers.Its can work in my browser , only problem is the directory file uploaded but all file is the same
ASKER
Actually I want to upload folder that contains of csv files , but the files have different data inside , so how can I display it all
ASKER
Unfortunately, it only works for a single file insertion. I have further tried to edit it, but it just inserts the last file that is in the directory. Any help will be much appreciated.
Where is your code that iterates over the files variable to get all the files and save them
When you submit multiple files they are put in an array that looks like this
I have already given you the process for iterating over this array in this comment (in your other question)
https://www.experts-exchange.com/questions/29078470/How-to-import-directory-and-echo-the-filename.html?anchorAnswerId=42435985#a42435985
When you submit multiple files they are put in an array that looks like this
Array
(
[files] => Array
(
[name] => Array
(
[0] => 01.csv
[1] => 02.csv
[2] => 03.csv
[3] => 04.csv
[4] => 05.csv
)
[type] => Array
(
[0] => application/vnd.ms-excel
[1] => application/vnd.ms-excel
[2] => application/vnd.ms-excel
[3] => application/vnd.ms-excel
[4] => application/vnd.ms-excel
)
[tmp_name] => Array
(
[0] => C:\Windows\Temp\php5FEB.tmp
[1] => C:\Windows\Temp\php5FEC.tmp
[2] => C:\Windows\Temp\php5FED.tmp
[3] => C:\Windows\Temp\php5FEE.tmp
[4] => C:\Windows\Temp\php5FEF.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
)
[size] => Array
(
[0] => 170516
[1] => 116832
[2] => 2351
[3] => 275819
[4] => 1870
)
)
)
I have already given you the process for iterating over this array in this comment (in your other question)
https://www.experts-exchange.com/questions/29078470/How-to-import-directory-and-echo-the-filename.html?anchorAnswerId=42435985#a42435985
ASKER
ArraySo I need list down the file for each array?
(
[files] => Array
(
[name] => Array
(
[0] => 01.csv
[1] => 02.csv
[2] => 03.csv
[3] => 04.csv
[4] => 05.csv
)
[type] => Array
(
[0] => application/vnd.ms-excel
[1] => application/vnd.ms-excel
[2] => application/vnd.ms-excel
[3] => application/vnd.ms-excel
[4] => application/vnd.ms-excel
)
[tmp_name] => Array
(
[0] => C:\Windows\Temp\php5FEB.tmp
[1] => C:\Windows\Temp\php5FEC.tmp
[2] => C:\Windows\Temp\php5FED.tmp
[3] => C:\Windows\Temp\php5FEE.tmp
[4] => C:\Windows\Temp\php5FEF.tmp
)
=> Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
)
[size] => Array
(
[0] => 170516
[1] => 116832
[2] => 2351
[3] => 275819
[4] => 1870
)
)
)
something like this :
[0] => file1.csv
[1] => file2.csv
[2] => file3.csv
[3] => file4.csv
[4] => file5.csv
Until file30.csv?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Im sorry Julian not so understand ur coding :(
What don't you understand?
ASKER
I tried to implement your coding ..
Parse error: syntax error, unexpected 'foreach' (T_FOREACH) in C:\xampp\htdocs\db_v3\uplo ad.php on line 11
<?php
include 'config.php';
if(isset($_POST['btn-upload']))
{
$targetDir = "uploads/";
$files = $_FILES['file']
foreach($files['name'] as $key => $name) {
$file = $files['tmp_name'][$key];
$target = $targetDir . "/" . $name;
if (is_uploaded_file($file)) {
move_uploaded_file($file, $target);
echo $file . '</br>';
}
}
}
?>
I not sure is it the right way , but the output get error . Parse error: syntax error, unexpected 'foreach' (T_FOREACH) in C:\xampp\htdocs\db_v3\uplo
You are missing a ';' on line 10
$files = $_FILES['file']
Should be$files = $_FILES['file'];
ASKER
Parse error: syntax error, unexpected 'foreach' (T_FOREACH) in C:\xampp\htdocs\db_v3\uplo ad.php on line 11
still have this error
still have this error
Please don't post errors without the accompanying source code. I have no idea what your code now looks like and the error on its own is of no use.
ASKER
<?php
include 'config.php';
if(isset($_POST['btn-upload']))
{
$targetDir = "uploads/";
$files = $_FILES['file'];
foreach($files['name'] as $key => $name) {
$file = $files['tmp_name'][$key];
$target = $targetDir . "/" . $name;
if (is_uploaded_file($file)) {
move_uploaded_file($file, $target);
echo $file . '</br>';
}
}
}
?>
This my coding
ASKER
$targetDir = "uploads/";
anyway the "uploads/" is nid to set path ?
ASKER
Thank Julian Hansen ! I tried your code and success already
You are welcome.
In other words
Open in new window
Instead ofOpen in new window
Note the [] on the end of the name - this will create names for the uploaded files that PHP will translate into an array.
Post your HTML form so we can see how you have implemented that.
If you use the above you will get a response that looks something like this
Open in new window
In other words an array of N items for each of the attributes for a file upload - where N is the number of files uploaded.
You will then need to create a loop that operates on these arrays - checking the file is uploaded and then moving / renaming them to their new location before updating the DB.
Post your <form> code so I can confirm the above.