从mysql php上传下载

时间:2016-05-02 10:45:06

标签: php mysql wordpress

我已经使用了可在线获取的脚本。 File upload.php允许用户上传文件,然后将所选文件存储在MySQL数据库中。     稍后,download.php脚本显示存储在数据库中的所有文件的链接。当用户单击该链接时,应下载该文件。我已经附上了下面的脚本。     但问题是,我没有使用任何upload.php也没有下载.php。     我在wordpress中尝试了这个,使用" php代码发布(只有短代码放在页面上)"一次性。

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

$fileType = (get_magic_quotes_gpc() == 0 ? mysql_real_escape_string(
$_FILES['userfile']['type']) :
mysql_real_escape_string(stripslashes($_FILES['userfile'])));

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

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

$content = addslashes($content);

fclose($fp);

if(!get_magic_quotes_gpc())

{

$fileName = addslashes($fileName);

}

mysql_connect("localhost","*****","***");

mysql_select_db("****");

$query = "INSERT INTO wp3_cte (FileupName, size, type, content ) ".

"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

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

echo "File $fileName uploaded";

}

?>
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellspacing="1" cellpadding="1">
<tbody>
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />

<input id="userfile" type="file" name="userfile" /></td>
<td width="80"><input id="upload" type="submit" name="upload" value=" Upload " /></td>
</tr>
</tbody>
</table>
</form>
<html>
<head>
    <title>Download File From MySQL Database</title>
    <meta http-equiv="Content-Type" content="text/html; 
          charset=iso-8859-1">
</head>
<body>


<?php
mysql_connect("localhost","**********","**********");
mysql_select_db("**********");
$query = "SELECT id, FileupName FROM wp3_cte";
$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)) {
 ?>
<a href="download.php?id=<?php echo urlencode($id); ?>"
               ><?php echo urlencode($name); ?></a> <br>
            <?php
}
}
mysql_close();
?>
</body>
</html>
<?php
if (isset($_GET['id'])) {
mysql_connect("localhost","**********","**********");
mysql_select_db("**********");
$id = $_GET['id'];
$query = "SELECT FileupName, type, size, content"FROM wp3_cte WHERE id = '$id'";
           $result = mysql_query($query) or die('Error, query failed');
           list($name, $type, $size, $content) = mysql_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
ob_clean();
flush();
echo $content;
mysql_close();
exit;
  }
  ?>  


  The problem i am facing is in this peace of code:`download.php`.

 Uploaded file is showing properly if i click on download file blank page appears.

 Id is displayed in Url

 The uploaded file is saving in some folder named upload in ftp server i tried a lot but i was out with no result.
 can any one help me Thanks in advance!!!!

0 个答案:

没有答案