SimpleFS now creates ZIP archives for multi-file uploads

This commit is contained in:
Andrew S. Rightenburg 2023-04-26 22:43:27 -04:00
parent 73f928232c
commit b1082726f8
Signed by: rail5
GPG key ID: A0CB570AB6629159
4 changed files with 52 additions and 6 deletions

View file

@ -6,4 +6,4 @@ CHANGELOG
2023-04-26:
* Updated the upload page to display the file's download link immediately after uploading
* SimpleFS now automatically creates a ZIP file when a user uploads multiple files (requires php-zip module)

View file

@ -5,6 +5,7 @@ Simple, Self-Hosted, PHP File Sharing
- Portable
- Dual- or single-user set up *(users with permission to upload)*
- Option to automatically delete files after a certain length of time
- Automatically ZIPs files if you upload more than one at a time
- SQLite (No need for a bulky SQL Server)
### Installation
@ -14,8 +15,9 @@ Simple, Self-Hosted, PHP File Sharing
*That's it*
# Requirements
* [PHP 7.2+](https://www.php.net)
* [PHP 7.4+](https://www.php.net)
* [SQLite Module for PHP](https://www.php.net/manual/en/sqlite3.installation.php)
* [ZIP Module for PHP](https://www.php.net/manual/en/zip.installation.php)
- Please ensure that your **php.ini** *permits uploads.*
Check for the line:

View file

@ -17,7 +17,13 @@ if (!extension_loaded("pdo_sqlite")) {
echo '<div align="center"><h1><font color="FF0000">Warning: You do not have the PHP SQLite extension installed.</font></h1><h3>Please install the PHP sqlite3 extension before moving forward</h3></div><br><br>';
}
if (!extension_loaded("zip")) {
echo '<div align="center"><h1><font color="FF0000">Warning: You do not have the PHP ZIP extension installed.</font></h1><h3>Please install the PHP-zip extension before moving forward</h3></div><br><br>';
}
echo '<div align="center"><b><u>php.ini</u></b> specifies your server\'s <i>maximum upload filesize</i> as:<b> '.ini_get('upload_max_filesize').'</b></div><br>';
echo '<div align="center"><b><u>php.ini</u></b> specifies that your server can permit uploads of up to <b> '.ini_get('max_file_uploads').' files</b> in one request</div><br>';
?>
<div align="center">
<h1>IMPORTANT:</h1>

View file

@ -17,7 +17,14 @@ echo deliverTop("SimpleFS - Upload");
if ($_POST['fsubmitted'] == "true") {
$target_dir = "files/";
$target_file = $target_dir . basename($_FILES["upfile"]["name"]);
// If the user is uploading multiple files, we'll ZIP them
if (count($_FILES["upfile"]["name"]) > 1) {
$target_file = $target_dir . "SimpleFS_User$currentUser " . date('Y-m-d H_i_s') . ".zip";
} else {
$target_file = $target_dir . basename($_FILES["upfile"]["name"][0]);
}
$uploadOk = true;
$fileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
@ -56,7 +63,22 @@ if ($_POST['fsubmitted'] == "true") {
if ($uploadOk == false) {
echo "<div align='center'><h1>Error: file was not uploaded</h1></div>";
} else {
if (move_uploaded_file($_FILES["upfile"]["tmp_name"], $target_file)) {
// If the user is uploading multiple files, we'll ZIP them
if (count($_FILES["upfile"]["name"]) > 1) {
$zip_archive = new ZipArchive;
$zip_archive->open($target_file, ZipArchive::CREATE);
foreach ($_FILES["upfile"]["tmp_name"] as $key=>$tmp_file_name) {
$zip_archive->addFile($tmp_file_name, "/".$_FILES["upfile"]["name"][$key]);
}
$file_upload_complete = $zip_archive->close();
} else {
$file_upload_complete = move_uploaded_file($_FILES["upfile"]["tmp_name"][0], $target_file);
}
if ($file_upload_complete) {
$newFileId = rand(10000, 99999);
while (in_array($newFileId, $fileListId)) {
@ -73,7 +95,7 @@ if ($_POST['fsubmitted'] == "true") {
/* Tell the user all is well */
echo "<div align='center'><h1>The file ". htmlspecialchars( basename( $_FILES["upfile"]["name"])). " has been uploaded.</h1></div>";
echo "<div align='center'><h1>Uploaded!</h1></div>";
/* Provide the download link */
@ -103,7 +125,23 @@ window.onload = giveLink;
}
echo deliverMiddle("Upload", '<form action="upload.php" method="post" enctype="multipart/form-data"><input type="hidden" name="fsubmitted" id="fsubmitted" value="true"><input type="file" name="upfile" id="upfile">', '<button><i class="fa fa-upload">Upload</i></button></form>');
echo '<script type="text/javascript">
function checkLimit(files) {
if (files.length > '.ini_get('max_file_uploads').') {
alert("You may only upload up to '.ini_get('max_file_uploads').' files at once on this server\nFor administrators: this setting can be changed in your php.ini");
let list = new DataTransfer;
for (let i=0; i<'.ini_get('max_file_uploads').'; i++) {
list.items.add(files[i]);
}
document.getElementById("upfile").files = list.files;
}
}
</script>';
echo deliverMiddle("Upload", '<form action="upload.php" method="post" enctype="multipart/form-data"><input type="hidden" name="fsubmitted" id="fsubmitted" value="true"><input type="file" name="upfile[]" id="upfile" onChange="checkLimit(this.files)" multiple>', '<button><i class="fa fa-upload">Upload</i></button></form>');
echo deliverBottom();