Te dejo una guía
file table
Código SQL:
Ver originalCREATE TABLE files(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
cont BLOB,
PRIMARY KEY(id)
)
list.php
Código PHP:
Ver original<?php
$server = ["server"=>"localhost", "user"=>"root", "pass"=>"", "db"=>"test"];
$con = new mysqli($server["server"], $server["user"], $server["pass"], $server["db"]);
if (!$con->connect_errno) {
$q = $con->query("SELECT * FROM files");
if($q->num_rows > 0){
?>
<table>
<tr>
<td>Name</td>
<td>Size</td>
<td>Link</td>
</tr>
<?php
while($file = $q->fetch_assoc()){
echo "<tr>";
echo "<td>".$file["name"]."</td>";
echo "<td>".round(strlen($file["cont"])/1024, 2)." Kb</td>"; echo "<td><a href=\"download.php?id=".$file["id"]."\">Download</a></td>";
echo "</tr>";
}
?>
</table>
<?php
}
else{
echo "No files";
}
}
else{
echo "DB ERROR";
}
?>
download.php
Código PHP:
Ver original<?php
$server = ["server"=>"localhost", "user"=>"root", "pass"=>"", "db"=>"test"];
function my_is_int($i){
try{
return true;
}
catch(Exception $e){
return false;
}
}
$id = -1;
if(isset($_GET["id"]) && my_is_int
($_GET["id"])) $id = intval($_GET["id"]); if($id != null){
$con = new mysqli($server["server"], $server["user"], $server["pass"], $server["db"]);
if (!$con->connect_errno) {
$q = $con->query("SELECT * FROM files WHERE id=$id");
if($q->num_rows > 0){
$file = $q->fetch_assoc();
header("Content-Type: application/octet-stream"); header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment; filename=\"" .$file["name"]. "\""); echo $file["cont"];
}
}
}
echo 0; //send a 0 if some error...
?>
upload.php
Código PHP:
Ver original<?php
function myfread($file){
return $cnt;
}
return "";
}
$server = ["server"=>"localhost", "user"=>"root", "pass"=>"", "db"=>"test"];
$con = new mysqli($server["server"], $server["user"], $server["pass"], $server["db"]);
if (!$con->connect_errno) {
if(isset($_FILES["file"])){ $f = $_FILES["file"];
$st = $con->prepare("INSERT INTO files (name, cont) VALUES (?, ?)");
$st->bind_param("ss", $f["name"], myfread($f["tmp_name"]));
if ($st->execute())
echo "uploaded";
else
echo "some error occurred";
}
else{
?>
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="file"/><input type="submit" value="upload" />
</form>
<?php
}
}
else{
echo "DB ERROR";
}
?>