In this post i'll show you how to make your own file sharing script.
Rrequirements :-
- Server with mysql and apache
- A lil bit of mind
=> let the code begin \ _(^_^)_/
- First of all we'll code mysql db
- I assume that u know how to create a sql db so lets move on to table
- From bellow mysql table query
CREATE TABLE upload (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
size INT NOT NULL,
content MEDIUMBLOB NOT NULL,
password VARCHAR(30) NOT NULL,
protected INT NOT NULL PRIMARY KEY(id),
PRIMARY KEY(id)
);
- so the above is our sql query :) which create a table named as upload with 7 coulmns.
- HERE MYSQL WORK IS OVER LETS MOVE ON TO PHP MY LOVE <3
2. PHP Work start here
- Firstly we make a config file where our database login ll be saved call it config.php
<?php
$dbhost = 'localhost';
$dbuser = 'dnuser';
$dbpass = 'urdbpass';
$dbname = 'urdbname';
?>
- Connect to db by connectdb.php
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error($conn));
mysql_select_db($dbname);
?>
- main page with upload form call it index.php
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile"> </div> <br>
Password (optional) : <input type="password" name="pas" placeholder="Password"><br>
<br>
<input name="upload" type="submit" class="btn btn-success" id="upload" value=" Upload & Share">
</form>
- Upload file call it upload.php : here on this page we are doing upload work if user enter password then protected column in db is change to "1" and in password column given password ll be inserted and then it ll upload file :)
<?
include "config.php";
include "connectdb.php";
$password = $_POST['pas']; if (preg_match('/./', $password)) {
$passw = $_POST['pas'];
$ispro = 1;
}
else {
$ispro = 0;
$passw = '';
}
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$pass = $_POST['pass'];
$idno = mysql_insert_id();
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$query = "INSERT INTO upload (name, size, type, content, password, protected ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$passw', '$ispro' )";
mysql_query($query) or die('Error, query failed');
$last = mysql_insert_id();
$result = "check.php?id={$last}";
echo "$fileName Uploaded Succesfully !!
Download link : {$result} ";
}
?>
- As you seen we made a password protected system so now we ll check for password in check.php
<?php
$id = $_GET['id'];
include "config.php";
include "connectdb.php";
$result = mysql_query("SELECT password, protected FROM upload WHERE id = '$id'");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$isprot = $row[1];
$defa = 1;
$passa = $row[0];
if ($defa == $isprot) {
// if file protected redirect to password prompt page
header("Location: passprotected.php?id={$id}");
} else {
// if file not protected start download
$query = mysql_query("SELECT name, type, size, content " .
"FROM upload WHERE id = '$id'");
list($name, $type, $size, $content) = mysql_fetch_array($query);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;
}
}
?>
- password prompt page is passprotected.php
<?php
$id = $_GET['id'];
include "config.php";
include "connectdb.php";
?><div class="container"><br><div class="box box-default"> <center> <br> <font size="4">Wait a second, The file you are trying to download is <strong>Password Protected.</strong> Please, let us verify your identity. <hr>
<form method="post" action="pd.php?id=<?php echo "$id";?>">
Enter Password : <input type="password" name="madafa" placeholder="Password here" ><br><br> <input type="submit" class="btn btn-success" value="Verify and Download">
</form><hr>
</center<hr>
</div>
</div>
- If given password is correct start download ;) call it pd.php
<?php
$passb = $_POST['madafa'];
$id = $_GET['id'];
include "config.php";
include "connectdb.php";
$result = mysql_query("SELECT password FROM upload WHERE id = '$id'");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$passa = $row[0];
if ($passa == $passb) {
$query = mysql_query("SELECT name, type, size, content " .
"FROM upload WHERE id = '$id'");
list($name, $type, $size, $content) = mysql_fetch_array($query);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;
} else {
header("Location: passprotected.php?id={$id}");
echo '<script>alert("The password you given is not correct.")</script>';
}
}
?>