Hi everyone

I want to add all text files in a given directory to a html combo box, when the user selects a file, I have one php script which takes the file and adds it to mysql query LOAD DATA INFILE!

I have successfully loaded data from a given file, but only when the file is hardcoded like: LOAD DATA INFILE 'C:\myfiles\data.txt'.

I want to get this file from a list in the combo box, please help!!

i use this to display the code on my server so i don't have to ftp all the time to see errors.

you can make some changes to it so it will work for you. just to let you know its not complete but works. i didn't add the extension validation yet, but the directory stuff works.

<?php

$dir = dirname(__FILE__);
$allowed = array('php');
$directories = array('private','public_html','demo','classes','config','cron');

$files = array();

function listFiles($dir) {
	global $files,$directories;
	if (is_dir($dir)) {
		$open = opendir($dir);
		if (!$open) {
			die('Unable to open directory: ' . $dir);
		}
		else {
			while (($file = readdir($open)) !== false) {
				if ($file !== '.' && $file !== '..') {
					$name = $dir . '/' . $file;
					if (is_dir($name)) {
						if (in_array($file,$directories)) {
							listFiles($name);
						}
					}
					else {
						$files[] = $name;
					}
				}
			}
			closedir($open);
		}
	}
	else {
		die($dir . ' is not a directory');
	}
}

listFiles($dir);

echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="get">';
echo '<select name="file" onchange="this.form.submit()">';
echo '<option value=""></option>';
foreach($files as $file) {
	echo '<option value="' . $file . '">' . str_replace($_SERVER['DOCUMENT_ROOT'],'',$file) . '</option>';
}
echo '</select>';
echo '</form>';

if (isset($_GET['file'])) {
	$file = $_GET['file'];
	$open = fopen($file,'r');
	if ($open) {
		$i = 1;
		echo '<div style="width:80%;height:600px;"><pre>';
		while (!feof($open)) {
			$buffer = fgets($open, 4096);
			echo htmlentities($buffer);
		}
		echo '</pre></div>';
		fclose($open);
	}
}

?>

i created it in about 10 minutes so it not perfect.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.