Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

php execute programs

Posted on 2011-10-19
19
Medium Priority
?
285 Views
Last Modified: 2012-05-12
Hi E's, do you know if have any functions in php that can execute programs in windows?
In practice, in the browser I present one list with some different files, .docs, .php etc. If I click in the .doc document the php open the Office Word with that file, if I click in .php file php will open Dreamwever in that file.

Is possible do this in php?

Regards, JC
0
Comment
Question by:Pedro Chagas
  • 8
  • 4
  • 3
  • +2
19 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36996019
Can you clarify what you mean? When you "run" the file, where would it run: on the user's machine, or on the server? If on the user's machine, you wouldn't have any control over this since there is a natural security boundary between web servers and the client machines they interact with. If on the server, how would you know it was running? For batch scripts, this might be fine, but trying to run DreamWeaver on a server doesn't quite make sense since there would be nobody to interact with it.
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 36996096
Hi, the system is just for I use in my computer.
I write php my own technological library, where I put web-design examples. I have lots of php files, and Word files in that library. I have the library installed in a normal hosting, and I execute that library from my browser in my home computer. So I thing, why not open the correspondent programs when I click in the files of my library?
Please if you don't understand my explanation tell me!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36996148
Are user machine and server machine the same machine?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Accepted Solution

by:
Vampireofdarkness earned 1400 total points
ID: 36996153
What you want, I think, is content-disposition:

   header('Content-Disposition: attachment; filename="yourdocument.doc"');

When you click on, say http://localhost/download.php?file=yourdocument.doc, you open the file (fopen), write the contents to the page and stick that header in. It'll prompt the user to download the file. In your case, you select the 'Open with' option and ensure the default program is the one you want to open.

Also see here (http://php.net/manual/en/function.header.php) for a reasonable example, from "phpnet at holodyn dot com 31-Jan-2011 09:01".
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 36996247
Hi @kaufmed, not was the same machine, The library is in a normal hosting and the computer is the my home computer!

Hi @Vampireofdarkness, In snippet code I create a simple code for maybe you can exemplify how work your solution in that code.
Can you help me?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
 <a href="http://www.example.com/folder/mydoc.doc">Office Word doc open</a>
</body>
</html>

Open in new window

0
 
LVL 9

Expert Comment

by:Vampireofdarkness
ID: 36996306
The word document will need to be hosted on your server, and the link will need to point to your server (http://www.example.com/download.php?file=folder/mydoc.doc). You will need a PHP file (download.php) with the correct header, as above, that reads the .doc file and outputs the contents to the browser.

It is late already, so I'll knock something together in the morning.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36997834
I think the Vampireofdarkness suggestion at post ID:36996153 be the only way to do what you want. As far as I know php can't directly open a file running the associated progam in Windows. Maybe you can embed a Java applet to do that...
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 200 total points
ID: 36997870
I found this Java code here http://stackoverflow.com/questions/390736/open-a-file-with-an-external-application-on-java:

public static void open(File document) throws IOException {
    Desktop dt = Desktop.getDesktop();
    dt.open(document);
}

For better documentation look at http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/

If that makes sense for you, you could open a new question in Java zone to get help on how to implement that java code within your actual page.

Cheers
0
 
LVL 27

Expert Comment

by:skullnobrains
ID: 36999186
vampireofdarkness gave you a good hint, i think

i'd also use fileinfo in order to provide the proper content-type header.
some browsers will rely on the extension, others on the content-type, and most will probably use both indirectly :
- use the content-type in order to determine if they can display the file directly
- hand the file over to windows, if they can't and let windows use the extension in order to pick the right program

the php cannot do much more since it runs on the server and cannot know what programs are installed on your computer, and it would be a huge security breach if a server could tell your system to run programs.

alternatively, why the hell do you want to do this in PHP ?
if you simply publish the directory that contains the library and remove and handler that would execute the files on the server, your web server will send the files with the proper headers, since it is it's essential basic function, and your browser and/or windows will do the rest if they are configured properly.
you may need to configure file-types association in windows and possibly the browser. some browsers have neat add-ons that will let you open files with specific programs such as firefox's "launchy"
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 36999243
Who can help me to put in practice the solution of @Vampireofdarkness with my code?
0
 
LVL 27

Assisted Solution

by:skullnobrains
skullnobrains earned 400 total points
ID: 36999316
$file=$_GET['file']; # assuming you sent the file as ...?file=/path/to/file

header('Content-Disposition: attachment; filename="'.basename($file).'"');
readfile($file);
0
 
LVL 9

Expert Comment

by:Vampireofdarkness
ID: 37001948
When you ask for help putting the code into practice, do you really mean you want someone to make the code for you, or point you in the right direction?
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 37002050
Point in the right direction.
I don't say nothing yet because I star my work now. I hope be back soon!
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 37002094
Let me see if I understang,
What code I put inside download.php? the code of php.net that has been mencionated in post 36996153?

Where I put this line:
header('Content-Disposition: attachment; filename="'.basename($file).'"');
readfile($file);
in my code mencionated in post 36996247?
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 37005278
Hi, is correct what I say in my last post?
0
 
LVL 27

Expert Comment

by:skullnobrains
ID: 37005741
yes, the code is the proper one. actually, those 2 lines should be enough by themselves.
just make sure you do not send any output to the browser before you call the header() function.

but again, if you just let the webserver do it's job, the result will be exactly the same, and bugfree
0
 
LVL 9

Expert Comment

by:Vampireofdarkness
ID: 37007191
Some applications have addons for browsers to open the document in the browser (adobe reader, for example). The attachment header will force a download rather than displaying in the browser.
0
 
LVL 3

Assisted Solution

by:Pedro Chagas
Pedro Chagas earned 0 total points
ID: 37049570
Hi, I create 2 files, one where customer have a link to do the download or open the document, the second wull process the request.
index.php:
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>tutorial n 1009 - executar ficheiros em php</title>
</head>
<body>
<p><a href="download.php?download=../../imagem/jpg640x480/DSC_2576.jpg">download image</a></p> 
</body>
</html>

Open in new window


download.php
 
<?php
function downloadFile( $fullPath ){

  // Must be fresh start
  if( headers_sent() )
    die('Headers Sent');

  // Required for some browsers
  if(ini_get('zlib.output_compression'))
    ini_set('zlib.output_compression', 'Off');

  // File Exists?
  if( file_exists($fullPath) ){
   
    // Parse Info / Get Extension
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
   
    // Determine Content Type
    switch ($ext) {
      case "pdf": $ctype="application/pdf"; break;
      case "exe": $ctype="application/octet-stream"; break;
      case "zip": $ctype="application/zip"; break;
      case "doc": $ctype="application/msword"; break;
      case "xls": $ctype="application/vnd.ms-excel"; break;
      case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
      case "gif": $ctype="image/gif"; break;
      case "png": $ctype="image/png"; break;
      case "jpeg":
      case "jpg": $ctype="image/jpg"; break;
      default: $ctype="application/force-download";
    }

    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers
    header("Content-Type: $ctype");
    header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".$fsize);
    ob_clean();
    flush();
    readfile( $fullPath );

  } else
    die('File Not Found');

}

downloadFile($_GET[download]);

?>

Open in new window

0
 
LVL 3

Author Closing Comment

by:Pedro Chagas
ID: 37074924
In my last post I show in snippets codes the right solution, off course based in experts solutions. The goal is, when a new EE user open this question have direct access to the right code.
Thanks for all.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question