0

我需要通过后端的自定义组件上传一些文件。我这样做了,文件路径也被插入到数据库中。我是JFile用来上传的。我可以上传所有图片和 zip 文件,但不能上传 PDF 文件。我用来上传的代码是:

JFile::upload( $file['tmp_name'], $filepath);

我应该在哪里输入JFileJoomla 类中支持的文件类型。帮我上传PDF文件。

我的 HTML 代码是:

<tr>
   <td>
      <label for="uploaded"><?php echo JText::_( 'Upload file' ); ?>:</label>
   </td>
   <td>
      <input type="file" name="uploaded" value="" size="30" class="inputbox" /> 
   </td>
</tr>
4

2 回答 2

1

取自 Joomla 文档并进行了一些更改:

$file = JFactory::getApplication()->input->files->get('uploaded');
$filename = JFile::makeSafe($file['name']);
$filename = str_replace(' ', '_', $filename);
$fileSize = $filename['size'];

$uploadedFileNameParts = explode('.',$filename);
$uploadedFileExtension = array_pop($uploadedFileNameParts);

$validFileExts = explode(',', 'zip,pdf,jpg');
$extOk = false;

foreach($validFileExts as $key => $value){
        if( preg_match("/$value/i", $uploadedFileExtension ) ){
                $extOk = true;
        }
}

if ($extOk == false || !$validFileExts) {
    // return an arror if the file type isn't allowed
    echo JText::_( 'INVALID EXTENSION' );
    return;
}
//check file size. If over 2MB, throw an error
else if ( $fileSize > 2000000 ){
    echo JText::_( 'FILE BIGGER THAN 2MB' );
    return;
}
else {
    // your upload script
}  

希望这可以帮助

于 2013-10-16T08:39:25.933 回答
0

您是否使用 Joomla 模型表单来创建上传字段?如果是 - 尝试在字段属性中定义“接受”参数。更多信息在这里:http ://docs.joomla.org/File_form_field_type

更新的答案:尝试改用此代码

$file = JFactory::getApplication()->input->files->get('uploaded');
$filename = JFile::makeSafe($file['name']);
$filename = str_replace(' ', '_', $filename);
$fileSize = $filename['size'];

$uploadedFileNameParts = explode('.',$filename);
$uploadedFileExtension = array_pop($uploadedFileNameParts);

$validFileExts = array('zip', 'pdf', 'jpg');
$extension = JFile::getExt($file['name']);

if (!in_array($extension, $validFileExts)) {
    // return an arror if the file type isn't allowed
    echo JText::_( 'INVALID EXTENSION' );
    return;
}
//check file size. If over 2MB, throw an error
else if ( $fileSize > 2000000 ){
    echo JText::_( 'FILE BIGGER THAN 2MB' );
    return;
}
else {
    // your upload script
}
于 2013-10-16T07:03:24.537 回答