newbiePHP Posted May 31, 2008 Share Posted May 31, 2008 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 Quote Link to comment Share on other sites More sharing options...
Prismatic Posted May 31, 2008 Share Posted May 31, 2008 You can't run an application on a user's computer through PHP, this includes opening text files. exec() and system() both act locally on the server. Quote Link to comment Share on other sites More sharing options...
kbh43dz_u Posted May 31, 2008 Share Posted May 31, 2008 do you want to get it opened in your browser? or how do you want to "run" it. what should it be good for? do you mean that the browser doesn't show the download-dialog but just opens the file? Quote Link to comment Share on other sites More sharing options...
newbiePHP Posted May 31, 2008 Author Share Posted May 31, 2008 Prismatic, thanks for the reply ok....is there anyway around this at all? maybe some kind of run command or anything really? Quote Link to comment Share on other sites More sharing options...
Prismatic Posted May 31, 2008 Share Posted May 31, 2008 Well what are you trying to do exactly? Quote Link to comment Share on other sites More sharing options...
kbh43dz_u Posted May 31, 2008 Share Posted May 31, 2008 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 Quote Link to comment Share on other sites More sharing options...
newbiePHP Posted May 31, 2008 Author Share Posted May 31, 2008 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!"); } ?> Quote Link to comment Share on other sites More sharing options...
newbiePHP Posted May 31, 2008 Author Share Posted May 31, 2008 vamosbenediktm, i'm not dealing with client/ servers. This program is only running on my laptop to search my directorys Quote Link to comment Share on other sites More sharing options...
Prismatic Posted May 31, 2008 Share Posted May 31, 2008 <?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; } } ?> Quote Link to comment Share on other sites More sharing options...
newbiePHP Posted May 31, 2008 Author Share Posted May 31, 2008 Thanks Prismatic thats great. but what if it is a text file? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted May 31, 2008 Share Posted May 31, 2008 Out of curiosity, I tried to open a text file with notepad (I'm on Windows): <?php exec('notepad nameoftextfile.txt'); ?> And it worked fine. The script keeps loading until you've closed notepad though. Quote Link to comment Share on other sites More sharing options...
newbiePHP Posted May 31, 2008 Author Share Posted May 31, 2008 thebadbad, Thanks for the help. I tried this but the notepad file never actual opened. exec('notepad C:/College Work/Example.txt'); Is there somehthing wrong with my code that i can't see? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted May 31, 2008 Share Posted May 31, 2008 It must be the space in the path, try exec('notepad "C:/College Work/Example.txt"'); Quote Link to comment Share on other sites More sharing options...
kbh43dz_u Posted May 31, 2008 Share Posted May 31, 2008 maybe the space! Quote Link to comment Share on other sites More sharing options...
Prismatic Posted May 31, 2008 Share Posted May 31, 2008 Thanks Prismatic thats great. but what if it is a text file? It just dumps the contents to the browser, as most browsers will just display the contents of the text file. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted May 31, 2008 Share Posted May 31, 2008 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. Quote Link to comment Share on other sites More sharing options...
newbiePHP Posted May 31, 2008 Author Share Posted May 31, 2008 That didn't work either. I tried a different file with no spaces in the name but it didn't work either. I just kept loading without actual opening the notepad file Thanks for all the help by the way Quote Link to comment Share on other sites More sharing options...
Prismatic Posted May 31, 2008 Share Posted May 31, 2008 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 Quote Link to comment Share on other sites More sharing options...
thebadbad Posted May 31, 2008 Share Posted May 31, 2008 newbiePHP, if you press Windows-key + R, and type "notepad" and press OK, does Notepad open? If not, try calling it by the absolute path. Something like <?php exec('C:/WINDOWS/notepad.exe'); ?> Quote Link to comment Share on other sites More sharing options...
newbiePHP Posted May 31, 2008 Author Share Posted May 31, 2008 thebadbad, Yep it opens and if i type in the path in the run command box(C:/College Work/Example.txt) that opens the text file to. can't figure out why it won't do it in the script Quote Link to comment Share on other sites More sharing options...
thebadbad Posted May 31, 2008 Share Posted May 31, 2008 Could be your firewall blocking your server from opening it. My firewall asked for permission when I first tried. Quote Link to comment Share on other sites More sharing options...
newbiePHP Posted May 31, 2008 Author Share Posted May 31, 2008 Nope no firewalls or servers. This is all running on my laptop. The scirpt is searching for the file on my hard drive Quote Link to comment Share on other sites More sharing options...
thebadbad Posted May 31, 2008 Share Posted May 31, 2008 I know, by server I meant your computer (acting like a server). You have no firewall?? Quote Link to comment Share on other sites More sharing options...
corbin Posted May 31, 2008 Share Posted May 31, 2008 When PHP runs under Apache, and Apache is a service, you have to allow Apache to interact with the desktop before PHP can do somethings... Are you running PHP under Apache? Quote Link to comment Share on other sites More sharing options...
newbiePHP Posted May 31, 2008 Author Share Posted May 31, 2008 oh right sorry. i was thinking along the same lines as you about them interfering so I turned them of for few mins Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.