News:

SMF - Just Installed!

Main Menu

Recent posts

#1
PHP Programing / Closer To Home (I'm Your Capta...
Last post by KingSparta - Apr 06, 2024, 02:11 PM
#2
PHP Programing / Validate Form Data With PHP
Last post by KingSparta - Dec 21, 2023, 03:45 PM
<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $name = test_input($_POST["name"]);
  $email = test_input($_POST["email"]);
  $website = test_input($_POST["website"]);
  $comment = test_input($_POST["comment"]);
  $gender = test_input($_POST["gender"]);
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>
#3
PHP Programing / Delete a Cookie
Last post by KingSparta - Dec 21, 2023, 03:35 PM

To delete a cookie, use the setcookie() function with an expiration date in the past:

Example
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>

<?php
echo "Cookie 'user' is deleted.";
?>

</body>
</html>
#4
PHP Programing / scandir($dir);
Last post by KingSparta - Dec 21, 2023, 01:58 PM
<?php
$dir    = '/';
$files1 = scandir($dir);
$files2 = scandir($dir, SCANDIR_SORT_DESCENDING);

print_r($files1);
print_r($files2);
?>

   
  • => $Recycle.Bin
  • [1] => $WinREAgent
        [2] => .GamingRoot
        [3] => Documents and Settings
        [4] => DumpStack.log.tmp
        [5] => HP
        [6] => Local Publish
        [7] => LocalPreview
        [8] => MyWebSite
        [9] => OneDriveTemp
        [10] => PDF's
        [11] => PHP
        [12] => Program Files
        [13] => Program Files (x86)
        [14] => ProgramData
        [15] => Recovery
        [16] => System Volume Information
        [17] => Users
        [18] => WYSIWYG Web Builder
        [19] => Windows
        [20] => Windows (C) - Shortcut.lnk
        [21] => XboxGames
        [22] => bootTel.dat
        [23] => hiberfil.sys
        [24] => lw-chat-php
        [25] => pagefile.sys
        [26] => swapfile.sys
        [27] => xampp
        [28] => xampp4
#5
PHP Programing / PHP Create/Retrieve a Cookie
Last post by KingSparta - Dec 17, 2023, 11:35 AM
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
  echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
  echo "Cookie '" . $cookie_name . "' is set!<br>";
  echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

</body>
</html>
#6
PHP Programing / Complete Upload File PHP Scrip...
Last post by KingSparta - Dec 17, 2023, 11:30 AM
Complete Upload File PHP Script
The complete "upload.php" file now looks like this:

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}

// Check if file already exists
if (file_exists($target_file)) {
  echo "Sorry, file already exists.";
  $uploadOk = 0;
}

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}
?>
#7
PHP Programing / allow only some file formats
Last post by KingSparta - Dec 17, 2023, 11:29 AM
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  $uploadOk = 0;
}
#8
PHP Programing / Check file size
Last post by KingSparta - Dec 17, 2023, 11:25 AM
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}
#9
PHP Programing / Check if file already exists
Last post by KingSparta - Dec 17, 2023, 11:24 AM
// Check if file already exists
if (file_exists($target_file)) {
  echo "Sorry, file already exists.";
  $uploadOk = 0;
}
#10
PHP Programing / Create The Upload File PHP Scr...
Last post by KingSparta - Dec 17, 2023, 11:22 AM
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}
?>