Solved

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

Posted on 2016-09-19
10
108 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 56

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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 56

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 56

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reverse engineer .sql from php files 11 52
Checking https returns 301 21 56
update query is not working , giving Mariadb server version error 1 40
Rewrite Rule head scratcher 18 29
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

756 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