- Saving files with PHP scripts in local applications
- Saving and storing files locally¶
- PHP code: saving a file with fopen / fwrite¶
- PHP File Create/Write
- PHP Create File — fopen()
- Example
- PHP File Permissions
- PHP Write to File — fwrite()
- Example
- PHP Overwriting
- Example
- PHP Append Text
- Example
- Complete PHP Filesystem Reference
- PHP Save File to Folder: Best Practices and Tips for Optimization
- Using file_put_contents() to Save a File to a Specific Folder
- Using move_uploaded_file() to Move a File to a Different Folder
- Using rename() to Move a File from One Folder to Another
- Using fopen() to Create a New File
- Using ZipArchive to Create and Download a Zip File from a Folder in PHP
- Other PHP code examples for saving files to a folder
- Conclusion
Saving files with PHP scripts in local applications
When run in a desktop application, PHP code can access local files on the end user’s computer, so it is possible to save and modify files locally. To load compiled files, please see this page.
Please refer to the Saving Files topic of the General Demonstration for live demonstrations and further explanation about saving local files with your ExeOutput for PHP apps.
Saving and storing files locally¶
PHP provides you with several ways to load and save files. In order to correctly save files, you must ensure that the user has permissions to write to files into the location you want.
For instance, a portable application is generally started from a USB stick and the folder that contains the EXE has write permissions. Your application will be able to save files in that folder. On the contrary, if your application is installed in the Program Files directory, it is generally not allowed to save files in the application’s folder because of the Windows UAC feature.
Thus, ExeOutput for PHP provides you with a storage folder dedicated to your application. The absolute path to this folder can be retrieved with the following php command:
This folder is always available: you can use it to store the settings of your application. You may customize the name of the storage folder in ExeOutput by going to the «Output -> Output Settings» page.
PHP code: saving a file with fopen / fwrite¶
In compiled applications, you must pass absolute paths to the fopen PHP function.
If you want to let your end users choose the path where the file should be saved, you can display a Save As dialog box.
PHP File Create/Write
In this chapter we will teach you how to create and write to a file on the server.
PHP Create File — fopen()
The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files.
If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).
The example below creates a new file called «testfile.txt». The file will be created in the same directory where the PHP code resides:
Example
PHP File Permissions
If you are having errors when trying to get this code to run, check that you have granted your PHP file access to write information to the hard drive.
PHP Write to File — fwrite()
The fwrite() function is used to write to a file.
The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written.
The example below writes a couple of names into a new file called «newfile.txt»:
Example
$myfile = fopen(«newfile.txt», «w») or die(«Unable to open file!»);
$txt = «John Doe\n»;
fwrite($myfile, $txt);
$txt = «Jane Doe\n»;
fwrite($myfile, $txt);
fclose($myfile);
?>?php
Notice that we wrote to the file «newfile.txt» twice. Each time we wrote to the file we sent the string $txt that first contained «John Doe» and second contained «Jane Doe». After we finished writing, we closed the file using the fclose() function.
If we open the «newfile.txt» file it would look like this:
PHP Overwriting
Now that «newfile.txt» contains some data we can show what happens when we open an existing file for writing. All the existing data will be ERASED and we start with an empty file.
In the example below we open our existing file «newfile.txt», and write some new data into it:
Example
$myfile = fopen(«newfile.txt», «w») or die(«Unable to open file!»);
$txt = «Mickey Mouse\n»;
fwrite($myfile, $txt);
$txt = «Minnie Mouse\n»;
fwrite($myfile, $txt);
fclose($myfile);
?>?php
If we now open the «newfile.txt» file, both John and Jane have vanished, and only the data we just wrote is present:
PHP Append Text
You can append data to a file by using the «a» mode. The «a» mode appends text to the end of the file, while the «w» mode overrides (and erases) the old content of the file.
In the example below we open our existing file «newfile.txt», and append some text to it:
Example
$myfile = fopen(«newfile.txt», «a») or die(«Unable to open file!»);
$txt = «Donald Duck\n»;
fwrite($myfile, $txt);
$txt = «Goofy Goof\n»;
fwrite($myfile, $txt);
fclose($myfile);
?>?php
If we now open the «newfile.txt» file, we will see that Donald Duck and Goofy Goof is appended to the end of the file:
Complete PHP Filesystem Reference
For a complete reference of filesystem functions, go to our complete PHP Filesystem Reference.
PHP Save File to Folder: Best Practices and Tips for Optimization
Learn how to save files to a specific folder in PHP with functions such as file_put_contents(), move_uploaded_file(), rename(), fopen(), and ziparchive. Follow best practices for file uploads and optimize your PHP code for efficiency and security.
PHP is a powerful and widely-used programming language for web development. It provides a variety of functions to load and save files, making it easy to work with file systems in web applications. In this blog post, we will discuss how to save files to a specific folder using PHP, covering various functions, best practices, and tips for optimization.
Using file_put_contents() to Save a File to a Specific Folder
One of the simplest and most straightforward ways to save a file to a specific folder in PHP is to use the file_put_contents() function. This function takes two parameters: the path to the file and the data to write to the file.
Here’s an example code snippet showing how to use file_put_contents() to save a file to a specific folder:
file_put_contents('/path/to/file.txt', 'Hello World!');
The above code will save the string “Hello World!” to a file located at /path/to/file.txt .
Using move_uploaded_file() to Move a File to a Different Folder
Another common use case for saving files in PHP is handling file uploads. When a user uploads a file to a web application, the file is typically stored in a temporary location on the server. To move the file to a permanent location, you can use the move_uploaded_file() function.
The move_uploaded_file() function takes two parameters: the temporary path of the uploaded file and the new path to move the file to. Here’s an example code snippet showing how to use move_uploaded_file() to move a file to a different folder:
move_uploaded_file($_FILES['file']['tmp_name'], '/path/to/new/folder/file.txt');
The above code will move the uploaded file to /path/to/new/folder/file.txt .
Using rename() to Move a File from One Folder to Another
If you need to move a file from one folder to another in PHP, you can use the rename() function. This function takes two parameters: the current path of the file and the new path to move the file to.
Here’s an example code snippet showing how to use rename() to move a file from one folder to another:
rename('/path/to/old/folder/file.txt', '/path/to/new/folder/file.txt');
The above code will move the file located at /path/to/old/folder/file.txt to /path/to/new/folder/file.txt .
Using fopen() to Create a New File
To create a new file in PHP, you can use the fopen() function. This function takes two parameters: the path to the file and the mode in which to open the file.
Here’s an example code snippet showing how to use fopen() to create a new file:
$file = fopen('/path/to/new/folder/file.txt', 'w'); fwrite($file, 'Hello World!'); fclose($file);
The above code will create a new file located at /path/to/new/folder/file.txt and write the string “Hello World!” to it.
Using ZipArchive to Create and Download a Zip File from a Folder in PHP
If you need to create and download a zip file from a folder in PHP, you can use the ZipArchive class. This class provides methods to add files to the archive and download the archive.
Here’s an example code snippet showing how to use ZipArchive to create and download a zip file:
$zip = new ZipArchive(); $zip->open('/path/to/new/archive.zip', ZipArchive::CREATE); $zip->addFile('/path/to/folder/file.txt', 'file.txt'); $zip->close(); header('Content-Type: application/zip'); header('Content-disposition: attachment; filename=archive.zip'); readfile('/path/to/new/archive.zip');
The above code will create a new zip file located at /path/to/new/archive.zip , add the file located at /path/to/folder/file.txt to the archive, and then download the archive to the user’s browser.
When working with file uploads in PHP, there are several best practices to keep in mind to ensure that your code is efficient and secure. Here are a few tips:
- Validate file types and sizes: Before accepting a file upload, make sure to validate the file type and size to prevent malicious uploads and ensure that your server can handle the file.
- Rename files to prevent overwriting: To prevent files from being overwritten, consider renaming uploaded files using a unique identifier.
- Store uploaded files outside of the web root: Storing uploaded files outside of the web root can help prevent unauthorized access to the files.
Other PHP code examples for saving files to a folder
In Php , for example, php file put content code example
["more" => ["yes" => "More"]]];$inp = file_get_contents('results.json'); $tempArray = json_decode($inp); array_push($tempArray, $data); $jsonData = json_encode($tempArray); file_put_contents('results.json', $jsonData);
Conclusion
In this blog post, we have covered various functions in PHP that can be used to save files to a specific folder, including file_put_contents() , move_uploaded_file() , rename() , fopen() , and the ZipArchive class. We have also discussed best practices for file uploads, such as validating file types and sizes and renaming files to prevent overwriting. By following these guidelines and tips, you can optimize your file uploads and ensure that your PHP code is efficient and secure.