Skip to content

Instantly share code, notes, and snippets.

@mdibrahimk48
Last active April 26, 2023 09:04
Show Gist options
  • Save mdibrahimk48/281e6a65a33a728c10781be2c9226693 to your computer and use it in GitHub Desktop.
Save mdibrahimk48/281e6a65a33a728c10781be2c9226693 to your computer and use it in GitHub Desktop.
PHP script
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$image = $_FILES["image"];
// Validate name format with regular expression
if (!preg_match("/^[A-Z][a-z]+\s[A-Z][a-z]+$/", $name)) {
die("Invalid name format");
}
// Check if file is a GIF image
$allowed_types = array("image/gif");
if (!in_array($image["type"], $allowed_types)) {
die("File is not a GIF image");
}
// Rename uploaded file to "number.gif"
$directory = "uploads/";
$filename = count(glob($directory . "*.gif")) + 1 . ".gif";
$destination = $directory . $filename;
if (!move_uploaded_file($image["tmp_name"], $destination)) {
die("Failed to upload file");
}
// Append valid record to data.txt
$record = "$name $filename";
if (preg_match("/^[A-Z][a-z]+\s[A-Z][a-z]+\s\d+\.gif$/", $record)) {
file_put_contents("data.txt", $record . PHP_EOL, FILE_APPEND);
}
echo "Upload successful";
}
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="surname">Surname:</label>
<input type="text" id="surname" name="surname" required><br><br>
<label for="image">Image (GIF format only):</label>
<input type="file" id="image" name="image" accept=".gif" required><br><br>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$image = $_FILES["image"];
// Validate name format with regular expression
if (!preg_match("/^[A-Z][a-z]+\s[A-Z][a-z]+$/", $name)) {
die("Invalid name format");
}
// Check if file is a GIF image
$allowed_types = array("image/gif");
if (!in_array($image["type"], $allowed_types)) {
die("File is not a GIF image");
}
// Rename uploaded file to "number.gif"
$directory = "uploads/";
$filename = count(glob($directory . "*.gif")) + 1 . ".gif";
$destination = $directory . $filename;
if (!move_uploaded_file($image["tmp_name"], $destination)) {
die("Failed to upload file");
}
// Write name and filename to data.txt
$data_file = "data.txt";
$data_line = "$name $filename\n";
file_put_contents($data_file, $data_line, FILE_APPEND);
echo "Upload successful";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment