toorhamza Posted July 26, 2013 Share Posted July 26, 2013 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/280552-download-code-error-in-php-need-help/ Share on other sites More sharing options...
fastsol Posted July 27, 2013 Share Posted July 27, 2013 I have a tutorial on file download, you'll just need to edit how you get the file name in the script to use your database, beyond that it's pretty easy. http://amecms.com/article/PHP-Force-File-Download-With-File-Whitelist Quote Link to comment https://forums.phpfreaks.com/topic/280552-download-code-error-in-php-need-help/#findComment-1442354 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.