prosseek
prosseek

Reputation: 190979

How to make it run the `exec()` command in PHP to edit some file in Mac OS X?

When I have this PHP script.

<?php
    exec('/usr/local/bin/mate hello.txt');
?>

It doesn't work on web browser with http://abc/hello.php, for example.

The command of mate is 'TextMate app', and it's just for editing hello.txt.

Is this some php permission problem? I just need to run some commands on my local web server (I'm the only user), so I can open the permission to run this seemingly dangerous exec() function.

I use Mac OS X 10.6.6/Apache/PHP5.

ADDED

I guess it's not possible to launch something with PHP on my Mac, but for my purposes to open an TextMate editor to edit something, using txmt protocol works perfectly fine.

SOLVED

In terms of launching an Application in web browser (especially Safari) cannot be done with php, but with protocol handler.

Launching TextEditor to edit something.

TextEditor provides its own protocol handler txmt://open/?url=file://THE_FILE_TO_EDIT".

Or, you can have a button to edit the file when clicked.

<form action="txmt://open/?url=file://FILE_TO_EDIT" method="post">
  <button type="submit">Edit</button>
</form>

Launching other app

You need to come up with your own protocol handler. has all the necessary information with an example.

For example, for launching PathFinder

Make URL types/schemes at Info.plist.

You may want to main window that pops up. You can set Application is agent.

enter image description here

Make the pf: protocol handler.

It just analyzes the input of pf:INPUT_GINVEN, to get the INPUT_GIVEN part to give it as a parameter to PathFInder.

@implementation URLHandlerCommand!
- (id)performDefaultImplementation {
    NSString *urlString = [self directParameter];

    NSLog(@"url :=: %@", urlString);

    NSArray *components = [urlString componentsSeparatedByString: @":"];
    NSString* string2 = (NSString*) [components objectAtIndex:1];

    NSLog(@"url :=: %@", string2);

    [[NSWorkspace sharedWorkspace] openFile:string2 withApplication:@"Path Finder"];
    [[NSApplication sharedApplication] terminate:nil];

    return nil;
}

@end

Use the pf:OPEN_DIRECTORY protocol.
<form action="pf:DIRECORY_TO_OPEN" method="post">
    <button type="submit">Open</button>
</form>

Upvotes: 0

Views: 3604

Answers (2)

Andrew Marshall
Andrew Marshall

Reputation: 96994

If the script is running on a remote web server, then the exec() will run on the remote server, not the local machine. My guess is you want the command run on the local computer that is visiting the website, but this is not possible for very good (security) reasons.

What you probably want to use is the txmt:// protocol as described in the Textmate Manual:

<a href="txmt://open/?url=file://~/.bash_profile&line=11&column=2">Open in Textmate</a>

Upvotes: 2

profitphp
profitphp

Reputation: 8354

You are doing it right. Try running something else like ls and see if that works. When you run that command, does it show some output then return you to a prompt? If it doesn't, then you're not going to get anything by running it through exec in PHP. You are not going to be able to run a command line editor in the browser.

Upvotes: 2

Related Questions