Solved

I need help developing an PHP application that browses for a file.

Posted on 2016-09-19
10
53 Views
Last Modified: 2016-11-02
Hi Experts,
I need help writing a PHP application that browses for a file.  I am using windows.  I have XAMPP installed on my machine (I don't know if this is relevant or not).
 
How can I do this?

Thanks in advance,
mrotor
0
Comment
Question by:mainrotor
10 Comments
 
LVL 5

Expert Comment

by:D Patel
Comment Utility
Find the simple File Browser PHP Script as below :

<?php
$root = __DIR__;

function is_in_dir($file, $directory, $recursive = true, $limit = 1000) {
    $directory = realpath($directory);
    $parent = realpath($file);
    $i = 0;
    while ($parent) {
        if ($directory == $parent) return true;
        if ($parent == dirname($parent) || !$recursive) break;
        $parent = dirname($parent);
    }
    return false;
}

$path = null;
if (isset($_GET['file'])) {
    $path = $_GET['file'];
    if (!is_in_dir($_GET['file'], $root)) {
        $path = null;
    } else {
        $path = '/'.$path;
    }
}

if (is_file($root.$path)) {
    readfile($root.$path);
    return;
}

if ($path) echo '<a href="?file='.urlencode(substr(dirname($root.$path), strlen($root) + 1)).'">..</a><br />';
foreach (glob($root.$path.'/*') as $file) {
    $file = realpath($file);
    $link = substr($file, strlen($root) + 1);
    echo '<a href="?file='.urlencode($link).'">'.basename($file).'</a><br />';
}

Open in new window

0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
I need help writing a PHP application that browses for a file.
Can you explain a bit more on exactly what you want to achieve.
Do you want to create a list of all files and folders that you can click on and navigate through
Or
Do you want to be able to find a specific file.
0
 

Author Comment

by:mainrotor
Comment Utility
Julian,
I want to be able to browse and find for a specific file.

Thanks,
mrotor
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
Do you need to search in one folder or recursively through a hierarchy?

If you know the folder you can do

if (file_exists("somespecificfile.txt")) {
   // process here
}
else {
   // what to do if there is no file
}

Open in new window


If you need to do a recursive search then I would need to ask why you would want to do this. What is the use case - it is a lot of processing that could be avoided by potentially changing rules / configuration of how your files are ordered.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:mainrotor
Comment Utility
Thanks Julian for your response,
I need to browse through a hierarchy.

I want to give the users the ability to browse through a file directory.
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
How would they interact with this code - do you want to display the file system in a tree (GUI) ?
If so then people have already solved this problem. If you Google "PHP file browser" - you should find a number of samples that do this.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
Here is a script that will recursively search its own directory and all directories below it.  For obvious reasons you would want to restrict access to only authorized users!
<?php // demo/find_files.php
/**
 * Put this script in the web root directory
 *
 * Traverse all directories down the web tree
 * Show all matching files
 *
 * http://php.net/manual/en/class.recursivedirectoryiterator.php#85805
 */
ob_start();
error_reporting( E_ALL );
ini_set( 'display_errors', TRUE );
ini_set( 'log_errors',     TRUE );


// START IN THE CURRENT DIRECTORY
$path = realpath(getcwd());

// THE FILE NAME
$fnam   = $_GET['q'];
if (empty($fnam)) trigger_error("Missing q= URL Argument; please specify the file name.", E_USER_ERROR);
$signal = strrev($fnam);

// COLLECT THE DIRECTORY INFORMATION OBJECTS
$objs = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);

// ITERATE OVER THE OBJECTS
foreach($objs as $name => $obj)
{
    // PROCESS THE REQUESTED FILE NAME(S) ONLY
    $test = strrev($name);
    if (strpos($test, $signal) === 0)
    {
        echo PHP_EOL . "<br>$name";
    }
}

// IF THERE ARE NO MATCHING FILE(S)
$out = ob_get_contents();
if (empty($out)) echo "<h3>No $fnam found.</h3>";


// SHOW THE GIT BRANCH
$root = '.git/HEAD';
$text = @file_get_contents($root);
if ($text)
{
    $text = explode(DIRECTORY_SEPARATOR, $text);
    $text = array_slice($text, 2);
    $name = implode(DIRECTORY_SEPARATOR, $text);
    echo PHP_EOL . "<br>On Git branch: $name" . PHP_EOL;
}
else
{
    echo PHP_EOL . "<br>On Git branch: UNKNOWN" . PHP_EOL;
}

// SCRIPT TERMINATION WILL FLUSH THE OUTPUT BUFFER TO THE CLIENT BROWSER

Open in new window

0
 

Author Comment

by:mainrotor
Comment Utility
Ray,
thank you very much for providing some code.
I will test it and post my results.

thank you,
mrotor
1

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now