Jump to content

Download Code Error In PHP? Need Help


toorhamza

Recommended Posts

Hello Guys,
 
I am learning PHP and tried to create a simple php upload and download script connecting to mysql databse. I used uid() to generate a special id for each file and stored it the database. I am almost done but the problem is I could not figure out how to write the download.php code?
Please help me. I am sharing my index.php, upload.php and download.php current files.
 
My index.html
 
<html>
<body>
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>


</body>
</html>
 
My uploader.html
 
         >?php
     $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 


     if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} 
$uid = uniqid();
$filename = basename( $_FILES['uploadedfile']['name']);
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'INSERT INTO replitz_file '.
       '(file_uid,file_name) '.
       'VALUES ( "$uid", "$filename" )';


mysql_select_db('replitz');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "http://localhost/Project/download.php?$uid";
mysql_close($conn);
?> >


My download.php


>>>




>?php


$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$db = mysql_select_db('replitz', $con);
$file = $_GET['_'];
$query = "SELECT * FROM replitz_files WHERE id='$file_uid'";
$result = mysql_query($query);
$r = mysql_fetch_array($result);


$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Link Not Valid: ' . mysql_error());
echo "Link Not Valid";
mysql_close($conn);
?>`enter code here`

My download.php

 
<?php


$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$db = mysql_select_db('replitz', $con);
$file = $_GET['_'];
$query = "SELECT * FROM replitz_files WHERE id='$file_uid'";
$result = mysql_query($query);
$r = mysql_fetch_array($result);


$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Link Not Valid: ' . mysql_error());
echo "Link Not Valid";
mysql_close($conn);
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.