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
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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.