简体   繁体   English

在Joomla PHP中上传图片时出现问题

[英]Issue uploading image in Joomla PHP

I am using Joomla and created Image Gallery for K2 Component. 我正在使用Joomla并为K2 Component创建了图片库。 I want to upload image into my gallery, and used for away from garbage name i used $fileName = preg_replace('/[^\\w\\._]+/', '_', $fileName); 我想将图像上传到我的画廊,并用于远离垃圾名称,我使用了$fileName = preg_replace('/[^\\w\\._]+/', '_', $fileName); by this i can upload images with "- (dash)"in them but it creates problem while uploading images with "whitespace" in them and gives error. 通过这种方式,我可以上传带有“-(破折号)”的图像,但是在上传带有“空格”的图像时会出现问题,并给出错误。

 **JFile: :copy: Cannot find or read file: $/opt/lampp/htdocs/joomla_2.5/images/folkgallery/tmp/14 .jpg**

When I remove the above code preg_match then i can upload images with whitespace in them but not able to upload images with -(dash)in them. 当我删除上面的代码preg_match时,我可以上传带有空格的图像,但不能上传带有-(破折号)的图像。 So kindly provide some way that I can upload image with anytype of image name it contains. 因此,请提供一种可以上传包含任何类型图像名称的图像的方法。

Add a \\s on your regular expfession to match the space. 在常规权限上添加一个\\ s以匹配该空间。

[^\\w\\s._] [^ \\ w \\ s._]

Edit: with more testing, i think that this expression is enough for your needs: 编辑:经过更多测试,我认为此表达式足以满足您的需求:

preg_replace('/[_|\s]+/i', '-', 'fil ename_1.jpg');

will produce 将产生

fil-ename-1.jpg

But, for Joomla, I bet it's better to use this (as it is told here: http://docs.joomla.org/Secure_coding_guidelines#File_uploads ) 但是,对于Joomla,我敢打赌最好使用它(如此处所述: http : //docs.joomla.org/Secure_coding_guidelines#File_uploads

$file = JRequest::getVar( 'Filedata', '', 'files', 'array' );

jimport('joomla.filesystem.file');
$file['name'] = JFile::makeSafe($file['name']);

if (isset( $file['name'] )) {
    $filepath = JPath::clean( $somepath.'/'.strtolower( $file['name'] ) );
    JFile::upload( $file['tmp_name'], $filepath );
}

The link is outdated (JRequest is deprecated and it is recommended to use JInput) but it solves your problem. 链接已过期(不建议使用JRequest,建议使用JInput),但是它可以解决您的问题。

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM