Jump to content

Run a text file


newbiePHP

Recommended Posts

Hey Guys,

 

I've written a program that searchs through a directory and finds a particular file.

 

Now, all i want to do is simply run that file as if i double clicked the mouse on it and it opened.

 

I don't want to read in the file or do anything with it, just run it.

 

I've tried exec commands and Fopen but no joy so far.

 

Any help would be great, thanks.

 

NewbiePHP

Link to comment
https://forums.phpfreaks.com/topic/108150-run-a-text-file/
Share on other sites

oh, you meant you want to run a file on the client side. No, of course there is NO WAY to do this. This would be a really heavy security issue. PHP can only work on the server side. Just javascript can operate on the client side, but unless there is a big bug you cant access files. javascript operates in a so called "sandbox", there is (should be) no way out - and so it should be.

 

kind regards

Link to comment
https://forums.phpfreaks.com/topic/108150-run-a-text-file/#findComment-554391
Share on other sites

I've included the code below. It search the directory for "Example.jpg" or "Example.gif" or "Example.txt".

 

When it finds one of them I just want it to open that file and show it to me.

 

It searches it fine and can find the file but struggling to find a way to open that file.

 

<?php

if (isset($_POST['submit'])) {	
$FindFile=$_POST['FindFile'];
}

$directory = 'C:/College Work/'; //trailing slash
$wtlf = 'Example'; //what to look for

$flag = false;
$ext = array( '.jpg' , '.gif' , '.txt' );
for( $i = 0; count( $ext ) > $i; $i++ )
{
     if( file_exists( $directory . $FindFile . $ext[$i] ) )
     {
          $flag = true;
          $name = $directory . $FindFile . $ext[$i];
          break;
     }
}

if( $flag == true )
{
//open the file. NOTE: below does not work. tried putting fopen instead of exec but still wouldn't open the file
     exec('$name') or exit("Unable to open file!");
     
}
?>

Link to comment
https://forums.phpfreaks.com/topic/108150-run-a-text-file/#findComment-554394
Share on other sites

<?php

if (isset($_POST['submit'])) {    
$FindFile=$_POST['FindFile'];
}

$directory = 'C:/College Work/'; //trailing slash
$wtlf = 'Example'; //what to look for

$flag = false;
$ext = array( '.jpg' , '.gif' , '.txt' );
for( $i = 0; count( $ext ) > $i; $i++ )
{
     if( file_exists( $directory . $FindFile . $ext[$i] ) )
     {
        if($ext[$i] == ".jpg" || $ext[$i] == ".gif")
        {
            echo( "<img src=\"". $directory . $FindFile . $ext[$i] ."\">" );
        }
        else
        {
            echo( $directory . $FindFile . $ext[$i] ); 
        } 
        break;
     }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/108150-run-a-text-file/#findComment-554399
Share on other sites

It just dumps the contents to the browser, as most browsers will just display the contents of the text file.

 

Wouldn't you need to do a header redirect to the file? Your script above only echoes the path when it's a txt file.

 

Edit: In Firefox, local files are accessed with "file:///" in front of the path.

Link to comment
https://forums.phpfreaks.com/topic/108150-run-a-text-file/#findComment-554413
Share on other sites

It just dumps the contents to the browser, as most browsers will just display the contents of the text file.

 

Wouldn't you need to do a header redirect to the file? Your script above only echoes the path when it's a txt file.

 

Edit: In Firefox, local files are accessed with "file:///" in front of the path.

 

oh my, you're right lol

 

 

Link to comment
https://forums.phpfreaks.com/topic/108150-run-a-text-file/#findComment-554417
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.