PHP:如何重命名使用 Zend_Form_Element_File 上传的文件?
表单:
//excerpt
$file = new Zend_Form_Element_File('file');
$file->setLabel('File to upload:')
->setRequired(true)
->addValidator('NotEmpty')
->addValidator('Count', false, 1)
->setDestination(APPLICATION_UPLOADS_DIR);
$this->addElement($file);
控制器:
//excerpt
if ($form->isValid($request->getPost()) {
$newFilename = 'foobar.txt';
//how should I rename the file?
//When should I rename the file? Before or after receiving?
try {
$form->file->receive();
echo 'filename: '. $form->file->getFileName();
}
}
问题:
当我调用
$form->file->getFileName()
时,它返回完整路径,而不仅仅是文件名。如何只输出文件名?//答案:首先,获取文件名各部分的数组: $pathparts = pathinfo($form->file->getFileName()); //然后获取你想要使用的部分 $originalFilename = $pathparts['basename'];
如何将文件名重命名为我想要的名称?这可以通过
Rename
过滤器来完成吗?我已经在表单中设置了目的地,所以我想做的就是更改文件名。也许我不应该在表单中设置目的地?或者也许这不能用过滤器来完成。也许我应该用 PHP 函数来做到这一点?我该怎么办?//答案:使用重命名过滤器: $form->file->addFilter('重命名', 'new-file-name-goes-here.txt');
最终解决方案:
这就是我最终所做的:
public function foobarAction()
{
//...etc...
if (!$form->isValid($request->getPost())) {
$this->view->form = $form;
return;
}
//the following will rename the file (I'm setting the upload dir in the form)
$originalFilename = pathinfo($form->file->getFileName());
$newFilename = 'file-' . uniqid() . '.' . $originalFilename['extension'];
$form->file->addFilter('Rename', $newFilename);
try {
$form->file->receive();
//upload complete!
$file = new Default_Model_File();
$file->setDisplayFilename($originalFilename['basename'])
->setActualFilename($newFilename)
->setMimeType($form->file->getMimeType())
->setDescription($form->description->getValue());
$file->save();
} catch (Exception $e) {
//error: file couldn't be received, or saved (one of the two)
}
}
Form:
//excerpt
$file = new Zend_Form_Element_File('file');
$file->setLabel('File to upload:')
->setRequired(true)
->addValidator('NotEmpty')
->addValidator('Count', false, 1)
->setDestination(APPLICATION_UPLOADS_DIR);
$this->addElement($file);
Controller:
//excerpt
if ($form->isValid($request->getPost()) {
$newFilename = 'foobar.txt';
//how should I rename the file?
//When should I rename the file? Before or after receiving?
try {
$form->file->receive();
echo 'filename: '. $form->file->getFileName();
}
}
Questions:
When I call
$form->file->getFileName()
it returns the full path, not just the file name. How can I output just the name of the file?//Answer: First, get an array of the parts of the filename: $pathparts = pathinfo($form->file->getFileName()); //then get the part that you want to use $originalFilename = $pathparts['basename'];
How can I rename the filename to something I want? Can this be done with the
Rename
filter? I'm already setting the destination in the form, so all I want to do is change the filename. Maybe I shouldn't be setting the destination in the form? Or maybe this can't be done with a filter. Maybe I should be doing this with a PHP function? What should I do?//Answer: Use the rename filter: $form->file->addFilter('Rename', 'new-file-name-goes-here.txt');
Final Solution:
This is what I ended up doing:
public function foobarAction()
{
//...etc...
if (!$form->isValid($request->getPost())) {
$this->view->form = $form;
return;
}
//the following will rename the file (I'm setting the upload dir in the form)
$originalFilename = pathinfo($form->file->getFileName());
$newFilename = 'file-' . uniqid() . '.' . $originalFilename['extension'];
$form->file->addFilter('Rename', $newFilename);
try {
$form->file->receive();
//upload complete!
$file = new Default_Model_File();
$file->setDisplayFilename($originalFilename['basename'])
->setActualFilename($newFilename)
->setMimeType($form->file->getMimeType())
->setDescription($form->description->getValue());
$file->save();
} catch (Exception $e) {
//error: file couldn't be received, or saved (one of the two)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用重命名过滤器< /a>.
Use the rename filter.
要回答问题 1,要从完整路径获取文件名,可以使用
basename
,或pathinfo
。例如(从文档复制粘贴):
或者:
要重命名/移动文件,我想
rename
可以解决问题,即使它是完全不是“Zend Framework 解决方案”。如果文件尚未被 ZF 移动并且仍在临时目录中,则应使用
move_uploaded_file
< /a> - 但当您使用setDestination
时,我认为该文件不再位于系统的临时目录中。To answer question 1, to get a filename from a full path, you can use
basename
, orpathinfo
.For example (copy-paste from the doc) :
Or :
To rename / move the file, I suppose
rename
would do the trick, even if it's quite not "Zend Framework solution".If the file has not been moved by ZF and is still in the temporary directory, you should use
move_uploaded_file
-- but as you are usingsetDestination
, I suppose the file is no longer in the sytem's temporary directory.各位,这是一个使用 文件上传后的重命名过滤器。还有更多选项,是的,您需要考虑现有文件,但为了让您开始,就在这里。
当文件通过下面的表单上传时,它将被重命名为“config.ini”。
Folks, here's a simple example of a form that uses the rename filter on a file after it's been uploaded. There are many more options and yes, you'd need to take in to consideration existing files, but to get you started, here you go.
When the file's uploaded through the form below, it will be renamed to 'config.ini'.
轻松修复让 Zend 在上传之前重命名
我在这里解决的问题在这里有更详细的解释:http://www.thomasweidner.com/flatpress/2009/04/17/receering-files-with-zend_form_element_file/
我遇到了麻烦在上传之前重命名文件并找到适合我的场景的解决方案。在某些时候,Zend 认为让 file 元素的 getValue() 方法为您上传文件是很聪明的做法。幸运的是,他们添加了一个选项来禁用此功能。
解决方案:如果您在文件元素上调用 getValue() 或在表单上调用 getValues(),并且您想在上传之前修改名称,则必须在 Zend_Form_Element_File 上设置 setValueDisabled(true) 。
仅供参考:我并不声称这是优化的,我只是声称它对我有用
创建表单元素(里面有魔法)
< strong>上传前重命名(在 preUpload($form) 内)
辅助方法
Easy fix to get Zend to rename before uploading
The problem I address here is explained in more detail here: http://www.thomasweidner.com/flatpress/2009/04/17/recieving-files-with-zend_form_element_file/
I was having trouble getting the file to rename before uploading and found the solution for my scenario. At some point Zend thought it clever to have the getValue() method of the file element upload the file for you. Fortunately they added an option to disable this feature.
Solution: If you are calling getValue() on the file element, or getValues() on the form, and you want to modify the name before it uploads you have to set setValueDisabled(true) on your Zend_Form_Element_File.
Fyi: I don't claim this to be optimized, I just claim it to work for me
Creating the form element (magic inside)
Rename before uploading (inside preUpload($form))
Helper methods
由于一些原因,使用 Zend 很难做到这一点。
例如,假设您有一个名为 /path/to/my/pics 的目标目录。如果两个用户同时上传一张名为“me.png”的图片,那么他们可能会互相覆盖。这是因为重命名过滤器是在文件移动到 /path/to/my/pics 后应用的。因此,在被新文件上传覆盖之前,它可能不会被重命名。
我这样做的方法是执行以下操作,
1. 扩展http传输适配器以将原始文件名传递给重命名过滤器。普通的http传输适配器传入tmp目录中的临时名称,并且没有文件扩展名。
之后,您必须将前缀添加到您正在使用的表单中,以便表单可以找到您的适配器,并且您的适配器可以找到您创建的新重命名过滤器。
我这样做的原因是因为我的目标目录将为每个用户包含一张图片,其中每张图片被命名为“user1.jpg”或“user2.png”。我想在移动文件的同时重命名该文件,这样它就不会覆盖我想要保留的目录中的任何其他文件。
这是我使用过的代码。
那是适配器,现在是过滤器。
然后,您必须将前缀路径添加到您上传文件的文件元素中。
当您将过滤器添加到文件元素时,您必须按以下方式执行
现在,当文件移动到新目录时,它们将具有原始文件扩展名,并且将具有您指定的新名称当您将过滤器添加到文件元素时。
如果这很难理解,请告诉我。我花了一段时间才弄清楚 Zend 中发生了什么来做到这一点,所以如果它对任何人有帮助,请自由使用此代码。
This is difficult to do with Zend for a few reasons.
For example, say you have a destination directory called /path/to/my/pics. If two users, at the same time, upload a picture called 'me.png' then they might override each other. This is because the rename filter is applied AFTER the file is moved to /path/to/my/pics. Thus, it might not be renamed before it is overwritten by a new file upload.
The way I did it was to do the following,
1. Extend the http transfer adapter to pass the rename filter the original file name. The normal http transfer adapter passes in the temporary name in the tmp directory and does not have the file extension.
Afterward, you'll have to add the prefix to the form you're using so that the form can find your adapter and so your adapter can find the new Rename filter you've made.
The reason I did it this way is because my destination directory was going to have one pic in it for every user where each pic was named 'user1.jpg' or 'user2.png'. I wanted to rename the file at the same time that I moved it so it wouldn't override any other files in the directory that I wanted to keep.
Here is the code I've used.
That is the adapter, now for the filter.
You'll then have to add the prefix path's to the file element that you've made to upload the file.
When you add the filter to the file element you'll have to do it the following way
Now, when the files get moved over to the new directory they'll have the original files extension and they'll have the new name that you specified when you added the filter to the file element.
If this was hard to understand please let me know. It took me a while to figure out what was going on in Zend to do this so if it helps anyone, use this code freely.
// 对于 Zend Framework:: 重命名上传的文件
// For Zend Framework :: Rename Uploaded File
您可以使用重命名过滤器。如果您想在上传过程中重命名文件名,也许它会对您有所帮助。
首先,我们需要一个函数重命名或从文件名中删除不需要的字符,例如使用这个。
之后就可以使用上面的函数进行重命名了。
那很容易。是不是?
You can use Rename filter. If you want to rename your filename during uploading maybe it helps you.
First we need a function rename or remove unwanted characters from your filename for example use this.
After that you can use the above function for renaming.
That is easy. Is not it?