Jump to content

little help please. need to make file get uploaded by form/


lovephp
Go to solution Solved by lovephp,

Recommended Posts

friend i got this importing csv to database working fine but how could i make it that i choose the csv file from a html form and then upload please help me out

 

 
 
<?php
//database connection details
require_once('config.php');
// path where your CSV file is located
define('CSV_PATH','C:\Users\Downloads/');
// Name of your CSV file
$csv_file = CSV_PATH . "student.csv";
if (($getfile = fopen($csv_file, "r")) !== FALSE) {
        $data = fgetcsv($getfile, 100000000, ",");
        while (($data = fgetcsv($getfile, 100000000, ",")) !== FALSE) {
         $num = count($data);
         for ($c=0; $c < $num; $c++) {
             $result = $data;
             $str = implode(",", $result);
             $slice = explode(",", $str);
             $col0 = $slice[0];
             $col1 = $slice[1];
             $col2 = $slice[2];
             $col3 = $slice[3];
             $col4 = $slice[4];
             $col5 = $slice[5];
             $col6 = $slice[6];
             $col7 = $slice[7];             
// SQL Query to insert data into DataBase
$query = "INSERT INTO details(id, student_phone, student_fname, student_lname, street_address, city, state, zip)
VALUES('".$col0."','".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."','".$col7."')";
$s=mysql_query($query);
     }
   }
 }
echo "File data successfully imported to database!!";
?>

 

 

 

regards and thanks :)

Edited by lovephp
Link to comment
Share on other sites

tried but not working so asked for help

<?php
//database connection details
require_once('config.php');

$uploaddir = 'uploads';
$csv_file = $uploaddir . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  unlink($uploadfile);
}

if (($getfile = fopen($csv_file, "r")) !== FALSE) {
        $data = fgetcsv($getfile, 100000000, ",");
        while (($data = fgetcsv($getfile, 100000000, ",")) !== FALSE) {
         $num = count($data);
         for ($c=0; $c < $num; $c++) {
             $result = $data;
             $str = implode(",", $result);
             $slice = explode(",", $str);
             $col0 = $slice[0];
             $col1 = $slice[1];
             $col2 = $slice[2];
             $col3 = $slice[3];
             $col4 = $slice[4];
             $col5 = $slice[5];
             $col6 = $slice[6];
             $col7 = $slice[7];             
// SQL Query to insert data into DataBase
$query = "INSERT INTO details(id, student_phone, student_fname, student_lname, street_address, city, state, zip)
VALUES('".$col0."','".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."','".$col7."')";
$s=mysql_query($query);

     }

   }

 }

//echo "File data successfully imported to database!!";

?>
<form enctype="multipart/form-data" action="mysqldump.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>

Edited by lovephp
Link to comment
Share on other sites

ok i tried this

 

<?php
    require_once('config.php');
if(isset($_FILES['csv'])){
    $errors= array();
    $file_name = $_FILES['csv']['name'];
    $file_size =$_FILES['csv']['size'];
    $file_tmp =$_FILES['csv']['tmp_name'];
    $file_type=$_FILES['csv']['type'];   
    $file_ext=strtolower(end(explode('.',$_FILES['csv']['name'])));
    $extensions = array("csv");         
    if(in_array($file_ext,$extensions )=== false){
     $errors ="Unknown file extensions, Upload only .csv file";  }
    if(empty($errors)==true){
      $csv_file =  move_uploaded_file($file_tmp,"uploads/".$file_name);
      if (($getfile = fopen($csv_file, "r")) !== FALSE) {
        $data = fgetcsv($getfile, 100000000, ",");
        while (($data = fgetcsv($getfile, 100000000, ",")) !== FALSE) {
         $num = count($data);
         for ($c=0; $c < $num; $c++) {
             $result = $data;
             $str = implode(",", $result);
             $slice = explode(",", $str);
             $col0 = $slice[0];
             $col1 = $slice[1];
             $col2 = $slice[2];
             $col3 = $slice[3];
             $col4 = $slice[4];
             $col5 = $slice[5];
             $col6 = $slice[6];
             $col7 = $slice[7];             
// SQL Query to insert data into DataBase
$query = "INSERT INTO details(id, student_phone, student_fname, student_lname, street_address, city, state, zip)
VALUES('".$col0."','".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."','".$col7."')";
$s=mysql_query($query);
     }
   }
 }
    }else{
        echo $errors;
    }
}
?>
<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="csv" />
    <input type="submit"/>
