在PHP和mysql中打开下载的文件不起作用

时间:2017-04-02 06:39:46

标签: php mysql

您好我正在尝试制作一个上传然后下载该文件的应用程序,但我在打开除txt文件之外的文件时遇到问题。我收到一条错误消息,表示"未能打开"。

请帮我解决这个问题所以我提供了我的代码。

谢谢。

所以这是我的代码:

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

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

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

$content = addslashes($content);

fclose($fp);

if(!get_magic_quotes_gpc())

{

$fileName = addslashes($fileName);

}

include_once 'dbConnect.php';

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

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

mysqli_query($conn,$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>

upload.php的

<?php

include_once 'dbConnect.php';

$sql = "Select * from gravator";

$res = mysqli_query($conn,$sql) or die('Error, query failed');

if(isset($_GET['id'])) { // if id is set then get the file with the id from   
database

$id = $_GET['id'];

$query = "SELECT name, type, size, content FROM gravator WHERE id = $id";

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

list($name, $type, $size, $content) =

mysqli_fetch_array($result);

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

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

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

echo $content; exit;

}

?>
<?php

$query = "SELECT id, name FROM gravator";

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

if(mysqli_num_rows($result) == 0)

{

echo "Database is empty";

}

else

{

while(list($id, $name) = mysqli_fetch_array($result))

{

?>

<a href="download.php?id=<?php echo $id;?>"><?php echo $name; ?></a>

<?php

}

}

?>

0 个答案:

没有答案