You can use a browser to upload files with PHP. The files can be either text and binary files. PHP has several file manipulation functions, that allow you to control what you do with a file once it has been uploaded, how large the file to be uploaded should be, where it should go after being uploaded etc.
To upload files, create a file upload form:
Example:
<FORM ENCTYPE="multipart/form-data" ACTION="somephpscript.php3" METHOD=POST> <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000"> Send this file: <INPUT NAME="userfile" TYPE="file">
Fetching and saving the contents of a remote file
Submit a query to a search engine and save the results to a local file
1. Create a form that submits a query to a search engine.
2. Script should automatically save the query results to a file. Each query should be saved to separate files.
Figuring out the url of the remote file/search engine that you will submit your query to:
Downloading Files
Using the header function to force file downloads
If you have files that you would like users to be able to download, it can be done simply by providing a link to the file. When the link is clicked, the browser will prompt the user to download the file or it will display it if the file type is known to the browser i.e. text files and gif/jpeg images. If you do not want the files displayed in the browser you can use the header() in PHP with the appropriate content type to force a file download.
Working with directories
Directories: Open, read, close and view a list of files
An explanation of how to open a directory, read the contents of a directory, and close a directory.
Function opendir (string path)
The opendir() function opens a directory. It takes the path of the directory (can be just the name of the directory) as its parameter, and returns a directory handle that can be used by
the closedir(), readdir(), and rewinddir() functions.
Example:
$dir = opendir("test");
Editing text files with PHP
Editing a file using a textarea
Sometimes you can't download/upload a file but must instead edit a file online. You can use a textarea to display the contents of a file, edit the file, then save your changes to the file that you opened.
You should should have this script in a secured directory and add code to make sure it is not used in a manner that you would not want it to be used. Be cautioned that this script could cause serious harm, depending on the code that is added to the edited file.
<?php if ($submit) {
Using a header and footer for an entire site
If you have a site with many pages, or even one with few pages, and you would like not to update all your HTML files when the site design changes, you can use the include() function to implement a header and footer file that will be used on all your pages.
Password protecting a file using PHP functions
How to password protecting a file using FTP functions
There is an alternative to using .htaccess and .htpassword to password protect files.
PHP can mimic the HTTP authentication process by setting the appropriate headers for the username/password dialog box to pop up. The values entered in the box are stored in the $PHP_AUTH_USER, $PHP_AUTH_PW and $PHP_AUTH_TYPE variables.
To see if your PHP installation supports FTP Functions you can run the phpinfo() function and look for "FTP Functions" :
PHP Code:
<?php phpinfo(); ?>
Reading data from text files with PHP
Before you read data stored in a file, use the fopen() function to open the file.
To read the data from the file, use fopen() in [e]read mode. After opening the
file, assign the contents of the file to a variable using the file() function. You can then print the contents of the file, then use the fclose() function to close the file after you are finished reading the data.
Function file(string filename [, int use_include_path])
The function reads all the contents of a file into an array. Each line of the file is a
Creating text files with PHP
PHP has file handling functions that can create, read or delete files. To to create a text file several functions are used together. The fopen() function, opens a file for writing, the fputs() function
to actually write information to a file, and the fclose() function to close the file when it is no longer
going to be used.
Function fopen(string filename, string mode)
The function expects to be given the name of file, and the reading/writing mode of the file, and returns a file handler.
Note: Modes that can be used to create a new text file are:
Creating and deleting directories
Script showing how to create/delete directory
Migrated forum post
Script showing how to create/delete dirs.
Code:
This creates a directory that has a mode of 777.
PHP Code:
<?php if ($makedir == "MakeDir") { $newdir = "$dirname"; $dirmake = mkdir("$newdir", 0777); print "<a href="$newdir">View New Directory</a>n"; } ?> <form action="<? $PHP_SELF; ?>"> <input type="text" name="dirname"> <input type="submit" name="makedir" value="MakeDir"> </form>