具有多个文件的Zend_File_Transfer不会同等上传 [英] Zend_File_Transfer w/multiple files does not upload equally
问题描述
< input name =image type =file/>
< input name =filetype =file/>
以下是处理上传的控制器中的代码块。其实还有一点,但是这是相关的一块:
$ $ p $ $ data $'image'] =(isset($ _ FILES [image])&& $ _FILES [image] [name]?$ _FILES [image] [name]:NULL);
$ data ['file'] =(isset($ _ FILES [file])&& $ _FILES [file] [name]?$ _FILES [file] [ ] : 空值);
$ options = array('ignoreNoFile'=> TRUE);
$ upload = new Zend_File_Transfer();
$ upload-> setOptions($ options)
- > addFilter('Rename',array('target'=&> RESOURCES_IMG。$ data ['image'], ('overame'=> TRUE),'image')
- > addFilter('Rename',array('target'=> RESOURCES_FILES。$ data ['file'],'overwrite'=> TRUE ),'file')
- > addValidator('ImageSize',false,array('minwidth'=> 100,
'maxwidth'=> 100,
'minheight' ='100,
'maxheight'=> 100),'image')
- > addValidator('Extension',false,'jpg','image');
if(!$ upload-> isValid())
{
echo'< h1> Oops< / h1>< p>请更正以下错误: < hr />< / p>';
foreach($ upload-> getMessages()as $ key => $ val)
{
echo'< p>< strong>'。 $键。 '< / strong>< br />'。 $ val。 < / p为H.;
}
die;
}
else
{
$ upload-> receive();
} // if(!$ upload-> isValid())
直截了当。 $数据的东西只是我抓住文件名,如果它存在或设置变量为NULL。我有我的addFilter()和addValidator()分割出来,只影响其相关的文件在这种情况下,图像或文件 - 表单字段的名称。
文件上传始终有效!然而,图像上传不是,更重要的是它将临时文件放到RESOURCES_FILES目录中,这根本没有任何意义。所以这个目录有PDF文件,除了像php8TJT13,phpXmOzQM等文件以外的任何其他文件。
我一直盯着这段代码,通过Stack Overflow搜索,出现,我找不到有这个问题的人。帮助!
好的,所以我做了更多的挖掘,结果可能是我的链接方式有问题 我使用 addFilter()
所以我决定向不同的方向移动,试图隔离每个文件并单独处理它。到目前为止,它似乎工作。这里是修改的代码:
$ p $ $ data ['image'] =(isset($ _ FILES [image])& ;& $ _FILES [image] [name]?$ _FILES [image] [name]:NULL);
所以我告诉循环跳过那个和
$ data ['file'] =(isset($ _ FILES [file])&& $ _FILES [file] [name]?$ _FILES [file] [ ] : 空值);
$ upload = new Zend_File_Transfer();
$ files = $ upload-> getFileInfo();
$ options = array('ignoreNoFile'=> TRUE);
$ upload-> setOptions($ options);
$ b foreach($ files as $ field => $ contents)
{
if(!strlen($ contents [name]))
{
继续;
$
$ b $ //上传图片的指令
if($ field =='image')
{
$ upload-> addFilter('重命名',array('target'=> RESOURCES_IMG。$ data ['image'],'overwrite'=> TRUE),'image')
- > addValidator('ImageSize',false,array ('minwidth'=> 100,
'maxwidth'=> 100,
'minheight'=> 100,
'maxheight'=> 100),'image')
- > addValidator('Extension',false,'jpg','image');
上传文件
if($ field =='file')
{
$ upload-> addFilter('重命名',数组('target'=> RESOURCES_FILES。$ data ['file'],'overwrite'=> TRUE),'file');
$ b $ if(!$ upload-> receive($ field)){
echo'< h1> Oops< / h1>< p>请更正以下错误:< hr />< / p>';
foreach($ upload-> getMessages()as $ key => $ val)
{
echo'< p>< strong>'。 $键。 '< / strong>< br />'。 $ val。 < / p为H.;
}
die;
// return;
$ foreach
code $
$ h $伪$解释
getFileInfo()
获取可用的文件数组,然后循环遍历每个文件。在我的第一个 for
循环的开头,我检查这个文件是否有名字,如果没有名字,我假设这个字段是空的, code> NULL 继续
。
一旦我在循环中,我只是使用一个简单的条件匹配我的上传指令与适当的表单字段。其余的应该是相当自明的,如果你是Zend的东西。
希望这有助于其他人在我的困境。如果你是一个Zend大师,也许你可以评论我的解决方案或修复造成这个问题的错误。可能会有更多的Zend的方法,但它现在正在工作,这感觉很好。
引用
为什么我认为这是 addFilter()
方法的链接,请参阅示例#3(下面)中的注释:
注意:即使允许多次设置相同的过滤器,在同一个过滤器中使用不同的选项时,这样做也会导致问题。
http:// framework.zend.com/manual/1.11/en/zend.file.transfer.filters.html
随机的博客文章激励我尝试隔离每一个,我打电话给它,上传指令,虽然我不知道这是所谓的:
$ b
Weird title, yes, but the problem is simple; simply aggrevating. I have a form, that I built without using Zend_Form, and it has two file uploads:
<input name="image" type="file" />
<input name="file" type="file" />
Here is the chunk of code from my controller that is handling the upload. There's actually a little more, but this is the relevant piece:
$data['image'] = (isset($_FILES["image"]) && $_FILES["image"]["name"] ? $_FILES["image"]["name"] : NULL);
$data['file'] = (isset($_FILES["file"]) && $_FILES["file"]["name"] ? $_FILES["file"]["name"] : NULL);
$options = array('ignoreNoFile' => TRUE);
$upload = new Zend_File_Transfer();
$upload->setOptions($options)
->addFilter('Rename', array('target' => RESOURCES_IMG . $data['image'], 'overwrite' => TRUE), 'image')
->addFilter('Rename', array('target' => RESOURCES_FILES . $data['file'], 'overwrite' => TRUE), 'file')
->addValidator('ImageSize', false, array('minwidth' => 100,
'maxwidth' => 100,
'minheight' => 100,
'maxheight' => 100), 'image')
->addValidator('Extension', false, 'jpg', 'image');
if (!$upload->isValid())
{
echo '<h1>Oops</h1><p>Please correct the following errors: <hr /></p>';
foreach ($upload->getMessages() as $key => $val)
{
echo '<p><strong>' . $key . '</strong><br />' . $val . '</p>';
}
die;
}
else
{
$upload->receive();
} // if (!$upload->isValid())
It's pretty straight forward. The $data stuff is just me grabbing the filename if it's there or setting the variable to NULL. I have my addFilter() and addValidator() segmented out to only affect their relevant files in this case "image" or "file" - the names of the form fields.
The "file" upload always works! However, the "image" upload doesn't and what's more it puts the temporary file into the RESOURCES_FILES directory which makes no sense at all. So that directory has PDFs and whatever else in addition to files like php8TJT13, phpXmOzQM, etc.
I have been staring at this code and searching through Stack Overflow and whatever Google will turn up and I can't find anyone having this problem. Help!
Alright, so I did more digging and it turns out there may be an issue with the way I was chaining addFilter()
so I decided to move in a different direction, trying to isolate each file and handle it's upload individually. So far it appears to be working. Here is the revised code:
$data['image'] = (isset($_FILES["image"]) && $_FILES["image"]["name"] ? $_FILES["image"]["name"] : NULL);
$data['file'] = (isset($_FILES["file"]) && $_FILES["file"]["name"] ? $_FILES["file"]["name"] : NULL);
$upload = new Zend_File_Transfer();
$files = $upload->getFileInfo();
$options = array('ignoreNoFile' => TRUE);
$upload->setOptions($options);
foreach ($files as $field => $contents)
{
if(!strlen($contents["name"]))
{
continue;
}
// upload instructions for image
if ($field == 'image')
{
$upload->addFilter('Rename', array('target' => RESOURCES_IMG . $data['image'], 'overwrite' => TRUE), 'image')
->addValidator('ImageSize', false, array('minwidth' => 100,
'maxwidth' => 100,
'minheight' => 100,
'maxheight' => 100), 'image')
->addValidator('Extension', false, 'jpg', 'image');
}
// upload instructions for file
if ($field == 'file')
{
$upload->addFilter('Rename', array('target' => RESOURCES_FILES . $data['file'], 'overwrite' => TRUE), 'file');
}
if(!$upload->receive($field)) {
echo '<h1>Oops</h1><p>Please correct the following errors: <hr /></p>';
foreach ($upload->getMessages() as $key => $val)
{
echo '<p><strong>' . $key . '</strong><br />' . $val . '</p>';
}
die;
//return;
}
} // foreach
Pseudo Explanation
I use the getFileInfo()
to grab an array of the files available to me then I loop over each. At the beginning of my first for
loop I check to see if this file has a name, if it doesn't have a name I assume that field was left blank and is NULL
so I tell the loop to skip over that and continue
.
Once I'm in the loop I'm just matching my upload directives with the appropriate form field using a simple conditional. The rest should be fairly self-explanatory if you're into Zend stuff.
Hopefully this helps someone else that was in my predicament. If you are a Zend guru maybe you can comment on my solution or fix the bug that's causing the issue. There may be a more "Zend" way of doing it, but it's working now and that feels damn good.
References
Why I thought it was the chaining of the addFilter()
method, see the note under Example #3 (below):
Note: Note that even though setting the same filter multiple times is allowed, doing so can lead to issues when using different options for the same filter.
http://framework.zend.com/manual/1.11/en/zend.file.transfer.filters.html
A random blog article that inspired me to try isolating each, I'm calling it, "upload directive" although I'm not sure if that's what it's called:
这篇关于具有多个文件的Zend_File_Transfer不会同等上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!