FILE :

To print the file ,use print_r($_FILES) in php file.

For this set enctype="multipart/form-data" in form action.

example:
  	
			<?php
				print_r($_FILES);
			?>
		
Output :
Array ( [files_name] => Array ( [name] => pg_db.txt [type] => text/plain [tmp_name] => /tmp/phpQpMvE4 [error] => 0 [size] => 1360128 ) )

UPLOAD FILE

First you have to upload the file and copy it to a location.

Syntax $file=$_FILES['files_name']['tmp_name'];
$dest_file=$_FILES['files_name']['name'];
copy($file,'/var/www/html/tutorials/PHP/images/'.$dest_file);

DOWNLOAD FILE

Syntax $dfile="/var/www/html/tutorials/PHP/images/pg_db.txt";
header("Content-type:plain/text");
header("Content-Disposition:attachment;filename=pg_db.txt");
readfile($dfile);
Various format of downloadimg file
$file_extension = strtolower(substr(strrchr($filename,"."),1));
switch( $file_extension ) {
case "pdf": $ctype="Content-Type:application/pdf"; break;
case "exe": $ctype="Content-Type:application/octet-stream"; break;
case "zip": $ctype="Content-Type:application/zip"; break;
case "doc": $ctype="Content-Type:application/msword"; break;
case "xls": $ctype="Content-Type:application/vnd.ms-excel"; break;
case "ppt": $ctype="Content-Type:application/vnd.ms-powerpoint"; break;
case "gif": $ctype="Content-Type:image/gif"; break;
case "png": $ctype="Content-Type:image/png"; break;
case "jpeg":$ctype="Content-Type:image/jpeg"; break;
case "jpg": $ctype="Content-Type:image/jpg"; break;
case "mp3": $ctype="Content-Type:audio/mpeg"; break;
case "wav": $ctype="Content-Type:audio/x-wav"; break;
case "mpe": $ctype="Content-Type:video/mpeg"; break;
case "mov": $ctype="Content-Type:video/quicktime"; break;
case "avi": $ctype="Content-Type:video/x-msvideo"; break;
case "xml": $ctype="Content-Type:text/xml"; break;
case "css": $ctype="Content-Type:text/css"; break;
case "js": $ctype="Content-Type:application/javascript"; break;
case "csv":$ctype="Content-Type: application/csv";break;
case "txt": $ctype="Content-type: text/plain";break;
default: $ctype="Content-type:application/force-download";
}

move_uploaded_file

Syntax move_uploaded_file ( string filename, string destination);
filename:The filename of the uploaded file.
destination:The destination of the moved file.
example:
  	
			<?php
				$file=$_FILES['files_name']['tmp_name'];
				$dest_file=$_FILES['files_name']['name'];
				define('ROOT',"/var/www/html/tutorials/PHP/images/");
				move_uploaded_file($file,ROOT.$dest_file);
			?>
		

Unlink


The unlink() function deletes a file.
This function returns TRUE on success, or FALSE on failure.
Syntax bool unlink ( string $filename [, resource $context ] )
example:
  	
			<?php
				$fh = fopen('test.html', 'a');
				fwrite($fh, '

Hello world!

'); fclose($fh); unlink('test.html'); ?>

Set Permission


For permission set , use chmod function
Syntax bool chmod ( string filename, int mode)
example:
  	
			<?php
				// Read and write for owner, nothing for everybody else
				chmod("/somedir/somefile", 0600);

				// Read and write for owner, read for everybody else
				chmod("/somedir/somefile", 0644);

				// Everything for owner, read and execute for others
				chmod("/somedir/somefile", 0755);

				// Everything for owner, read and execute for owner group
				chmod("/somedir/somefile", 0750);
			?>
		

File Function


1.fopen
Syntax fopen( string filename, string mode [, int use_include_path [, resource zcontext]])
mode             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.
example:
  	
			<?php
				$handle = fopen("/home/rasmus/file.txt", "r");
			?>
		

2.fclose
Closes an open file pointer
Syntax fclose($handle);

3.fread

fread() reads up to length bytes from the file pointer referenced by handle. Reading stops when length bytes have been read, EOF (end of file) is reached.

Syntax string fread ( resource handle, int length);

example:
  	
			<?php
				// get contents of a file into a string
				$filename = "/usr/local/something.txt";
				$handle = fopen($filename, "r");
				$contents = fread($handle, filesize($filename));
				fclose($handle);
			?>
		

4.fwrite()

writes the contents of string to the file stream pointed to by handle. If the length argument is given, writing will stop after length bytes have been written or the end of string is reached, whichever comes first. fwrite() returns the number of bytes written, or FALSE on error.

example:
  	
			<?php
				$fp = fopen('data.txt', 'w');
				fwrite($fp, '1');
				fwrite($fp, '23');
				fclose($fp);
				// the content of 'data.txt' is now 123 and not 23!
			?>
		

5.fgets()
Syntax string fgets ( resource handle [, int length])
Returns a string of up to length - 1 bytes read from the file pointed to by handle. Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first). If no length is specified, the length defaults to 1k, or 1024 bytes.

example:
  	
			<?php
				$handle = fopen("/tmp/inputfile.txt", "r");
				while (!feof($handle)) {
					$buffer = fgets($handle, 4096);
					echo $buffer;
				}
				fclose($handle);
			?>
		

6.fgetcsv()
Syntax array fgetcsv ( resource handle, int length [, string delimiter [, string enclosure]])
Same as fgets() except that fgetcsv() parses the line it reads for fields in CSV format and returns an array . The optional third delimiter parameter defaults as a comma, and the optional enclosure defaults as a double quotation mark.

example:
  	
			<?php
				$handle = @fopen(ROOT."holidays.csv", "r");
            if ($handle) {
               while (!feof($handle)) {
                   $buffer = fgetcsv($handle, 4096);
                  print_r($buffer);
               }
               fclose($handle);
            }
			?>