How to import directory and echo the filename ?

I want to import the directory without set the import location , because i have many directory to import.

$log_directory = '/xampp/htdocs/mydoc';

$results_array = array();

if (is_dir($log_directory))
{
        if ($handle = opendir($log_directory))
        {
                //Notice the parentheses I added:
                while(($file = readdir($handle)) !== FALSE)
                {
                        $results_array[] = $file;
                }
                closedir($handle);
        }
}

//Output findings
foreach($results_array as $value)
{
    echo $value . '<br />';
}

Open in new window


$log_directory = '/xampp/htdocs/mydoc';
I dont wanna set path like this .. izit posible or not ? because i lazy set again and again to import different directory
Scott Yong Asked:
Who is Participating?
 
Julian HansenCommented:
To upload multiple files you need
a) A file input control with attribute multiple and a name specificying an array
<input type="file" name="files[]" multiple>

Open in new window

This will allow for multiple selection of files on the client which will then be sent as an array of files to the server.

b) On the server you need to retrieve the array of files

$files = $_FILES['files']

Open in new window


And then

c) You need to loop over that array and process each file in turn
$targetDir = "uploads/";
foreach($files['name'] as $key => $name) {
    $file = $files['tmp_name'][$key];
    $target = $targetDir  . "/" . $name;
    if (is_uploaded_file($file)) {
       move_uploaded_file($file, $target);
    }
}

Open in new window


EDIT:
Remember to add the form enctype attribute to "multipart/form-data"
<form action="..." method="post"  enctype="multipart/form-data">

Open in new window

0
 
Julian HansenCommented:
Upload to where?

You are using server side code - which means the directory must be on the server - where are you wanting to upload it to and how?
0
 
Scott Yong Author Commented:
No I want to list down all the file name i uploaded without setting the path $log_directory = '/xampp/htdocs/mydoc';
0
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
Scott Yong Author Commented:
<?php
include 'config.php';

if(isset($_POST['btn-upload']))
{  


$targetDir = "uploads/";
$fileName = $_FILES['file']['name'];
$targetFile = $targetDir.$fileName;


//Show filename with file extension
 print $fileName . "\n";
}

?>

Open in new window


I found this can but only single file will display ~
0
 
Scott Yong Author Commented:
Notice: Undefined variable: files in C:\xampp\htdocs\db_v3\upload.php on line 8

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\db_v3\upload.php on line 8

Error

$targetDir = "uploads/";
foreach($files['name'] as $key => $name) {
    $file = $files['tmp_name'][$key];
    $target = $targetDir  . "/" . $name;
    if (is_uploaded_file($file)) {
       move_uploaded_file($file, $target);
    }
}
0
 
Julian HansenCommented:
Where is $files declared?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.