Jump to content

[SOLVED] Only partial success with mySQL file upload/download


uladk

Recommended Posts

Hello everyone,

 

I found a tutorial online (http://www.php-mysql-tutorial.com/php-mysql-upload.php) for uploading files into mysql and then downloading them again but only certain files are being downloaded properly. Images and pdf's refuse to open once downloaded but I have had success with text files and partial success with word files (word had to recover it).

 

Are there any errors or missing code to process files properly?

Thanks

 

Database table:

CREATE TABLE upload (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
size INT NOT NULL,
content MEDIUMBLOB NOT NULL,
PRIMARY KEY(id)
);

 

The upload.php:

<?php
//MY CONNECTION INFO

$action = $_REQUEST['action'];

switch ($action) {

	case 'update' :

		$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);
		$content = addslashes($content);
		fclose($fp);

		if(!get_magic_quotes_gpc())
		{
			$fileName = addslashes($fileName);
		}

		if(isset($_REQUEST['upload'])) {
			$query = "INSERT INTO upload (name, size, type, content ) "."VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
			mysql_query($query) or die('Error, query failed');					

			echo "<br>File $fileName uploaded<br>";

			header("location:uploadform.php");
		}
		if(isset($_REQUEST['cancel'])) {
			header("location:uploadform.php");
		}
		break;
}
?>

<form action="?action=update" method="post" enctype="multipart/form-data" name="uploadform">
<input name="userfile" type="file" class="box" id="userfile">
<input name="upload" type="submit" class="box" id="upload" value="Upload">
</form>

 

The download.php:

<?php

//MY CONNECTION INFO

if(isset($_GET['id']))
{
	$id      = $_GET['id'];
	$query   = "SELECT name, type, size, content FROM upload WHERE id = '$id'";
	$result  = mysql_query($query) or die('Error, query failed');
	list($name, $type, $size, $content) = mysql_fetch_array($result);

	header("Content-Disposition: attachment; filename=$name");
	header("Content-length: $size");
	header("Content-type: $type");
	echo $content;
	exit;
}

?>
<html><body>
<?php

$query  = "SELECT id, name FROM upload";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
} 
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
echo "<a href='download.php?id=$id'>$name</a> <br>";
}
}
?>
</body></html>

Link to comment
Share on other sites

Figured it out! Stripslashes didn't make a difference but thank you for your help.

 

The problem was I had an echo after my database connection (to verify I was connected):

 

	$DB_LINK = @mysql_connect($db_host, $db_user, $db_pass) or die("error connecting");
echo "<h2>Connection Successful</h2>";

 

That echoed line was joining on to any file data being retrieved from the database. I noticed that after uploading/downloading a CSS file, Dreamweaver picked up the <h2>... etc. Removed the echo and all is well.

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.