I try to generate a link in order to download a file from my database. I make this with first select a file from drop down list then it directs you to another page and in that page it creates your link but when I want to do this if I click download file it returns me the previous page ant it doesn't create a link.
Here is my download.php:
<html>
<body>
<title>Download your friend's uploads</title>
<form action="download2.php" method="post">
<?php
session_start();
$username =$_SESSION["uname"];
?>
<?php
$con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql= mysqli_query($con, "SELECT imagesnotes FROM userdata where sharedpeople='$username'");
echo "File or Image";
echo'<select name="imagesnotes">';
echo'<option value="" selected="selected">Select a File</option>';
while($row = mysqli_fetch_array($sql))
{
echo'<option value="' . $row['imagesnotes'] . '">'. $row['imagesnotes'] .'</option>';
}
echo'</select></p><p>';
mysqli_close($con);
?>
<td width="80"><input name="download" type="submit" class="box" id="download" value=" download "></td>
</form>
</body>
</html>
From there user selects a image and clicks submit button.
Here is my download2.php:
<?php
session_start();
$username =$_SESSION["uname"];
?>
<?php
$con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$imagesnotes = $_POST['imagesnotes'];
$query = "SELECT imagesnotes From userdata where imagesnotes='$imagesnotes' and sharedpeople='$username'";
$res = mysqli_query($con,$query);
$res or die('Error, query failed');
if(mysqli_num_rows($res) == 0){
echo "<br>Database is empty </br>";
}
else{
while(list($id) = mysqli_fetch_array($res)){
?>
<br><a href="download.php?id=<?php=$id;?>">download your file</a></br>
<?php
}
}
mysqli_close($con);
?>
and here,it must create a link to download the file. What am I doing it wrong? Can you help me?Thanks.
Update:
so my final version of uploaded2.php is like this:
<?php
session_start();
$username =$_SESSION["uname"];
?>
<?php
if(isset($_GET['imagesnotes']))
{
// if id is set then get the file with the id from database
$con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id = $_GET['imagesnotes'];
$query = "SELECT imagesnotes, type, size, content FROM datas WHERE imagesnotes = '$id'";
$result = mysqli_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysqli_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;
mysqli_close($con);
}
?>
<?php
$con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$imagesnotes = $_POST['imagesnotes'];
$query = "SELECT imagesnotes From userdata where imagesnotes='$imagesnotes' and sharedpeople='$username'";
$res = mysqli_query($con,$query);
$res or die('Error, query failed');
if(mysqli_num_rows($res) == 0){
echo "<br>Database is empty </br>";
}
else{
while(list($id) = mysqli_fetch_array($res)){
?>
<br><a href="download.php?id=<?php echo $id;?>">download your file</a></br>
<?php
}
}
mysqli_close($con);
?>
but it still brings me to the previous page. Why is it happening?
Update 2: So when I download the files, I download corrupted files. Here is my uploadfile.php:
<html>
<body>
<?php
session_start();
$username =$_SESSION["uname"];
?>
<title>Uploading Page</title>
<b>Hello again,<?php echo $username ?>. From here, you can select your image or file and upload our database.</b>
<form method="post" action ="uploaded.php" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</body>
</html>
And Here is my uploaded.php to confirm the update operation:
<html>
<body>
<title>Uploading Page</title>
<?php
session_start();
$username =$_SESSION["uname"];
?>
<?php
$con=mysqli_connect("localhost","root","","webpage");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
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'];
$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 datas (username,imagesnotes,size,type,content) ".
"VALUES ('$username','$fileName', '$fileSize', '$fileType', '$content')";
$res=mysqli_query($con,$query);
$res or die('Error, query failed');
echo "<br>File $fileName uploaded<br> <a href = 'default.php'>Return</a> ";
}
mysqli_close($con);
?>
</body>
</html>
What I'm doing it wrong? Thanks.
Update 3:Now I'm uploading my files to server not to database. but now I have a problem that I can't take that file for the download link. how can I do that?
here is my downloadyours.php:
<html>
<body>
<title>Download your uploads</title>
<?php
session_start();
$username =$_SESSION["uname"];
?>
<form action="downloadyours2.php" method="post">
<?php
$con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql= mysqli_query($con, "SELECT imagesnotes FROM datas where username='$username'");
echo "File or Image";
echo'<select name="imagesnotes">';
echo'<option value="" selected="selected">Select a File</option>';
while($row = mysqli_fetch_array($sql))
{
echo'<option value="' . $row['imagesnotes'] . '">'. $row['imagesnotes'] .'</option>';
}
echo'</select></p><p>';
mysqli_close($con);
?>
<td width="80"><input name="download" type="submit" class="box" id="download" value=" download "></td>
</form>
</body>
</html>
here is my downloadyours2.php:
<!DOCTYPE html>
<html>
<head>
<title>Your download link is ready</title>
</head>
<body>
<?php
// No need to query the database again, you get the filename from POST data
echo '<br /><a href="downloadedyours.php?imagesnotes=' . $_POST['imagesnotes'] . '">Download your file</a><br />';
?>
</body>
</html>
here is my downloadedyours.php:
<?php
session_start();
$username =$_SESSION["uname"];
?>
<?php
if(isset($_GET['imagesnotes']))
{
$con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$imagesnotes = $_GET['imagesnotes'];
// Set database name
$db = "webpage";
// If $_GET['imagesnotes'] is set then get the file with that filename from database
// Expecting a single result row: let's use mysqli_fetch_array(query(...), MYSQLI_ASSOC) function
$sql = mysqli_fetch_array(mysqli_query($con, "SELECT type, size FROM datas WHERE imagesnotes='$imagesnotes'"), MYSQLI_ASSOC);
header("Content-length:" . $sql['size']);
header("Content-Disposition: attachment; filename=" . $imagesnotes);
header("Content-Transfer-Encoding: Binary");
header("$target_file");
echo $sql['content'];
}
mysqli_close($con);
?>
</body>
</html>