You must first build an HTML form that lets users select a file to upload.

<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

  • enctype="multipart/form-data" - Necessary for our to-be-created PHP file to function properly. The enctype attribute specifies how form-data should be encoded before sending it to the server. See table below.
  • action="uploader.php" - The name of our PHP page that will be created, shortly.
  • method="POST" - Informs the browser that we want to send information to the server using POST.
  • input type="hidden" name="MA... - Sets the maximum allowable file size, in bytes, that can be uploaded.
  • input name="uploadedfile" - uploadedfile is how we will access the file in our PHP script.

Attribute Values

Value Description
application/x-www-form-urlencoded All characters are encoded before sent (this is default)
multipart/form-data No characters are encoded. This value is required when you are using forms that have a file upload control
text/plain Spaces are converted to "+" symbols, but no special characters are encoded

Save that form code into a file and call it upload.html.
After the user clicks submit, the data will be posted to the server and the user will be redirected to uploader.php

When the uploader.php file is executed, the uploaded file exists in a temporary storage area on the server. If the file is not moved to a different location it will be destroyed!

Here is uploader.php

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}

The $_FILES array is where PHP stores all the information about files.

  • uploadedfile - uploadedfile is the reference we assigned in our HTML form.
  • $_FILES['uploadedfile']['name'] - name contains the original path of the user uploaded file.
  • $_FILES['uploadedfile']['tmp_name'] - tmp_name contains the path to the temporary file that resides on the server. The file should exist on the server in a temporary directory with a temporary name.
|
If we want to add on to a file we need to open it up in append mode.

$myFile = "testFile.txt";
$fh = fopen($myFile, 'a');

Then we use fwrite() to write to the file. It would begin writing data at the end of the file.

$myFile = "testFile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "Hello\n";
fwrite($fh, $stringData);
$stringData = "We are vn4000\n";
fwrite($fh, $stringData);
fclose($fh);
|
We use unlink() to delete existing files

Before you can delete a file, you must first be sure that it is not open in your program. Use the fclose function to close down an open file.

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fclose($fh);
$myFile = "testFile.txt";
unlink($myFile);
|
The fgets() functions is used to read a line from a file. Using this function we either read the entire line into a string or specify the number characters we like to read.

fgets ($handle, $length);

  • $handle - the file pointer
  • $length - number of bytes to read. If length is not specified it will read at the end of the line.

Reading entire line

<?php
$fh = fopen("myfile.txt", "r");

$line = fgets($fh);
echo $line;

fclose($fh);
?>

Reading number of bytes

<?php
$fh = fopen("myfile.txt", "r");

$str = fgets($fh, 64);
echo $str;

$line2 = fgets($fh, 64);
echo $str;

fclose($fh);
?>

Reading file line by line

<?php
$fh = fopen("myfile.txt", "r");

while(true)
{
$line = fgets($fh);
if($line == null)break;

echo $line;
}

fclose($fh);
?>

Reading entire file at once

<?php
$fh = fopen("myfile.txt", "r");

$file = file_get_contents("myfile.txt");
echo $file;
?>
|
We can easily write text to files using the fwrite() function. However, you must make sure to open file in writable mode before writing.

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Hello\n";
fwrite($fh, $stringData);
$stringData = "Welcome to vn4000\n";
fwrite($fh, $stringData);
fclose($fh);
|