</form>

 

file gets uploaded but nothing into database gets inserted

Link to comment
Share on other sites

done did it atlast but the file is not getting deleted after inserting into db

 

<?php
    require_once('config.php');
if(isset($_FILES['csv'])){
    $errors= array();
    $file_name = $_FILES['csv']['name'];
    $file_size =$_FILES['csv']['size'];
    $file_tmp =$_FILES['csv']['tmp_name'];
    $file_type=$_FILES['csv']['type'];   
    $file_ext=strtolower(end(explode('.',$_FILES['csv']['name'])));
    $extensions = array("csv");         
    if(in_array($file_ext,$extensions )=== false){
     $errors ="Unknown file extensions, Upload only .csv file";  }
    if(empty($errors)==true){
     move_uploaded_file($file_tmp,"uploads/".$file_name);
      if (($getfile = fopen("uploads/".$file_name, "r")) !== FALSE) {
        $data = fgetcsv($getfile, 100000000, ",");
        while (($data = fgetcsv($getfile, 100000000, ",")) !== FALSE) {
         $num = count($data);
         for ($c=0; $c < $num; $c++) {
             $result = $data;
             $str = implode(",", $result);
             $slice = explode(",", $str);
             $col0 = $slice[0];
             $col1 = $slice[1];
             $col2 = $slice[2];
             $col3 = $slice[3];
             $col4 = $slice[4];
             $col5 = $slice[5];
             $col6 = $slice[6];
             $col7 = $slice[7];             
// SQL Query to insert data into DataBase
$query = "INSERT INTO details(id, student_phone, student_fname, student_lname, street_address, city, state, zip)
VALUES('".$col0."','".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."','".$col7."')";
$s=mysql_query($query);
     }
   }
 }
unlink("uploads/$file_name");
    }else{
        echo $errors;
    }
}
?>
<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="csv" />
    <input type="submit"/>
</form>

Edited by lovephp
Link to comment
Share on other sites

You didnt' give us an error so we don't know what's happening, but let me provide a little improvement on your code that may help you to get a better understanding of what you are doing.

 

To process the contents of the csv file do this:

 

 while ($slice = fgetcsv($getfile, 100000000, ",")) 
{
             $col0 = $slice[0];
             $col1 = $slice[1];
             $col2 = $slice[2];
             $col3 = $slice[3];
             $col4 = $slice[4];
             $col5 = $slice[5];
             $col6 = $slice[6];
             $col7 = $slice[7];
...
...
}

A much clearer and sensible of retrieving the data read in.             
 

Link to comment
Share on other sites

You didnt' give us an error so we don't know what's happening, but let me provide a little improvement on your code that may help you to get a better understanding of what you are doing.

 

To process the contents of the csv file do this:

 while ($slice = fgetcsv($getfile, 100000000, ",")) 
{
             $col0 = $slice[0];
             $col1 = $slice[1];
             $col2 = $slice[2];
             $col3 = $slice[3];
             $col4 = $slice[4];
             $col5 = $slice[5];
             $col6 = $slice[6];
             $col7 = $slice[7];
...
...
}

A much clearer and sensible of retrieving the data read in.             

 

thanks bro ill do that and could you tell me why the uploaded file not getting deleted?

Link to comment
Share on other sites

 error_reporting(E_ALL | E_STRICT | E_NOTICE);
 ini_set('display_errors', '1');

 

To repeat my previous

