diff options
| author | Maksymilian Jopek <maks@jopek.eu> | 2023-04-12 16:26:30 +0200 |
|---|---|---|
| committer | Maksymilian Jopek <maks@jopek.eu> | 2023-04-12 16:26:30 +0200 |
| commit | d5cb8fd65d5c86c8627d3728d3aec7e1ae6ed4dd (patch) | |
| tree | c547f0145026908e39e33dcaef9c496065c1d7c6 /public/fileManagerFiles/download.php | |
| download | php-upload-d5cb8fd65d5c86c8627d3728d3aec7e1ae6ed4dd.tar.gz php-upload-d5cb8fd65d5c86c8627d3728d3aec7e1ae6ed4dd.tar.zst php-upload-d5cb8fd65d5c86c8627d3728d3aec7e1ae6ed4dd.zip | |
Initial commit with working app
Diffstat (limited to 'public/fileManagerFiles/download.php')
| -rw-r--r-- | public/fileManagerFiles/download.php | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/public/fileManagerFiles/download.php b/public/fileManagerFiles/download.php new file mode 100644 index 0000000..1b7731c --- /dev/null +++ b/public/fileManagerFiles/download.php @@ -0,0 +1,63 @@ +<?php
+session_start();
+
+if ( !$_SESSION["logged"] )
+{
+ header("Location: /index.php");
+ exit();
+}
+
+function send($id)
+{
+ $path = "../../private/downloads.txt";
+ if ( !($fd = fopen($path, "r")) )
+ {
+ $_SESSION["communicates"]["serverError"] = "<span style='color:red'>Server error! Mail admin at admin@{$_SERVER['HTTP_HOST']}</span>";
+ header("Location: ../fileManager.php");
+ return;
+ }
+ $found = false;
+ while (!feof($fd))
+ {
+ $line = trim(fgets($fd));
+ $arr = explode(";", $line);
+ if ( count($arr) == 4 )
+ {
+ if ( $id === $arr[0] )
+ {
+ $found = true;
+ $path = $arr[2];
+ $name = $arr[1];
+ break;
+ }
+ }
+ }
+
+ //var_dump($found);
+ //var_dump($path . $id);
+ //var_dump(!file_exists($path . $id));
+ if ( !$found || !file_exists($path . $id) )
+ {
+ $_SESSION["communicates"]["fileError"] = "<span style='color:red'>No file with given id!</span>";
+ return;
+ }
+ $fd = fopen($path . $id, "r");
+ $size = filesize($path . $id);
+ //$contents = fread($fd, $size);
+ fclose($fd);
+
+ header("Content-Type: application/octet-stream");
+ header("Content-Length: $size;");
+ header('Content-Disposition: attachment; filename="'.$name.'"');
+ echo file_get_contents($path . $id);
+ //echo $contents;
+}
+
+if ( !empty($_GET['fileid']) )
+ send($_GET['fileid']);
+else
+{
+ $_SESSION["communicates"]["fileError"] = "<span style='color:red'>No file id given!</span>";
+ header("Location: ../fileManager.php");
+}
+?>
|
