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

别来无恙 提交于 2019-12-11 16:41:28

问题


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.

  • Q : How can I make it run the PHP 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.

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>

回答1:


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>



回答2:


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.



来源:https://stackoverflow.com/questions/5045945/how-to-make-it-run-the-exec-command-in-php-to-edit-some-file-in-mac-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!