我写了一个PHP代码,将.pdf和.docx文件上传到我在WWW目录中创建的文件夹(WAMP Server) 以下是上传文件的代码:
<?php
if ( ( ($_FILES["file"]["type"] == "application/vnd.openxmlformats- officedocument.word") || ($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/x-pdf") || ($_FILES["file"] ["type"] == "application/acrobat")
|| ($_FILES["file"]["type"] == "applications/vnd.pdf") || ($_FILES["file"]["type"] == "text/pdf") || ($_FILES["file"]["type"] == "text/x-pdf"))
&& ($_FILES["file"]["size"]<5000000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br/>";
}
else
{
if (file_exists("CVs/".$_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] ."already exists";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"CVs/" . $_FILES["file"]["name"]);
$cv=$_FILES["file"]["name"];
上述代码适用于上传.pdf文件但不上载.docx文件。我需要知道上传.docx文件的错误。
我再次编写了一个PHP代码来下载上传的.pdf文件。 我的代码如下所示:
<?php
$connect=mysql_connect("localhost","root","");
if(!$connect) die("Server failed to connect ".mysql_error());
mysql_select_db("geosas_rosterofexperts") or die(mysql_error());
$result=mysql_query("SELECT * FROM user_table");
while($row=mysql_fetch_array($result))
{
$id=$row["id"];
$fname=$row["fname"];
$mname= $row["mname"];
$lname=$row["lname"];
$gender=$row["gender"];
$address=$row["address"];
$country=$row["country"];
$mobile=$row["mobile"];
$email=$row["email"];
$altmail=$row["altmail"];
$education=$row["education"];
$field=$row["field"];
$certification=$row["award"];
$cv=$row["cv"];
echo "".$id."";
echo "<table width='100%' border='1px'>
<tr style='Background-color:#CCCCCC;'>
<td><a href='file_update.php?id=$id'>Update</a></td>
<td><a href='file_delete.php?id=$id'>Delete</a></td>
</tr>
<tr>
<td> <p style='color:#FF3300;'>First name</p> ".$fname."</td>
<td> <p style='color:#FF3300;'>Middle name</p> ".$mname."</td>
</tr>
<tr>
<td> <p style='color:#FF3300;'>Last name</p> ".$lname."</td>
<td> <p style='color:#FF3300;'>Gender</p> ".$gender."</td>
</tr>
<tr>
<td colspan=2> <p style='color:#FF3300;'>Address</p> ".$address."</td>
</tr>
<tr>
<td> <p style='color:#FF3300;'>Country</p> ".$country."</td>
<td> <p style='color:#FF3300;'>Mobile</p> ".$mobile." </td>
</tr>
<tr>
<td> <p style='color:#FF3300;'>Email</p> ".$email." </td>
<td> <p style='color:#FF3300;'>Alt.Email</p> ".$altmail."</td>
</tr>
<tr>
<td> <p style='color:#FF3300;'>Education</p> ".$education."</td>
<td> <p style='color:#FF3300;'>Field</p> ".$field."</td>
</tr>
<tr>
<td><p style='color:#FF3300;'>Specialization</p> ".$certification."</td>
<td><a href='CVs/'".$cv."'>CV</a></td>
</tr>
</table>
";
}
?>
此链接将我带到包含上传文件的文件夹,但我需要的是根据ID将我带到确切的文件,并在单击“CV”链接时下载文件。所以,请帮帮我
答案 0 :(得分:0)
1s anwer
将您的msword匹配代码替换为:
($_FILES["file"]["type"] == "application/msword")
第二个回答
要强行下载而不是导航,您可以使用下载属性。
<a href='#' download='your_cutom_file_name'>Download</a>
注意:IE和Safari不支持。
或者如果您想要更长的方式使用此代码:
<?php
$file = $_GET['file'];
download_file($file);
function download_file( $fullPath ){
if( headers_sent() )
die('Headers Sent');
// Required for some browsers
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
// File Exists?
if( file_exists($fullPath) ){
// Parse Info / Get Extension
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
// Determine Content Type
switch ($ext) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}