Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

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

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 60

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 60

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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses

916 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