joomla/filesystem

Joomla 文件系统包

3.0.2 2024-09-12 11:44 UTC

README

Latest Stable Version Total Downloads Latest Unstable Version License

文件上传示例

use Joomla\Filesystem\File;

$file = $this->input->files->get('file');

$config = array(
    'extensions'    => 'jpg,jpeg,gif,png,pdf,doc,docx',
    'max_size'      => 30000000, // 30 MB
    'folder'        => 'documents'
);

// Check there is some file to upload
if (empty($file['name']))
{
    return;
}

// Check max size
if ($file['size'] > $config['max_size'])
{
    throw new \RuntimeException('Uploaded file size (' . round($file['size'] / 1000) . ' kB) is greater than allowed size (' . round($config['max_size'] / 1000) . ' kB).');
}

$config['extensions'] = explode(',', $config['extensions']);

// Get File extension
$ext = strtolower(substr($file['name'], (strrpos($file['name'], '.') + 1)));

// Sanitize allowed extensions
foreach ($config['extensions'] as &$extension)
{
    $extension = str_replace('.', '', trim(strtolower($extension)));
}

// Check allowed extensions
if (!in_array($ext, $config['extensions']))
{
    throw new \RuntimeException('Uploaded file extension (' . $ext . ') is not within allowed extensions (' . implode(',', $config['extensions']) . ')');
}

$path = JPATH_ROOT . '/' . $config['folder'] . '/' . File::makeSafe($file['name']);

File::upload($file['tmp_name'], $path);

从 1.x 的变更

补丁程序

在 1.x 中,addaddFile 方法的第二个参数是可选的。在 2.0 中,此参数是必需的。此参数需要您正在修补的源根路径。

通过 Composer 安装

"joomla/filesystem": "~3.0" 添加到您的 composer.json 中的 require 块,然后运行 composer install

{
	"require": {
		"joomla/filesystem": "~3.0"
	}
}

或者,您可以直接从命令行运行以下命令

composer require joomla/filesystem "~3.0"

如果您想包含测试源,使用

composer require --prefer-source joomla/filesystem "~3.0"