简体   繁体   中英

Upload CSV file then Show Headings of columns

$org_file_name = $_FILES['file1']['name'];
            $file_size = $_FILES['file1']['size'];
            $ext = strtolower(pathinfo($org_file_name, PATHINFO_EXTENSION));
            $file_path = 'docs/';
            $rand = rand(111,999);          
        if($ext == 'csv')
        {
            if(file_exists($file_path))
                {
                     $file_path = 'docs/'.$org_file_name;
                }
            if(!move_uploaded_file($_FILES['file1']['tmp_name'],$file_path))
                {
                    die ('<script> alert("file not uploaded successfully."); </script>');
                }           
        }
        else
        {
            die ('<script> alert("Uploaded only CSV files.");  </script>');

How Do I rename CSV file when uploading and insert into database with renam and after insert show csv column headings in buttons

CSV headers (if present) are located right on the first line. So you can simply do:

$delimiter = ',';
$headers = fgetcsv(fopen($file_path, 'r'), $delimiter);

if (is_array($headers)) {
    foreach($headers as $header) {
         echo "<input type=\"button\" name=\"$header\"
               value=\"$header\"/>'";
        }
    }
}

And to rename the file while moving, you should change the second parameter of move_uploaded_file() to the desired name. If you like, you can get a unique ID using sha1_file($_FILES['file1']['tmp_name']). It will also avoid duplicated storage of identical files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM