| Server IP : 209.209.40.120 / Your IP : 216.73.217.112 Web Server : Microsoft-IIS/10.0 System : Windows NT NEWWWW 10.0 build 17763 (Windows Server 2019) i586 User : NEWWWW$ ( 0) PHP Version : 8.3.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : C:/HostingSpaces/admin/penpalamerica.com/wwwroot/penpalamerica/upload/ |
Upload File : |
<?php
try {
// Undefined | Multiple Files | $_FILES Corruption Attack
// If this request falls under any of them, treat it invalid.
if (
!isset($_FILES['file']['error']) ||
is_array($_FILES['file']['error'])
) {
throw new RuntimeException('Invalid parameters.');
}
// Check $_FILES['file']['error'] value.
switch ($_FILES['file']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
throw new RuntimeException('No file sent.');
echo "No file sent";
case UPLOAD_ERR_INI_SIZE:
echo "SiZE";
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('Exceeded filesize limit.');
echo "SiZ limitE";
default:
throw new RuntimeException('Unknown errors.');
echo "SiZE today ";
}
// You should also check filesize here.
if ($_FILES['file']['size'] > 100) {
throw new RuntimeException('Exceeded filesize limit.');
}
// DO NOT TRUST $_FILES['file']['mime'] VALUE !!
// Check MIME Type by yourself.
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search(
$finfo->file($_FILES['file']['tmp_name']),
array(
'jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
),
true
)) {
throw new RuntimeException('Invalid file format.');
}
// You should name it uniquely.
// DO NOT USE $_FILES['file']['name'] WITHOUT ANY VALIDATION !!
// On this example, obtain safe unique name from its binary data.
if (!move_uploaded_file(
$_FILES['file']['tmp_name'],
sprintf('./upload/%s.%s',
sha1_file($_FILES['file']['tmp_name']),
$ext
)
)) {
throw new RuntimeException('Failed to move uploaded file.');
}
echo 'File is uploaded successfully.';
} catch (RuntimeException $e) {
echo $e->getMessage();
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
header('Access-Control-Allow-Origin : *');
header('Access-Control-Allow-Methods : POST, GET, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Headers : *');
exit;
}
$filename = $_FILES["file"]["name"];
$tempname = $_FILES["file"]["tmp_name"];
$folder = "image/".$filename;
if (move_uploaded_file($tempname, $folder)) {
echo "Image uploaded successfully";
}else{
echo "Failed to upload image";
}
// $connect_to_db = mysqli_connect('localhost', 'smsEngine', 'guEnaR28T', 'smartsmsrouter');
//$user ="nosa"; //$_POST['user'];
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
/* Open a file for writing */
$fp = fopen($_FILES["file"]["name"], "w");
/* Read the data 1 KB at a time
and write to the file */
while ($data = fread($_FILES["file"], 1024))
fwrite($fp, $data);
/* Close the streams */
fclose($fp);
fclose($putdata);
//Move the file to the uploads folder
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);
//Get the File Location
$filelocation = 'http://yourdomain.com/uploads/'.$_FILES["file"]["name"];
echo $filelocation;
//Get the File Size
$size = ($_FILES["file"]["size"]/1024).' kB';
echo $size;
//Save to your Database
// mysqli_query($connect_to_db, "INSERT INTO images (user, filelocation, size) VALUES ('$user', '$filelocation', '$size')");
//Redirect to the confirmation page, and include the file location in the URL
// header('Location: confirm.php?location='.$filelocation);
}
} else {
//File type was invalid, so throw up a red flag!
echo "Invalid File Type";
}
echo "success";
?>