put an echo IN FRONT and one AFTER the unlink.  Showing the result of the unlink is not very helpful since it is Boolean most likely.

 

 

echo "About to do unlink<br>";
unlink(....);
echo "Did the unlink";

 

 

Link to comment
Share on other sites

 

 error_reporting(E_ALL | E_STRICT | E_NOTICE);

 ini_set('display_errors', '1');

 

To repeat my previous

put an echo IN FRONT and one AFTER the unlink.  Showing the result of the unlink is not very helpful since it is Boolean most likely.

echo "About to do unlink<br>";
unlink(....);
echo "Did the unlink";

yes i did and i get

 

Warning: unlink(uploads/student.csv) [function.unlink]: Permission denied in C:\wamp\www\student\sqldumup.php on line 35

 

this is line 35 echo unlink("uploads/$file_name");

Edited by lovephp
Link to comment
Share on other sites

This is no real reason to move_uploaded_file() if you aren't going to keep it. Just open the temporary file. PHP will remove that one at the end of the script (supposedly). But maybe you need to close it before you unlink it. If this is Windows, I'm pretty sure that is the problem.

 

As for processing ...

while (($data = fgetcsv($getfile, 100000000, ",")) !== FALSE) {
  if (count($data) >  $data = array_slice($data, 0, ;
    $query = "INSERT INTO details(id, student_phone, student_fname, student_lname, street_address, city, state, zip)
VALUES('" . implode("','", $data) . "')";
    $s=mysql_query($query);
  }
}
Actually, you would be better off using a prepared statement. And even better off by doing a multi-row insert. But since you are still using the mysql library, I'll leave it at that.

 

Note: the mysql library has been deprecated. You need to switch to mysqli.

Link to comment
Share on other sites

This is no real reason to move_uploaded_file() if you aren't going to keep it. Just open the temporary file. PHP will remove that one at the end of the script (supposedly). But maybe you need to close it before you unlink it. If this is Windows, I'm pretty sure that is the problem.

 

As for processing ...

while (($data = fgetcsv($getfile, 100000000, ",")) !== FALSE) {
  if (count($data) >  $data = array_slice($data, 0, ;
    $query = "INSERT INTO details(id, student_phone, student_fname, student_lname, street_address, city, state, zip)
VALUES('" . implode("','", $data) . "')";
    $s=mysql_query($query);
  }
}
Actually, you would be better off using a prepared statement. And even better off by doing a multi-row insert. But since you are still using the mysql library, I'll leave it at that.

 

Note: the mysql library has been deprecated. You need to switch to mysqli.

 

thanks that was helpful. now how on earth i sort the unlink file.

Link to comment
Share on other sites

But maybe you need to close it before you unlink it. If this is Windows, I'm pretty sure that is the problem.

 

thanks that was helpful. now how on earth i sort the unlink file.

Did you try closing the file? On a *nix system if would not matter, but Windows is particular about that.

Link to comment
Share on other sites

To sort a csv file is a bit of a bind but do-able.

 

Read the file in using fgetcsv which will give you an array.  Then grab the element of the array that you want to sort by and then assign that value as the key of a new array and the array you read the data into as the value of that key.

while ($row = fgetcsv($handle))
{
   //  use element #3 of the input as the sort key (starting with 0)
   $sortarray[{$row[3]}] = $row;

Now just sort the var $sortarray and then process the sorted array using a foreach.  Of course I haven't done this and I'm having a bad day on the forums, so there could be flaws in this.  Cheers! :)

foreach ($sortarray as $k=>$v)
{
     (process $v as an array the same as you would have processed $row in the first block of code)
}
Edited by ginerjm
Link to comment
Share on other sites

I thought it was a permissions problem.  Change the permissions and it will go away.  OR use the advice given to you and skip the whole move and creating a permanent file and just let the system delete the temporary file.

 

OH - and work on your English.  Or stop typing on your cell phone.  A little respect for yourself and those you wish to read and understand your writings can come in handy.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.