0% found this document useful (0 votes)
118 views6 pages

BSC VC (Web Designing) 2nd Year 3rd Unit (PHP)

The document covers data and file handling in PHP, including the use of superglobal arrays for form data, input and output controls, and validation methods. It explains cookies and sessions for maintaining user state, along with file operations such as reading, writing, uploading, and downloading files. Key functions and workflows for managing files and directories are also detailed, emphasizing their importance in dynamic web applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views6 pages

BSC VC (Web Designing) 2nd Year 3rd Unit (PHP)

The document covers data and file handling in PHP, including the use of superglobal arrays for form data, input and output controls, and validation methods. It explains cookies and sessions for maintaining user state, along with file operations such as reading, writing, uploading, and downloading files. Key functions and workflows for managing files and directories are also detailed, emphasizing their importance in dynamic web applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DATA AND FILE HANDLING

In PHP, data handling involves working with variables, arrays, and forms to process user input and
interact with databases. File handling includes reading, writing, uploading, and manipulating files on
the server. These operations are essential for building dynamic web applications that handle data
and files efficiently.

PHP FORMS
PHP provides several superglobal arrays to access form data, server information, and environment
variables:

1. $_GET:

- Contains data sent to the server via the URL query string (e.g., form submissions with
method="get").

- Accessed using the key-value pairs in the URL after the "?" symbol.

2. $_POST:

- Contains data sent to the server via HTTP POST method (e.g., form submissions with
method="post").

- Provides a more secure way to transmit sensitive data, as it's not visible in the URL.

3. $_REQUEST:

- Merges the contents of $_GET, $_POST, and $_COOKIE superglobal arrays.

- Can be used to access data regardless of the form submission method.

4. $_FILES:

- Contains information about uploaded files via HTTP POST method (e.g., file uploads through
forms with enctype="multipart/form-data").

- Provides details such as file name, type, size, and temporary location.

5. $_SERVER:

- Contains information about server and execution environment.

- Includes details such as server name, request method, script filename, etc.

6. $GLOBALS:

- A super global array that contains references to all variables defined in the global scope of the
script.

- Allows access to global variables from anywhere in the script.

7. *$_ENV:*
- Contains environment variables set in the server configuration.

- Provides access to system environment variables like PATH, HOME, etc.

Using these super global arrays, PHP developers can access and manipulate form data, server
information, and environment variables within their scripts, enabling dynamic and interactive web
application development.

INPUT-OUTPUT CONTROLS

Input Controls

It is used to take input from the user. Some of them are as follows:

1. HTML Forms: Use <form> elements to collect user input.

2. $_GET: Retrieves form data sent in the URL query string.

3. $_POST: Retrieves form data sent via HTTP POST method.

4. $_REQUEST: Merges $_GET, $_POST, and $_COOKIE arrays.

Output Controls

It is used to output for the user. Some of them are as follows:

1. echo: Outputs one or more strings.

2. print: Outputs a string.

3. printf: Outputs a formatted string.

4. print_r: Outputs human-readable information about a variable.

5. var_dump: Outputs structured information about a variable.

VALIDATION

Client-Side Validation

1. Implemented using JavaScript to provide immediate feedback to users.


2. Validates input before submitting the form to the server.
3. Enhances user experience but can be bypassed by disabling JavaScript.
Server-Side Validation

1. Implemented using PHP to ensure data integrity and security.


2. Validates input after form submission on the server.
3. Checks for data format, length, range, and presence.
4. Prevents malicious input and ensures consistent data processing.

COOKIES AND SESSIONS

Cookies

1. Small pieces of data stored on the client's browser.


2. Used to track user activity, personalize content, and maintain session state.
3. Set using the setcookie() function and accessed via the $_COOKIE superglobal array.
4. Can store limited data and have an expiration time.

Sessions

1. Server-side mechanism to track user interactions across multiple requests.


2. Stores session data on the server and assigns a unique session ID to each client.
3. Maintains session state until the user closes the browser or logs out.
4. Accessed using the $_SESSION superglobal array.
5. More secure than cookies for storing sensitive information.

Example Workflow

1. Input Controls: Create an HTML form to collect user data.

2. Validation: Implement server-side validation in PHP to ensure data integrity.

3. Output Controls: Use PHP functions like echo or print_r to display processed data.

4. Cookies and Sessions: Set cookies or start sessions to maintain user state or store preferences.

FILE HANDLING IN PHP


File handling in PHP involves reading, writing, uploading, and manipulating files and directories on
the server. Key functions include file_get_contents, file_put_contents, move_uploaded_file,
rename, unlink, mkdir, rmdir, and scandir. These operations enable developers to manage files
efficiently in PHP applications.

FILE AND DIRECTORY


In PHP, file and directory operations involve opening, closing, reading, writing, appending, and
deleting files, as well as managing directories. Additionally, uploading and downloading files are
common tasks in web development. Here's a brief overview of these operations:

1. Open File: Use fopen() to open a file for reading, writing, or appending.

2. Close File: Use fclose() to close an open file handle when done.

3. Read File: Use functions like fread() or file_get_contents() to read the contents of a file.

4. Write to File: Use functions like fwrite() or file_put_contents() to write data to a file.

5. Append to File: Use 'a' mode with fopen() to append data to a file without overwriting existing
content.

6. Delete File: Use unlink() to delete a file from the server.

7. Create Directory: Use mkdir() to create a new directory.

8. Remove Directory: Use rmdir() to remove an empty directory.

9. List Directory Contents: Use scandir() to list files and directories within a directory.

UPLOADING FILES
1. HTML Form:
Create a form with enctype="multipart/form-data" to allow file uploads.

2. Move Uploaded File:


Use move_uploaded_file() to move the uploaded file from temporary storage to a permanent
location on the server.

DOWNLOADING FILES
1. Send File to Browser:
Use readfile() or fopen() with fpassthru() to send a file to the browser for download.

Example Workflow

1. Uploading Files:

- Create an HTML form with a file input field for users to upload files.

- Use PHP to handle the form submission, move the uploaded file to a designated directory, and
perform any necessary validation.

2. Downloading Files:
- Generate a link or button that triggers a PHP script to send the desired file to the browser for
download.

- Ensure appropriate headers are set to specify the file type and trigger a download prompt.

FILE EXISTENCE
To check if a file exists, you can use the file_exists() function:

php

$filename = "example.txt";

if (file_exists($filename)) {

echo "File exists.";

} else {

echo "File does not exist.";

FILE SIZE
To get the size of a file in bytes, you can use the filesize() function:

php

$filename = "example.txt";

$filesize = filesize($filename);

echo "File size: $filesize bytes";

FILE RENAME
To rename a file, you can use the rename() function:

php

$oldname = "oldfile.txt";

$newname = "newfile.txt";

if (rename($oldname, $newname)) {
echo "File renamed successfully.";

} else {

echo "Error renaming file.";

READING AND DISPLAYING FILES IN A DIRECTORY


To read and display all files in a directory, you can use the scandir() function to get a list of files, then
loop through the list and display each file:

php

$directory = "path/to/directory";

$files = scandir($directory);

foreach ($files as $file) {

if (!is_dir($file)) {

echo "$file<br>";

To display only selected files based on certain criteria (e.g., file extension), you can add conditions
inside the loop:

php

$directory = "path/to/directory";

$files = scandir($directory);

foreach ($files as $file) {

if (!is_dir($file) && pathinfo($file, PATHINFO_EXTENSION) == 'txt') {

echo "$file<br>";

You might also like