• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 75
  • Last Modified:

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
<?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
 }
}
}
}

?>

Open in new window

0
Scott Yong
Asked:
Scott Yong
  • 14
  • 9
1 Solution
 
Julian HansenCommented:
I am guessing you used a multiple file input but did not specify an array as the name for the control

In other words
<input type="file" multiple" name="file">

Open in new window

Instead of
<input type="file" multiple" name="file[]">

Open 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
$_FILES
   name: Array()
      0 => Filename1.csv
      1 => Filename2.csv
      ...
    tmp_name: Array()
      0: d9dwe99x.tmp
      1: eix9d9b9d.tmp
       ...
     size: ...

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.
1
 
Scott Yong Author Commented:
<?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>

Open in new window

0
 
Scott Yong Author Commented:
That is my html code
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
Scott Yong Author Commented:
Do you know how to not target directory can upload file which mean all directory can access no specific directory?
0
 
Julian HansenCommented:
Where is your file input control?
0
 
Scott Yong Author Commented:
   <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>

Open in new window

0
 
Julian HansenCommented:
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
1
 
Scott Yong Author Commented:
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
0
 
Scott Yong Author Commented:
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
0
 
Scott Yong Author Commented:
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.
0
 
Julian HansenCommented:
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
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
        )
    )
)

Open in new window


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#a42435985
1
 
Scott Yong Author Commented:
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
        )
    )
)
So I need list down the file for each array?
something like this :
          [0] => file1.csv
          [1] => file2.csv
          [2] => file3.csv
          [3] => file4.csv
          [4] => file5.csv
Until file30.csv?
0
 
Julian HansenCommented:
To clarify. If you have this
<input type="file" name="files[]" multiple>

Open in new window

OR
<input type="file" webkitdirectory mozdirectory  name="file[]"  />

Open in new window

(PLEASE NOTE THE name="file[]" WHERE file[] HAS '[]' AT THE END. THIS IS IMPORTANT.)

It tells PHP to treat the incoming data as an Array.

An example of the Array returned by PHP is the one I posted above. It will have as many elements as files that have been selected.

You then use the process I described here https://www.experts-exchange.com/questions/29078470/How-to-import-directory-and-echo-the-filename.html#a42435985 to work through that array.
0
 
Scott Yong Author Commented:
Im sorry Julian not so understand ur coding :(
0
 
Julian HansenCommented:
What don't you understand?
0
 
Scott Yong Author Commented:
I tried to implement your coding ..
<?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>';
   
    }
}
}

?>	

Open in new window

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\upload.php on line 11
0
 
Julian HansenCommented:
You are missing a ';' on line 10
$files = $_FILES['file']

Open in new window

Should be
$files = $_FILES['file'];

Open in new window

1
 
Scott Yong Author Commented:
Parse error: syntax error, unexpected 'foreach' (T_FOREACH) in C:\xampp\htdocs\db_v3\upload.php on line 11
still have this error
0
 
Julian HansenCommented:
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.
0
 
Scott Yong Author Commented:
<?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>';
   
    }
}
}

?>	

Open in new window

This my coding
0
 
Scott Yong Author Commented:
$targetDir = "uploads/";

Open in new window

anyway the "uploads/" is nid to set path ?
0
 
Scott Yong Author Commented:
Thank Julian Hansen ! I tried your code and success already
0
 
Julian HansenCommented:
You are welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

  • 14
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now