$_FILES: used for file uploading $_SERVER: is a superglobal associative array that contains information Validation vs.
contains information Validation vs. Sanitization : Validation checks if the data is correct and
$fileName = $_FILES["myfile"]["name"]; // original file name about headers, paths, and request method. sanitization check for clean data.
$tmpName = $_FILES["myfile"]["tmp_name"]; // temporary PHP_SELF Filename of the currently executing script JSON_ENCODING: serializations
location Values in Server: $data = [“name” => “Hadia”];
move_uploaded_file($tmpName, "uploads/" . $fileName); PHP_SELF file name of the currently executing script $json = json_encode($data);
echo "File uploaded!"; SCRIPT_NAME The path of the current script JSON_DECODING: deserialization
Second Example: SERVER_NAME Name of the host server $arr = json_decode($json, true);
if(isset($_FILES["photo"])){ SERVER_ADDR IP address of the host server
$fileName = $_FILES["photo"]["name"]; HTTP_HOST Host header from the current request File Operations: Difference between Filter_input
$fileType = $_FILES["photo"]["type"]; HTTP_REFERER The complete URL of the current page Open and close and filter_var (Validation)
$fileSize = $_FILES["photo"]["size"]; REMOTE_ADDR IP address from where the user is viewing the current $file = fopen("[Link]", "r"); Filter_input() – getting the data
$tmpName = $_FILES["photo"]["tmp_name"]; page fclose($file); from superglobals
<?php Read line by line Filter_var() validating a normal
// Allow only JPG or PNG print "PHP_SELF: ".$_SERVER['PHP_SELF']; while(!feof($file)){ variable
if($fileType == "image/jpeg" || $fileType == "image/png"){ print "<br>"; echo fgets($file);} Filter_var: Validation
print "SERVER_NAME: ".$_SERVER['SERVER_NAME']; Write: $email = "hadia@[Link]";
// Check file size (max 2MB) print "<br>"; fwrite($file, "Hello");
if($fileSize <= 2000000){ print "HTTP_HOST: ".$_SERVER['HTTP_HOST']; Whole File: if(filter_var($email,
print "<br>"; $content = FILTER_VALIDATE_EMAIL)){
move_uploaded_file($tmpName, "uploads/" . $fileName); print "SCRIPT_NAME: ".$_SERVER['SCRIPT_NAME']; file_get_contents("[Link]"); echo "Valid Email";
echo "Image uploaded successfully!"; ?> file_put_contents("[Link]", "New }else{
<?php text"); echo "Invalid Email";
} else { if ($_SERVER["REQUEST_METHOD"] == "POST") { Check if file exists }
echo "File too large!"; // collect value of input field if(file_exists("[Link]")){
} $name = htmlspecialchars($_REQUEST['fname']); echo "Exists";}
if (empty($name)) { Filter_input : Validation
} else { echo "Name is empty"; $email = filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL);
echo "Only JPG and PNG allowed!"; } else { if($email){
} echo $name; echo "Valid Email";
} } }else{
} echo "Invalid Email";}
process uploaded file: ?>
if ($_SERVER['REQUEST_METHOD'] === 'POST’) { Filter_input(sanitization): Fget example:
$uploadDir = 'F:/temp/uploads/'; $name = filter_input(INPUT_POST, "name", $fileName = "F:/temp/[Link]";
$uploadFile = $uploadDir . basename($_FILES['userfile']['name']); FILTER_SANITIZE_SPECIAL_CHARS); $file = fopen($fileName, "r"); // Open for reading
if (move_uploaded_file($_FILES['userfile']['tmp_name'], echo $name; if ($file) {
$uploadFile)) { Filter_var (Saniztization): while (!feof($file)) {
echo "File is valid, and was successfully uploaded.\n"; $email = filter_var("hadia@@[Link]", FILTER_SANITIZE_EMAIL); $line = fgets($file);
} else { echo $email; echo $line;
echo "File upload failed.\n"; }
File Size limit: check file size in bytes Fwrite Example: fclose($file); // Close the file
if ($_FILES[“userfile"]["size"] > 500000) { $fileName = "F:/temp/[Link]"; } else {
echo "Sorry, your file is too large.";} $file = fopen($fileName, "w"); // Open for writing echo "Failed to open the file.";
Check if File Already Exists $cities = ["Toronto", "Hamilton", "Burlington"]; }
if (file_exists($uploadFile)) { if ($file) { File_put_contents
echo "Sorry, file already exists."; foreach ($cities as $item) { $fileName = "F:/temp/[Link]";
fwrite($file, $item.PHP_EOL);} $cities = ["Toronto", "Hamilton", "Burlington"];
fclose($file); // Close the file $result = file_put_contents($fileName, join(PHP_EOL, $cities));
echo "File created and written successfully."; if ($result) {
} else {echo "Failed to open the file.";} echo "File created and written successfully.";
} else {
echo "Failed to open the file."
File_get_contents
$fileName = "F:/temp/[Link]";
$result = file_get_contents($fileName);
if ($result) {
echo $result;
} else {
echo "Failed to open the file.";