SlideShare a Scribd company logo
PHP AND FILE SYSTEM
INCLUSION PHP FILES IN THE PHP DOCUMENT
include (URL); // or include URL;
include_once (URL);
require (URL); // or require URL;
require_once (URL);
Example:
echo "first<br>";
include("second.php");
echo "first<br>";
If URL doesn’t 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
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.
'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";
}
?>

More Related Content

PPTX
Ch3(working with file)
Chhom Karath
 
PPTX
Php File Operations
Jamshid Hashimi
 
PPTX
Uploading a file with php
Muhamad Al Imran
 
PPTX
File upload for the 21st century
Jiří Pudil
 
XLS
Up.Php
wsoom
 
PPT
Php File Operations
mussawir20
 
ODT
Huong dan cai dat hadoop
Quỳnh Phan
 
PDF
Mengembalikan data yang terhapus atau rusak pada hardisk menggunakan ubuntu
Alferizhy Chalter
 
Ch3(working with file)
Chhom Karath
 
Php File Operations
Jamshid Hashimi
 
Uploading a file with php
Muhamad Al Imran
 
File upload for the 21st century
Jiří Pudil
 
Up.Php
wsoom
 
Php File Operations
mussawir20
 
Huong dan cai dat hadoop
Quỳnh Phan
 
Mengembalikan data yang terhapus atau rusak pada hardisk menggunakan ubuntu
Alferizhy Chalter
 

What's hot (19)

PPT
File Upload
webhostingguy
 
ODP
Php File Upload
Hiroaki Kawai
 
PPTX
File Uploading in PHP
Idrees Hussain
 
PPT
Php i basic chapter 4
Muhamad Al Imran
 
PPT
php file uploading
Purushottam Kumar
 
PPT
05 File Handling Upload Mysql
Geshan Manandhar
 
PPT
PHP file
tumetr1
 
PPTX
On secure application of PHP wrappers
Positive Hack Days
 
PDF
Filesystem Abstraction with Flysystem
Frank de Jonge
 
PPT
Intro to php
Sp Singh
 
PPTX
Php talk
Jamil Ramsey
 
TXT
Cpsh sh
Ben Pope
 
PPT
Intro to PHP
Sandy Smith
 
PPTX
Quick beginner to Lower-Advanced guide/tutorial in PHP
Sanju Sony Kurian
 
PDF
TDC São Paulo 2016 - Become a jedi with php streams
Matheus Marabesi
 
PDF
scdevsumit 2016 - Become a jedi with php streams
Matheus Marabesi
 
PPTX
Basics of Unix Adminisration
Venkateswarlu Malleboina
 
PDF
Not Really PHP by the book
Ryan Kilfedder
 
PPT
Filing system in PHP
Mudasir Syed
 
File Upload
webhostingguy
 
Php File Upload
Hiroaki Kawai
 
File Uploading in PHP
Idrees Hussain
 
Php i basic chapter 4
Muhamad Al Imran
 
php file uploading
Purushottam Kumar
 
05 File Handling Upload Mysql
Geshan Manandhar
 
PHP file
tumetr1
 
On secure application of PHP wrappers
Positive Hack Days
 
Filesystem Abstraction with Flysystem
Frank de Jonge
 
Intro to php
Sp Singh
 
Php talk
Jamil Ramsey
 
Cpsh sh
Ben Pope
 
Intro to PHP
Sandy Smith
 
Quick beginner to Lower-Advanced guide/tutorial in PHP
Sanju Sony Kurian
 
TDC São Paulo 2016 - Become a jedi with php streams
Matheus Marabesi
 
scdevsumit 2016 - Become a jedi with php streams
Matheus Marabesi
 
Basics of Unix Adminisration
Venkateswarlu Malleboina
 
Not Really PHP by the book
Ryan Kilfedder
 
Filing system in PHP
Mudasir Syed
 
Ad

Similar to File system (20)

PPTX
Unit3IIpartpptx__2024_10_17_19_07_58 2.pptx
harleensingh985
 
PPSX
DIWE - File handling with PHP
Rasan Samarasinghe
 
PDF
Web Development Course: PHP lecture 3
Gheyath M. Othman
 
PPT
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
PPTX
PHP File Handling
Degu8
 
DOCX
Php files
kalyani66
 
DOCX
Php advance
Rattanjeet Singh
 
PPT
Files and Directories in PHP
Nicole Ryan
 
PPTX
lecture 11.pptx
ITNet
 
PPTX
4 - Files and Directories - Pemrograman Internet Lanjut.pptx
MasSam13
 
PDF
Perl Programming - 03 Programming File
Danairat Thanabodithammachari
 
PPTX
object oriented programming in PHP & Functions
BackiyalakshmiVenkat
 
PDF
File Handling.pdffile handling ppt final
e13225064
 
PPTX
PHP fundamnetal in information technology CHapter -02.pptx
worldchannel
 
PPTX
Secure PHP Coding - Part 1
Vinoth Kumar
 
PDF
RSS Application Using Dom
abdullah roomi
 
PPT
Lecture 20 - File Handling
Md. Imran Hossain Showrov
 
PPTX
File in C language
Manash Kumar Mondal
 
Unit3IIpartpptx__2024_10_17_19_07_58 2.pptx
harleensingh985
 
DIWE - File handling with PHP
Rasan Samarasinghe
 
Web Development Course: PHP lecture 3
Gheyath M. Othman
 
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
PHP File Handling
Degu8
 
Php files
kalyani66
 
Php advance
Rattanjeet Singh
 
Files and Directories in PHP
Nicole Ryan
 
lecture 11.pptx
ITNet
 
4 - Files and Directories - Pemrograman Internet Lanjut.pptx
MasSam13
 
Perl Programming - 03 Programming File
Danairat Thanabodithammachari
 
object oriented programming in PHP & Functions
BackiyalakshmiVenkat
 
File Handling.pdffile handling ppt final
e13225064
 
PHP fundamnetal in information technology CHapter -02.pptx
worldchannel
 
Secure PHP Coding - Part 1
Vinoth Kumar
 
RSS Application Using Dom
abdullah roomi
 
Lecture 20 - File Handling
Md. Imran Hossain Showrov
 
File in C language
Manash Kumar Mondal
 
Ad

Recently uploaded (20)

PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Doc9.....................................
SofiaCollazos
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 

File system

  • 1. PHP AND FILE SYSTEM
  • 2. INCLUSION PHP FILES IN THE PHP DOCUMENT include (URL); // or include URL; include_once (URL); require (URL); // or require URL; require_once (URL); Example: echo "first<br>"; include("second.php"); echo "first<br>"; If URL doesn’t exist the ‘require’ function results fatal error.
  • 3. FILE MANIPULATION • Check the existence of a file • Create a file • Append to a file • Rename a file • Delete a file
  • 4. 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);
  • 5. MODES FOR FILE OPEN 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. '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.
  • 6. 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;
  • 7. 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 ); //
  • 8. 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>
  • 9. 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
  • 10. 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"]; } ?>
  • 11. $_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
  • 12. 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"; } ?>
  • 13. 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"; } ?>