php execute programs

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
LVL 3
Pedro ChagasWebmasterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
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
Pedro ChagasWebmasterAuthor Commented:
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
käµfm³d 👽Commented:
Are user machine and server machine the same machine?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

VampireofdarknessCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pedro ChagasWebmasterAuthor Commented:
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
VampireofdarknessCommented:
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
Marco GasiFreelancerCommented:
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
Marco GasiFreelancerCommented:
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
skullnobrainsCommented:
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
Pedro ChagasWebmasterAuthor Commented:
Who can help me to put in practice the solution of @Vampireofdarkness with my code?
0
skullnobrainsCommented:
$file=$_GET['file']; # assuming you sent the file as ...?file=/path/to/file

header('Content-Disposition: attachment; filename="'.basename($file).'"');
readfile($file);
0
VampireofdarknessCommented:
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
Pedro ChagasWebmasterAuthor Commented:
Point in the right direction.
I don't say nothing yet because I star my work now. I hope be back soon!
0
Pedro ChagasWebmasterAuthor Commented:
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
Pedro ChagasWebmasterAuthor Commented:
Hi, is correct what I say in my last post?
0
skullnobrainsCommented:
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
VampireofdarknessCommented:
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
Pedro ChagasWebmasterAuthor Commented:
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
Pedro ChagasWebmasterAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.