Skip to content

Instantly share code, notes, and snippets.

@nukadelic
Created March 19, 2020 09:22
Show Gist options
  • Save nukadelic/13d12a0c659ee9946423d1fa21ad4fea to your computer and use it in GitHub Desktop.
Save nukadelic/13d12a0c659ee9946423d1fa21ad4fea to your computer and use it in GitHub Desktop.
PHP CDN - Upload file , list all files , delete file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport'/>
<meta name="viewport" content="width=device-width"/>
<title>PHP CDN</title>
</head>
<body>
<h1> PHP CDN hosts my temp crap </h1>
<p>htaccess to download all files (<a href="">info</a>): https://stackoverflow.com/questions/14388994/forcing-a-download-using-filesmatch-in-htaccess</p>
<p>Create the file ".htaccess" inside a folder cdn in your public_html folder </p>
<pre>Header set Content-Disposition attachment</pre>
<p>Note: replace domain name</p>
<!-- ---------------------------------- -->
<!-- ---------------------------------- -->
<!-- ---------------------------------- -->
<h1>Upload files to CDN folder</h1>
<p>Dir: <?php echo __DIR__; ?></p>
<p>Dir Basename: <?php echo basename(__DIR__); ?></p>
<p>Current Working Dir: <?php echo getcwd(); ?></p>
<form class="fileUploader" enctype="multipart/form-data" action="index.php" method="POST">
<span>Upload your file</span>
<input type="file" name="uploaded_file"></input>
<input type="submit" value="Upload"></input>
</form>
<div class="fileResult">
<?php
if( ! empty($_FILES[ 'uploaded_file' ] ) )
{
$path = __DIR__ . "/cdn/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path))
{
echo "The file " . basename( $_FILES['uploaded_file']['name']) . " has been uploaded";
}
else
{
echo "There was an error uploading the file, please try again!";
}
}
if( ! empty($_POST['delete_path']) )
{
unlink( $_POST['delete_path'] );
}
?>
</div>
<!-- ---------------------------------- -->
<!-- ---------------------------------- -->
<!-- ---------------------------------- -->
<h1>List / Delete files in CDN folder</h1>
<?php
$cdn_directory = __DIR__ . '/cdn/';
echo "<pre>CDN on DISK : " . $cdn_directory . "</pre>";
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($cdn_directory));
while($it->valid())
{
if (!$it->isDot() && ".htaccess" != $it->getSubPathName() )
{
$path = $it->key();
$file = str_replace( __DIR__ , "", $path );
?><div class="cdnFile">
<a class="cdnFile" href="https://www.DOMAIN_NAME.com<?php echo $file; ?>"><?php echo $file; ?></a>
<form action="index.php" method="POST">
<input type="hidden" name="delete_path" id="delete_path" value="<?php echo $path; ?>">
<input type="submit" value="Delete"></input>
</form>
</div><?php
}
$it->next();
}
?>
<!-- ---------------------------------- -->
<!-- ---------------------------------- -->
<!-- ---------------------------------- -->
<style type="text/css">
/* Remove banner 000webhost.com banner */
body > div > a > img { display: none; }
div.cdnFile {
padding: 7px;
background: #F4F4F4;
display: block;
border-top: 1px solid #CCC;
border-left: 1px solid #ccc;
font-size: 15px;
}
div.cdnFile a.cdnFile { color: #2000ff; }
div.cdnFile input[type="submit"] { color : red; }
div.cdnFile form
{
display: inline;
float: right;
}
form.fileUploader
{
border: 1px solid;
padding: 10px;
}
form.fileUploader input[type="file"]
{
margin-right:5px;
border-right:1px solid #EEE;
padding-right: 5px;
}
div.fileResult
{
background: #F4F4F4;
}
</style>
</body>
</html>
@nukadelic
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment