我们需要创建一个文件管理界面,该界面与在cakephp上构建的客户当前网站集成。
文件管理员只能让用户看到他们也有权限的文件。
用户将能够上传文件(任何大小)并让其他用户下载这些文件(如果权限允许)。
对于我们可以购买的许多文件管理系统来说,这不是问题,但我找不到的任何文件管理系统都会与他们当前的登录系统集成。客户端只希望用户登录一次以访问其用户CP及其文件。因此,据我所知,我们唯一的选择是自己构建一个文件管理界面。
我们允许使用PHP上传的一些选项有哪些?我知道我们需要增加php上传限制,但是php / apache会允许的上限吗?
如果相关文件可能会大约150MB,但是可能会出现文件大小较大的情况。
Linux服务器上文件上传/控制的注意事项是什么?
我想我没有真正的“具体”问题,但是我想就如何开始以及我们将遇到的一些典型陷阱提出一些建议。
答案 0 :(得分:10)
实际上,文件管理非常简单。以下是一些可能指向正确方向的建议。
首先,如果这是一个负载均衡的Web服务器情况,您需要稍微提高复杂性,以便将文件放在一个公共位置。如果是这种情况,请打电话给我,我很乐意向您发送我们用于相同情况的超轻文件服务器/客户端。
为了允许更大的上传,您可能希望影响一些变量。我建议使用apache指令将这些更改限制为特定文件:
<Directory /home/deploy/project/uploader>
php_value max_upload_size "200M"
php_value post_max_size "200M"
php_value max_input_time "1800"
# this one depends on how much processing you are doing to the file
php_value memory_limit "32M"
</Directory>
<强>架构:强>
创建一个数据库表,存储有关每个文件的一些信息。
CREATE TABLE `File` (
`File_MNID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`Owner_Field` enum('User.User_ID', 'Resource.Resource_ID') NOT NULL,
`Owner_Key` int(10) unsigned NOT NULL,
`ContentType` varchar(64) NOT NULL,
`Size` int(10) NOT NULL,
`Hash` varchar(40) NOT NULL,
`Name` varchar(128) NOT NULL,
PRIMARY KEY (`File_MNID`),
KEY `Owner` (`Owner_Field`,`Owner_Key`)
) ENGINE=InnoDB
什么是Owner_Field
和Owner_Key
?一个简单的方法来说明“实体”拥有该文件。在这种特定情况下,有多种类型的文件被上传。在您的情况下,简单的User_ID
字段可能就足够了。
存储所有者的目的是限制谁可以下载和删除文件。这对于保护下载非常重要。
以下是一个示例类,可用于接受来自浏览器的文件上传。当然,你需要修改它以适应它。
以下代码有几点需要注意。由于它与Application Server和文件服务器一起使用,因此有一些“替换”的东西。
App::CallAPI(...)
都需要替换为执行“相同内容”的查询或查询集。App::$FS->...
都需要用PHP中正确的文件处理函数替换,例如move_uploaded_file
,readfile
等...... 在这里。请记住,此处有一些功能允许您查看给定用户拥有的文件,删除文件等等。底部有更多解释......
<?php
class FileClient
{
public static $DENY = '/\.ade$|\.adp$|\.asp$|\.bas$|\.bat$|\.chm$|\.cmd$|\.com$|\.cpl$|\.crt$|\.exe$|\.hlp$|\.hta$|\.inf$|\.ins$|\.isp$|\.its$| \.js$|\.jse$|\.lnk$|\.mda$|\.mdb$|\.mde$|\.mdt,\. mdw$|\.mdz$|\.msc$|\.msi$|\.msp$|\.mst$|\.pcd$|\.pif$|\.reg$|\.scr$|\.sct$|\.shs$|\.tmp$|\.url$|\.vb$|\.vbe$|\.vbs$|vsmacros$|\.vss$|\.vst$|\.vsw$|\.ws$|\.wsc$|\.wsf$|\.wsh$/i';
public static $MAX_SIZE = 5000000;
public static function SelectList($Owner_Field, $Owner_Key)
{
$tmp = App::CallAPI
(
'File.List',
array
(
'Owner_Field' => $Owner_Field,
'Owner_Key' => $Owner_Key,
)
);
return $tmp['Result'];
}
public static function HandleUpload($Owner_Field, $Owner_Key, $FieldName)
{
$aError = array();
if(! isset($_FILES[$FieldName]))
return false;
elseif(! is_array($_FILES[$FieldName]))
return false;
elseif(! $_FILES[$FieldName]['tmp_name'])
return false;
elseif($_FILES[$FieldName]['error'])
return array('An unknown upload error has occured.');
$sPath = $_FILES[$FieldName]['tmp_name'];
$sHash = sha1_file($sPath);
$sType = $_FILES[$FieldName]['type'];
$nSize = (int) $_FILES[$FieldName]['size'];
$sName = $_FILES[$FieldName]['name'];
if(preg_match(self::$DENY, $sName))
{
$aError[] = "File type not allowed for security reasons. If this file must be attached, please add it to a .zip file first...";
}
if($nSize > self::$MAX_SIZE)
{
$aError[] = 'File too large at $nSize bytes.';
}
// Any errors? Bail out.
if($aError)
{
return $aError;
}
$File = App::CallAPI
(
'File.Insert',
array
(
'Owner_Field' => $Owner_Field,
'Owner_Key' => $Owner_Key,
'ContentType' => $sType,
'Size' => $nSize,
'Hash' => $sHash,
'Name' => $sName,
)
);
App::InitFS();
App::$FS->PutFile("File_" . $File['File_MNID'], $sPath);
return $File['File_MNID'];
}
public static function Serve($Owner_Field, $Owner_Key, $File_MNID)
{
//Also returns the name, content-type, and ledger_MNID
$File = App::CallAPI
(
'File.Select',
array
(
'Owner_Field' => $Owner_Field,
'Owner_Key' => $Owner_Key,
'File_MNID' => $File_MNID
)
);
$Name = 'File_' . $File['File_MNID'] ;
//Content Header for that given file
header('Content-disposition: attachment; filename="' . $File['Name'] . '"');
header("Content-type:'" . $File['ContentType'] . "'");
App::InitFS();
#TODO
echo App::$FS->GetString($Name);
}
public static function Delete($Owner_Field, $Owner_Key, $File_MNID)
{
$tmp = App::CallAPI
(
'File.Delete',
array
(
'Owner_Field' => $Owner_Field,
'Owner_Key' => $Owner_Key,
'File_MNID' => $File_MNID,
)
);
App::InitFS();
App::$FS->DelFile("File_" . $File_MNID);
}
public static function DeleteAll($Owner_Field, $Owner_Key)
{
foreach(self::SelectList($Owner_Field, $Owner_Key) as $aRow)
{
self::Delete($Owner_Field, $Owner_Key, $aRow['File_MNID']);
}
}
}
备注:强>
请记住,本课程不实施安全性。它假定调用者在调用FileClient::Serve(...)
之前有一个经过身份验证的Owner_Field和Owner_Key等...
有点晚了,所以如果其中一些没有意义,那就发表评论吧。祝你度过一个愉快的夜晚,我希望这会有所帮助。
PS。用户界面可以是简单的表格和文件上传字段等。或者你可以看上去并使用flash上传器......
答案 1 :(得分:1)
我建议您查看以下社区提供的CakePHP代码示例:
答案 2 :(得分:0)
这是个不错的选择。它需要您安装PC客户端和单个文件php服务器。但它运行得很快!