Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-09-19
10
Medium Priority
?
144 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 59

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
Supports up to 4K resolution!

The VS192 2-Port 4K DisplayPort Splitter is perfect for anyone who needs to send one source of DisplayPort high definition video to two or four DisplayPort displays. The VS192 can split and also expand DisplayPort audio/video signal on two or four DisplayPort monitors.

 
LVL 59

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 59

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

660 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