HOW TO CREATE YOUR PERSONAL DOWNLOAD LINK FOR FILES
For WordPress
users, uploading files like audio files are impossible so third
party sites are used to upload files and get the download link.
But most times, if those services are down, you become stranded.
How cool would it be to have your own file upload page to upload files and get a shortened URL
for it?
In this tutorial I
will share the code and teach you how to make your own file upload page to upload files and get a shortened URL
NOTE: IN THIS
TUTORIAL,
-
I ASSUME YOU HAVE A BLOG,
-
HAVE
LITTLE KNOWLEDGE OF HTML (IF NOT PHP)
-
AND ALSO
KNOW HOW TO USE YOUR HOSTING’S FILE MANAGER
WHAT YOU’RE GOING TO NEED:
-
A TEXT
EDITOR (I USED SUBLIME TEXT)
-
POSSIBLY
A BITLY.COM ACCOUNT (OPTIONAL)
-
ACCESS TO
YOUR HOSTING’S FILE MANAGER
Lets start.
This is my file tree to show the
necessary files and folders needed
You can download the source code
here http://arawolemichael.rf.gd/files/downloadpage.zip
First we start with index.php
Copy and Paste the codes
<?php
//File Upload Page
//check if form was submitted
if (isset($_POST['uploadfile']))
{
$uploadfile = $_FILES["uploadImage"]["tmp_name"];
$folderPath =
"uploadedfiles/";
$fullpath =$folderPath
. $_FILES["uploadImage"]["name"];
// Check if file already exists
if
(file_exists($_FILES["uploadImage"]["name"])) {
die("Sorry, file already exists.");
}
//check file format
$FileType =
strtolower(pathinfo($fullpath, PATHINFO_EXTENSION));
// Allow certain file formats
if ($FileType != "jpg"
&& $FileType != "png" && $FileType !=
"jpeg" && $FileType != "gif" && $FileType
!= "zip" && $FileType != "mp3" && $FileType
!= "m4a") {
echo "Sorry, only JPG, JPEG,
PNG, GIF, MP3, M4A files are allowed.";
exit();
}
if (! is_writable($folderPath) || !
is_dir($folderPath)) {
echo "error occured";
exit();
}
if
(move_uploaded_file($_FILES["uploadImage"]["tmp_name"],
$folderPath . $_FILES["uploadImage"]["name"])) {
$filename=basename($_FILES["uploadImage"]["name"]);
/* make a URL small */
function
make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1')
{
//create
the URL
$bitly
=
'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;
//get
the url
//could
also use cURL here
$response
= file_get_contents($bitly);
//parse
depending on desired format
if(strtolower($format)
== 'json')
{
$json
= @json_decode($response,true);
return
$json['results'][$url]['shortUrl'];
}
else
//xml
{
$xml
= simplexml_load_string($response);
return
'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
}
$urlto=$siteurl."/download.php?file=".$filename;
/* usage */
Please Edit the following to your
own details as it may affect your site if I close the account in the near
future
$appid='arawolemichael';
$appkey='R_2c00c6661a3d42eb80c3bd9fbd53a8e6';
$short =
make_bitly_url($urlto,$appid ,$appkey ,'json');
?>
<h1>File <?=$filename?>
Uploaded!</h1>
<h3>Link: <a
href='<?=$short?>'
target='_blank'><?=$short?></a></h3>
<a href=''>Go Back</a>
<?php
exit();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Upload
Document</title>
</head>
<body>
<center><h1>Upload
Document</h1></center>
<form action=""
method="post" enctype="multipart/form-data">
<label>Upload
File</label><br>
<input
type="file" name="uploadImage" required=""
/><br><br>
<input
type="submit" name="uploadfile"/>
</form>
</body>
</html>
Next File is the download.php file
<?php
//download.php file
if(!isset($_GET['file'])){
header("Location:
https://google.com");
exit();
}
$file=$_GET['file'];
header("Location:
$siteurl/uploadedfiles/$file");
exit();
?>
Now, make sure you make a folder
called “uploadedfiles”
And that’s all you need!
Leave a comment if you
encountered any problem or have questions!
You can download the source code
here http://arawolemichael.rf.gd/files/downloadpage.zip
Comments
Post a Comment