2013-05-27 73 views
0

我準備好撕掉我的頭髮了。

基本上我必須創建一個頁面,它讀取文件的目錄並創建一個表單,允許您按下每個文件名旁邊的導入按鈕將文件導入數據庫。

我通過目錄這樣的掃描:

$files=scandir('uploads');//get array of files/directories in uploads 
foreach($files as $file) {//loop through the array 
    if(!is_dir($file)) {//if not a directory must be a file 
     echo $file.'<input type="submit" value="Import CSV File" name="'.$file.'" >'; 
    } 
} 

,比相同的形式(全在一個表格)我做的:

$files=scandir('uploads');//get array of files/directories in uploads 
foreach($files as $file) {//loop through the array 
    if(!is_dir($file)) {//if not a directory must be a file 
     if(isset($_POST[$file])) {//this if never hits 
      echo 'aa'; 
      exit(); 
     } 
    } 
} 

,你可以通過我的意見時,看到我點擊一個按鈕,導入文件的if(isset($_POST[$file]))嗟......

我不知道爲什麼..

這裏是爲了更好地全面瞭解頁面上的一些代碼

import_csv.php:

<?php 
    if(session_id()=='') { 
     session_start(); 
    } 
    // check if user is not logged in thus is most likely not allowed to view the page or login went wrong 
    if(!isset($_SESSION['loggedin'])||$_SESSION['loggedin']===false) { 
     echo '<p>You are not logged in. Please <a href="index.php">login</a> and try again.</p>'; 
     exit();//stops the execution of the php file so we dont show the links below to unauthorized visitors 
    } 

    $files=scandir('uploads');//get array of files/directories in uploads 
    foreach($files as $file) {//loop through the array 
     if(!is_dir($file)) {//if not a directory must be a file 
      if(isset($_POST[$file])) { 
       echo 'aa'; 
       exit(); 
      } 
     } 
    } 
?> 

<h2 align="center">Import CSV Files:</h2> 

<p align="center"> 
This will allow you to view names of uploaded CSV files and import them into the database. 
Below are a list of available files on the server to be imported: 
</p> 

<form method="post" action="import_csv.php"> 

<?php 

    //Create connection and suppress any errors for now using @ 
    [email protected]_connect('localhost','jd','1111','my_db'); 

    // Check connection 
    if (mysqli_connect_errno($con)) { 
     echo 'Could not connect to database.'; 
     exit(); 
    }else { 
     //query all tables in db 
     $sql = "SHOW TABLES FROM my_db;"; 
     $result = mysqli_query($con,$sql); 


     //loop through results/all tables 
     while ($row = mysqli_fetch_row($result)) { 
      if($row[0]=='users'&&isset($_SESSION['user_type']) && $_SESSION['user_type']!='admin') { 
      }else { 
       echo '<input type="checkbox" name="'.$row[0].'" > '.$row[0].'<br></br>'; 
      } 
     } 
    } 

    $files=scandir('uploads');//get array of files/directories in uploads 
    foreach($files as $file) {//loop through the array 
     if(!is_dir($file)) {//if not a directory must be a file 
      echo $file.'<input type="submit" value="Import CSV File" name="'.$file.'" >'; 
     } 
    } 
?> 
</form> 
+0

的文件名是$ _POST數組中的關鍵?它從哪裏發佈? – 2013-05-27 21:59:07

+0

@Dagon你是什麼意思?表單標籤內部是我的php程序塊,它通過掃描目錄在按鈕旁邊創建按鈕和文本。當一個按鈕被按下時,表單會發送到* import_csv.php *這是同一頁面(全部在一個表單中),它將再次遍歷文件並檢查'if(isset($ _ POST [$ file])) –

回答

3

這是因爲文件擴展名的問題,文件擴展名總是應該的,但是PHP它轉換「」至 '_'。然後請重寫代碼一樣,

$files=scandir('uploads');//get array of files/directories in uploads 
foreach($files as $file) {//loop through the array 
    $postName = str_replace('.','_',$file); 
     if(isset($_POST[$postName])) {//if not a directory must be a file 
      echo 'aa'; 
      exit(); 
     } 
    } 

這裏的$文件應該喜歡FILE.CSV,但PHP請求數組包含$ _ POST [file_csv]

+0

+1你是最好的,非常感謝 –

+0

此外,我的複選框不會在帖子中打中可能是同樣的問題? –

+0

請打印整個$ _POST或$ _REQUEST數組print_r($ _ POST),它會顯示參數名稱 – Nisam