Jump to content

Recommended Posts

I have three files which i have posted below

 

File 1 upload.php which uploads the file to the database.

File 2 downlaod.php just give you the list of file which are available to download

File 3 download2.php Is the file which download the file selected.

 

My problem is once I have downloaded the file back from the database it is saying for example when trying to open a word document "The file x.docx connot be opened because there are problems with the content"

Also when downloading images from the database they will not open ethier.

 

Is it I am doing wrong

 

 

 

 

/*********************************************************************************************************************/

//

//

// Upload script (upload.php)

//

//

//

/*********************************************************************************************************************/

 

 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR...l1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<?php

//*******************************************************

//

// Open Connection to Database

//

//*******************************************************

 

include ("connection.php");

?>

 

<form method="post" 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="4000000">

<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>

 

 

<?php

 

 

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'];

 

$tfileName = trim($fileName);

$ttmpName = trim($tmpName);

$tfileSize = trim($fileSize);

$tfileType = trim($fileType);

 

 

$fp = fopen($ttmpName, 'r');

$content = fread($fp, filesize($ttmpName));

$content = addslashes($content);

fclose($fp);

 

$tcontent = trim($content);

 

if(!get_magic_quotes_gpc())

{

$fileName = addslashes($fileName);

}

 

$query = "INSERT INTO upload (name, size, type, content ) ".

"VALUES ('$tfileName', '$tfileSize', '$tfileType', '$tcontent')";

 

mysql_query($query) or die('Error, query failed');

 

 

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

}

?>

 

 

 

 

 

/*********************************************************************************************************************/

//

//

// Download script which give you a list of files which you can download (downloadfile.php)

//

//

//

/*********************************************************************************************************************/

 

<html>

<head>

<title>Download File From MySQL</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<body>

<?php

include ("connection.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))

while ($list = mysql_fetch_array($result))

{

$id = $list["id"];

$name = $list["name"];

echo "ID = $id ,Name = $name";

echo "<a href='downloadfile2.php?list=$id'>$name</a> <br>";

}

}

?>

</body>

</html>

 

 

 

 

/*********************************************************************************************************************/

//

//

// Download script which downloads the file (downloadfile2.php)

//

//

//

/*********************************************************************************************************************/

 

 

<?php

 

include ("connection.php");

 

$list = (isset($_GET['list'])) ? $_GET['list'] : '';

 

echo "$list";

 

//if(isset($_GET['id']))

if(isset($list))

 

{

// if id is set then get the file with the id from database

 

 

$id = $list;

 

 

$query = "SELECT `name`,`type`,`size`,`content` FROM upload WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');

 

$list2 = mysql_fetch_array($result);

 

$name = $list2["name"];

$type= $list2["type"];

$size= $list2["size"];

$content= $list2["content"];

 

$tcontent = trim($content);

 

header("Content-Transfer-Encoding: binary");

header("Content-length: $size");

header("Content-type: $type");

header("Content-Disposition: attachment; filename=$name");

echo "$tcontent";

 

exit;

}

 

?>

Link to comment
https://forums.phpfreaks.com/topic/270782-downloading-files-from-a-db-help/
Share on other sites

...

Is it I am doing wrong

...

Yip. Once you try and open the file, and then read the content into the database, anything that the original program attached to the file to identify it ias openable by that application will be lost. You shouldn't really be storing the contents of the files in this way anyway. What you want to do is store the information about the file in the database, and store the actual file in on the filesystem(it's what it's designed for after all) of the server.

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.