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 @
    $con=@mysqli_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>
4

1 に答える 1