Your ExeOutput for PHP application may need to work with local files. Since you can access local files directly with PHP, traditional file upload functions are often unnecessary. Your application is not designed to work like a web server; users do not have to upload local files to a remote server for your application’s PHP code to process them.
This topic shows you how to prompt a user to select a file and then process it with PHP.
Tip
Of course, ExeOutput for PHP also supports traditional file uploads. You can see the General Demonstration for live demonstrations and further explanations about uploading files with your ExeOutput for PHP apps.
Prompting the User with an “Open File” Dialog Box #
On the web, when a user needs to upload a file, the browser prompts them to select it with an “Open” dialog box. You can achieve the same result in an ExeOutput for PHP application.
The Open dialog box lets the user specify the drive, directory, and name of a file (or files) to open.
It can be invoked with the OpenFileDialog HEScript command.
Step 1: Create the HEScript #
Paste the following code into it:
function OpenDlgFile: String; begin Result := OpenFileDialog("Select a File to open", "*.*", "*.*", "All files (*.*)|*.*", ""); end;
This code calls the built-in OpenFileDialog HEScript command, which is described in the Script Reference topic.
function OpenFileDialog(const aTitle, aFilename, aDefaultExt, aFilter, aInitialDir: String): String;
- aTitle: Title of the dialog box.
- aFilename: Default filename.
- aDefaultExt: Default extension.
- aFilter: File extension filter.
- aInitialDir: Initial directory.
Result: Returns the full path to the selected file, or an empty string if the dialog is canceled.
- Click Save Script. The HEScript function is now ready to be called from PHP or JavaScript.
Step 2: Call from PHP #
<?php
echo "Asking for filename...<br>";
// Executes HEScript to call the system "Open" dialog...
$filename = exo_return_hescriptcom("UserMain.OpenDlgFile", "Error");
// The $filename variable contains the full path to the file selected by the user.
// It can then be passed to functions like `fopen`.
if (empty($filename)) {
echo "You have not selected a file. Operation canceled.<br>";
return;
}
// Example: load the selected file into a PDF library.
$pdf->load($filename);
?>Warning
Virtual files (in the Data subfolder) will not appear in the “Open” dialog box unless you activate the Do not hide virtual files in open/save dialog boxes security option.