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.
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>Deleting a file
Simple file delete example using a form
Migrated forum post
I received an email about deleting a file and I'm posting part of my reply here. I may expand it to be a full tutorial.
The unlink() function deletes files in PHP. http://www.php.net/manual/en/function.unlink.php
You should have read/write privileges to the directory that you're going to be deleting from (chmod 777)
This is a simple example of using a form to delete a file.
Code:
<?php
if ($submit && $deleteimage) {
function Delete_Photo($name){
$delete = unlink($name);
Using fgetscsv function
Reading the data saved in CSV file format
fgetcsv -- Gets line from file pointer and parse for CSV fields
Fgetcsv() example - Read and print entire contents of a CSV file
The field delimiter is assumed to be a comma, unless you specify another delimiter with the optional third parameter.
PHP Code
<?php
$fp = fopen('csvfile.txt','r');
while($line = fgetcsv($fp,'1024',',')){
echo "";
print_r($line);
echo "";
/*
foreach($line as $value){
echo "$value
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) {
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.
In order to use FTP functions with your PHP configuration, you should add the --enable-ftp option when installing PHP 4, and --with-ftp when using PHP 3.
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: