Last active
December 23, 2019 08:23
-
-
Save Scrumplex/3db93b1d72149dcfce88d4f4114a6cc1 to your computer and use it in GitHub Desktop.
ShareX Uploader (PHP)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$config = array( | |
'password' => "password", // A password to prevent other uploads. | |
'folders' => array( | |
'images' => "images", // folder for image/*. Change the second "images" to what you want | |
'videos' => "videos", // folder for video/*. Change the second "videos" to what you want | |
'files' => "files" // folder for */*. Change the second "files" to what you want | |
), | |
'banned_exts' => array( | |
".php", | |
".bat", | |
".exe" | |
), | |
'output_url' => "http://example.com/x", // The url, where you can access the parent directories of the defined folders. | |
'redirect_url' => "http://example.com" // The url, where direct access will be redirected to. | |
); | |
foreach ($config["folders"] as $folder) { | |
if (!file_exists($folder)) | |
mkdir($folder, 0777, true); | |
} | |
if (isset($_POST['password']) && $_POST['password'] == $config['password']) { | |
$file = $_FILES["upload"]; | |
foreach ($config["banned_exts"] as $ext) { | |
if (endsWith($file["name"], $ext)) { | |
die("File extension is not allowed!"); | |
} | |
} | |
$folder = "files"; | |
if (startsWith($file["type"], "video/")) { | |
$folder = "videos"; | |
} else if (startsWith($file["type"], "image/")) { | |
$folder = "images"; | |
} | |
$folderPath = $config['folders'][$folder]; | |
// prevent breaking out of desired folders | |
$fileName = str_replace('/', '_', $file['name']); | |
$target = __DIR__ . "/" . $folderPath . (strlen($folderPath) > 0 ? "/" : "") . $fileName; | |
$url = $config["output_url"] . "/" . $folderPath . (strlen($folderPath) > 0 ? "/" : "") . $fileName; | |
$number = 1; | |
if (file_exists($target)) { | |
$extensionPosition = strrpos($target, "."); | |
$extensionPositionUrl = strrpos($url, "."); | |
$extension = substr($target, $extensionPosition); | |
$targetWithoutExtension = substr($target, 0, $extensionPosition); | |
$urlWithoutExtension = substr($url, 0, $extensionPositionUrl); | |
$i = 1; | |
while (file_exists($target)) { | |
$target = $targetWithoutExtension . "_" . $i . $extension; | |
$url = $urlWithoutExtension . "_" . $i . $extension; | |
$i++; | |
} | |
} | |
if (move_uploaded_file($file['tmp_name'], $target)) { | |
die($url); | |
} | |
die("There was a problem while saving the file. Check permissions on target folders!"); | |
} | |
header("Location: " . $config["redirect_url"]); | |
function startsWith($haystack, $needle) | |
{ | |
return substr($haystack, 0, strlen($needle)) == $needle; | |
} | |
function endsWith($haystack, $needle) | |
{ | |
return substr($haystack, 0 - strlen($needle)) == $needle; | |
} | |
/* | |
Copyright 2017 Sefa Eyeoglu | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ |
@Joshua2504 there is an updated version of this here: https://gitlab.com/Scrumplex/ScreenshotBASH-Endpoint-PHP
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank god I found this in your gist. thank you so much!