We help IT Professionals succeed at work.

I upload file from directory to database but i realise that all file i upload is the same.

Yong Scott
Yong Scott asked
on
139 Views
Last Modified: 2018-01-22
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

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
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.

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

Author

Commented:
That is my html code

Author

Commented:
Do you know how to not target directory can upload file which mean all directory can access no specific directory?
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
Where is your file input control?

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

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
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

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

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

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.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
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

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?
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Im sorry Julian not so understand ur coding :(
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
What don't you understand?

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
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
You are missing a ';' on line 10
$files = $_FILES['file']

Open in new window

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

Open in new window

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
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
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.

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

Author

Commented:
$targetDir = "uploads/";

Open in new window

anyway the "uploads/" is nid to set path ?

Author

Commented:
Thank Julian Hansen ! I tried your code and success already
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
You are welcome.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.