Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NitescuLucian/f4e0ff41e80b54b02ea50e33c7e1f5d3 to your computer and use it in GitHub Desktop.
Save NitescuLucian/f4e0ff41e80b54b02ea50e33c7e1f5d3 to your computer and use it in GitHub Desktop.
1c8e0f7579556d1d1b3c352ba9d40a29
<?php
class AnyClass {
public $data = null;
public function __construct($data) {
$this->data = $data;
}
function __destruct() {
system($this->data);
}
}
if (isset($_FILES["myFile"])){
$filepath = $_FILES['myFile']['tmp_name'];
$fileSize = filesize($filepath);
$fileinfo = finfo_open(FILEINFO_MIME_TYPE);
$filetype = finfo_file($fileinfo, $filepath);
if ($fileSize === 0) {
die("The file is empty.");
}
if ($fileSize > 3145728) { // 3 MB (1 byte * 1024 * 1024 * 3 (for 3 MB))
die("The file is too large");
}
$allowedTypes = [
'image/png' => 'png',
'image/jpeg' => 'jpg'
];
if (!in_array($filetype, array_keys($allowedTypes))) {
die("File not allowed.");
}
$filename = md5(basename($filepath)); // I'm using the original name here, but you can also change the name of the file here
$extension = $allowedTypes[$filetype];
$targetDirectory = __DIR__ . "/uploads"; // __DIR__ is the directory of the current PHP file
$newFilepath = $targetDirectory . "/" . $filename . ".png"; // Distrust everything...
if (!copy($filepath, $newFilepath)) { // Copy the file, returns false if failed
die("Can't move file.");
}
unlink($filepath); // Delete the temp file
die("File uploaded successfully to: ".$newFilepath);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment