You are on page 1of 13

PHP AND FILE SYSTEM

INCLUSION PHP FILES IN THE PHP DOCUMENT


include (URL);
include_once (URL);
require (URL);
require_once (URL);

// or include URL;
// or require URL;

Example:
echo "first<br>";
include("second.php");
echo "first<br>";
If URL doesnt exist the require function results fatal error.

FILE MANIPULATION

Check the existence of a file

Create a file

Append to a file

Rename a file

Delete a file

PHP FILE HANDLING


Opening a File
resource fopen (string filename, string mode [, bool use_include_path])

Function fopen() returns a descriptor of a file (unique number) or - false.


$handle = fopen("/home/folder1/file.txt", "w");
$handle = fopen("images/file.gif", "wb");
$handle = fopen("http://somesite/page.php", "r");
Closing a File
bool fclose ( resource handle)
fclose ($handle);

MODES FOR FILE OPEN


mod
e

Description

'r'

Open for reading only; place the file pointer at the beginning of the file.

'r+'

Open for reading and writing; place the file pointer at the beginning of the file.

'w'

Open for writing only; place the file pointer at the beginning of the file and truncate the file to
zero length. If the file does not exist, attempt to create it.

'w+'

Open for reading and writing; place the file pointer at the beginning of the file and truncate
the file to zero length. If the file does not exist, attempt to create it.

'a'

Open for writing only; place the file pointer at the end of the file. If the file does not exist,
attempt to create it.

'a+'

Open for reading and writing; place the file pointer at the end of the file. If the file does not
exist, attempt to create it.

'x'

Create and open for writing only; place the file pointer at the beginning of the file. If the file
already exists, the fopen() call will fail by returning FALSE and generating an error of
level E_WARNING. If the file does not exist, attempt to create it.

'x+'

Create and open for reading and writing; place the file pointer at the beginning of the file. If
the file already exists, the fopen() call will fail by returning FALSE and generating an
error of level E_WARNING. If the file does not exist, attempt to create it.

READ AND WRITE


// file name
$fiiename = "file.txt";
// open a file for reading
$fd = fopen($filename, "r+" ) ;
// read a file content to the $bufer
$bufer = fread($fd, filesize($filename));
$bufer = htmlentities($bufer);
// &lt;
fwrite($bufer);
// return
// close a file
fclose($fd);
// out put a content to the browser
echo $bufer;

-> <

DIRECTORY AND FILE


string getcwd (void);
// Get the current directory
bool chdir ( string $directory );
// change the directory
bool mkdir ( string $pathname [, int $mode ] );
bool rmdir ( string $dirname );
// directory must be empty
bool unlink ( string $filename );
// remove the file
bool file_exists ( string $filename ); // check whether a file or directory exists
bool chmod ( string $filename, int $mode ); //

UPLOAD FILE(S)
form.php
<html> <body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> <br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

PHP.INI SETTINGS FOR FILE UPLOAD


;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;
; Whether to allow HTTP file uploads.

file_uploads = On
; Temporary directory for HTTP uploaded files (will use system default if not specified).

upload_tmp_dir = "c:\TEMP\"
; Maximum allowed size for uploaded files.

upload_max_filesize = 2M

upload_file.php
<?php
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br />";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>

$_FILES

$_FILES["file"]["name"] - the name of the uploaded file


$_FILES["file"]["type"] - the type of the uploaded file
$_FILES["file"]["size"] - the size in bytes of the uploaded file
$_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server
$_FILES["file"]["error"] - the error code resulting from the file upload

RESTRICTIONS ON UPLOAD
<?php
if (

(($_FILES["file"]["type"] == "image/gif") ||
($_FILES["file"]["type"] == "image/jpeg") ||
($_FILES["file"]["type"] == "image/pjpeg"))
&&
($_FILES["file"]["size"] < 20000)) {
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br />";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}

} else {
echo "Invalid file";
}
?>

SAVING
THE
UPLOADED
FILE
<?php
if (

(($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")


|| ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}

} else {
echo "Invalid file";
}
?>

You might also like