loilo/native-open

Open a file/URL/app from inside PHP, cross-platform

1.0.0 2019-08-19 08:43 UTC

This package is auto-updated.

Last update: 2024-11-09 20:34:08 UTC


README

native-open logo: A mouse pointer, presented as a hand with extended index finger, floating above a colored circle representing an interactive area.

Native Open

This package enables you to open a file/URL/app from inside PHP, cross-platform.

Note that it opens the app on the machine running the PHP code, so you can not use it to open apps on behalf of your website's users.

Most of the credit for this package goes to Sindre Sorhus as this is mostly a port of his open package for Node.js.

Installation

composer require loilo/native-open

Usage

use Loilo\NativeOpen\NativeOpen;

// Opens the image in the default image viewer
NativeOpen::open('picture.jpg'); 

// Opens the URL in the default browser
NativeOpen::open('https://github.com/loilo');

// Opens the URL in a specified browser
NativeOpen::open('https://github.com/loilo', 'firefox');

// Specify app arguments
NativeOpen::open('https://github.com/loilo', 'google chrome', ['--incognito']);

This package uses the open command on macOS, start on Windows and xdg-open on other platforms. Note that, if you define a specific app to open a target in, the app name is platform dependent. Don't hard code it in reusable modules. For example, Chrome is google chrome on macOS, google-chrome on Linux and chrome on Windows.