Solved

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

Posted on 2016-09-19
10
129 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 7

Expert Comment

by:D Patel
ID: 41805897
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 58

Expert Comment

by:Julian Hansen
ID: 41806029
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
ID: 41807659
Julian,
I want to be able to browse and find for a specific file.

Thanks,
mrotor
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41808104
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
 

Author Comment

by:mainrotor
ID: 41818802
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 58

Expert Comment

by:Julian Hansen
ID: 41819325
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 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41828647
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
ID: 41830352
Ray,
thank you very much for providing some code.
I will test it and post my results.

thank you,
mrotor
1

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article discusses how to implement server side field validation and display customized error messages to the client.
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 dynamically set the form action using jQuery.

617 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