I would like to add basic Cloud system to my website project. I have my php login system already included. Every person must be logged in to view this site.
There are several session variables like $_SESSION["id"]
.
I've got my files.php
file - main file with list of uploaded files on server, form to upload new and delete. File data to remove/add is sent to PHP script by POST + hidden inputs keeping user id.
I've found some basic PHP functions that are successfully connected to my website and working.
uploadscript.php (w3schools)
<?php
$target_dir = "files/" . $_POST["id"] . "/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = 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 {
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
header('Location: files.php?alert=4;');
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
header('Location: files.php?alert=5;');
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
header('Location: files.php?alert=3;');
$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)) {
header('Location: files.php?alert=1;');
} else {
header('Location: files.php?alert=2;');
}
}
?>
downloadscript.php (php.net/manual/en/function.readfile.php
)
<?php
$file = "files/" . $_POST["id"] . "/" . $_POST["file"];
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
deletescript.php (php.net/manual/en/function.unlink.php
)
<?php
$file = "files/" . $_POST["id"] . "/" . $_POST["file"];
if (file_exists($file)) {
unlink($file);
header('Location: files.php?alert=6;');
exit;
}
?>
Are these scripts secure? Do you suggest any modifications to it to prevent some unexpected and unwanted actions by users?
files.php
? \$\endgroup\$