我想使用php上传Microsoft word和power point文档。下面的代码适用于图像,但我希望它与word和power-point文档一起使用。我怎么改变它?是否也可以在网页上显示文档,以便其他用户可以下载它们(创建下载链接)。
<?php
$file = $_FILES['file_upload'];
$name = $file['name'];
$type = $file['type'];
$tmp_location = $file['tmp_name'];
$upload = 'uploads';
$final_destination = $upload.'/'.$name;
$error = $file['error'];
$max_upload_size = 2097152;
$size = $file['size'];
$allowedImageTypes = array('image/png', 'image/jpeg', 'image/gif', );
function imageTypeAllowed($imageType){
global $allowedImageTypes;
if(in_array($imageType, $allowedImageTypes))
{
return true;
}
else
{
return false;
}
}
//Check for errors
if($error > 0 || is_array($error)){
die("Sorry an error occured");
}
//Check if file is image
//Only required if image is only whjat we need
if(!getimagesize($tmp_location)){
die("Sorry, you can only upload image types");
}
if(!imageTypeAllowed($type)){
die("Sorry, file type is not allowed");
}
if(file_exists($final_destination)){
$final_destination = $upload.'/'.time().$name;
}
if(!move_uploaded_file($tmp_location, $final_destination)){
die("Cannot finish upload, something went wrong");
}
?>
<h2>File Successfully uploaded!</h2>
答案 0 :(得分:1)
您需要在$allowedImageTypes
数组中添加Powerpoint和Word的MIME类型,如下所示:
$allowedImageTypes = array(
'image/png', 'image/jpeg', 'image/gif',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ,
'application/vnd.openxmlformats-officedocument.presentationml.presentation'
);
有关Office文档的MIME类型的完整列表,请参阅this link。
然后,您需要删除以下验证:
// You should remove this block of code....
if(!getimagesize($tmp_location)) {
die("Sorry, you can only upload image types");
}
答案 1 :(得分:0)
关于你的第二个问题:
&#34;是否也可以在网页上显示文档,以便其他用户可以下载它们(创建下载链接)&#34;
(自Shankar already answered第一部分):
您可以使用glob()
函数:
旁注:如果您希望显示首选文件夹中的所有文件,请将*.doc
替换为*.*
。
$directory = "path/to/files/";
$files = glob($directory . "*.doc");
foreach($files as $file)
{
echo '<a href="'.basename($file).'">'.$file.'</a>';
}
您可以将'.$file.'
替换为&#34;下载&#34;将该单词显示为链接。
要限制某些文件扩展名,请使用:
$files = glob($directory . '*.{jpg,jpeg,png,gif,doc,ppt}', GLOB_BRACE);
类似于:
$directory = "path/to/folder/"; // keep the trailing slash, important
// $files = glob($directory . "*.jpg"); // limit .jpg files only
// show files of a certain extension only
$files = glob($directory . '*.{jpg,jpeg,png,gif,doc,ppt}', GLOB_BRACE);
foreach($files as $file)
{
echo '<a href="'.basename($file).'">'.$file.'</a>' . "<br>";
// OPTIONAL methods - Uncomment the one you wish to use
// or with the words View/Download file with path echo'ed
// echo '<a href="'.basename($file).'">View/Download file '.$file.'</a>' . "<br>";
// or with the words View/Download file with no echo'ed path
// echo '<a href="'.basename($file).'">View/Download file</a>' . "<br>";
}
答案 2 :(得分:0)
由于文件名已在数据库中,因此为该文件列创建的链接也将存在(如果不是,请手动添加)。
然后,在$file
下面添加以下代码,以便文件名中的空格替换为下划线(例如,stack over flow screenshot.jpg
将转换为stack_over_flow_screenshot.jpg
):
$fileNameNew = preg_replace('/\s+/', '_', $name);
现在,您可以添加以下内容为每个成员创建一个页面,他们可以在这里下载文件:
<?php
/* Connect to database here */
$objTotalRS = mysql_query("select * from tablename");
while ($data = mysql_fetch_array($objTotalRS))
{
?>
<table>
<tr>
<td>File Name :</td>
<td><?= stripslashes($data['name']) ?></td>
<td><a href="uploads/<?= $data['filenamenew'] ?>">Download Now</a></td>
</tr>
</table>
<?
}
?>
同时更改以下行:
$final_destination = $upload.'/'.$name;
为:
$final_destination = $upload.'/'.$fileNameNew;