Hello, I have a website which allows users to upload files to a database. I need to be able to retrieve those files and display them - allowing the users to download them.
I currently have this code in my main page:
<form enctype="multipart/form-data" action="upload_file.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
<div id="results">
<?php
$dbhandle = mysql_connect('localhost', 'root', '')
or die("Unable to connect to MySQL");
$selected = mysql_select_db("files",$dbhandle)
or die("Could not select the database");
$data = mysql_query("SELECT * FROM demo_files")
or die("Could not retrieve data.");
while($info = mysql_fetch_array($data)){
$fname = mysql_real_escape($info["demo_file"])
echo '<a href="getFile.php?fname=' . $fname .'">' . $fname. "</a><br/>\n";
}
?>
</div>
This is my form action file:
//Defining some Variables for Error Checking.
//Set File Size
$max = "1";
$max = (($max * 1024)*1024);
//Allowed File Types
$extensions = array("dem", "png", "jpg");
//Do not Edit Belo this Comment (:
//Grabbing File Extension
$file = $_FILES['uploaded']['name'];
$ext = pathinfo($file, PATHINFO_EXTENSION);
//Other Stuffs :D
$size = $_FILES['uploaded']['size'];
$processed = false;
$errors[] = false;
unset($errors[0]);
//Some Basic Error Checking
if(!isset($_FILES['uploaded']['tmp_name'])){
$errors[] = "No File Selected";
} else {
if(!in_array($ext, $extensions)){
$errors[] = "Unsupported File Type.";
} else {
if($size > $max){
$max = (($max/1024)/1024);
$errors[] = "File size is to large, max file size is {$max}Mb(s)";
} else {
$processed = true;
}
}
}
//If nothing came back as an error, continue with basic file processing and
//Any MySQL Database information.
if($processed){
$dbhandle = mysql_connect('localhost', 'root', '')
or die("Unable to connect to MySQL");
$selected = mysql_select_db("files",$dbhandle)
or die("Could not select the database");
$sql = "INSERT INTO demo_files (demo_file) VALUES ('{$file}')";
if(!mysql_query($sql, $dbhandle)) {
die('Error: ' . mysql_error());
}
header('Location: ../DeusExDemoUploader/index.php');
mysql_close();
}
//Process all and any Errors.
if($errors){
echo "<ul>";
foreach($errors as $error){
echo "<li>{$error}</li>";
}
echo "</ul>";
}
I need to be able to retrieve the BLOB data using the getFile.php script which is located within the returned anchor tags' 'href' attrbute.
I am completely lost with how to do this as I have only ever retrieved plain text data.
I am looking for a bit of advice and guidance.
Thank you in advange for any information you can give me.
Regards,
Labtec.😉