如何将typo3插件扩展转换为后端模块扩展?
我在运行typo3 10的后端模块时仍然遇到问题。我发现了以下扩展,它作为插件可以正常工作。https ://github.com/helhum/upload_example 现在我希望它作为后端模块运行以检查它是否可以这样工作。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据评论,这里的目标是通过后端模块上传一些照片。假设您已经有一个模块和一个操作,例如 indexAction (如果没有,以下是创建模块的方法:后端模块)这是您可以执行的操作。
HTML
此示例适用于一个文件,但您也可以使用输入上的
multiple
属性将其转换为多个文件。尽管如此,这里没什么特别的。一种简单的表单,其中包含一个用于选择文件的字段。请记住:TYPO3 使用 Bootstrap,因此您只需复制粘贴代码即可获得漂亮的表单。UploadAction
我们定义了上传发生的操作,在本例中是uploadAction。您在这里需要做的是获取表单参数并对其执行某些操作,例如验证它们是否是图像、csv 或任何其他文件类型。
这将为您提供“文件”参数内的值。
下一步是实际保存文件。我正在使用依赖注入,但为了这个答案,我将只使用 GeneralUtility::makeInstance 变体。
分解:
文件要上传到的位置
函数addUploadedFile接受两个参数。第一个是要上传的文件,第二个是复制行为。如果文件已经存在,它应该做什么。默认情况下,它会取消上传。但您可以重命名它,或替换它。所以它看起来像这样:
就是这样!
提示:
您不能总是相信来自客户端的内容,因此我首先将文件保存到临时文件夹中,检查内容 而不是文件的扩展名。任何人都可以将 php 文件重命名为 file.png 并仍然通过扩展名验证(假设您检查文件的扩展名是 png 或 jpg 等)。如果一切正常,您只需将文件复制到目标文件夹并删除临时文件夹即可。您可以在此处找到如何执行此操作:使用文件
如果你想避免/检查重复行为(如果我没记错的话,DuplicationBehaviour 只检查名称而不是内容),您可以通过两种方式完成。首先,您阅读文件的内容并生成哈希从它。然后您可以检查所有其他文件是否具有相同的哈希值。如果我没记错的话,TYPO3 实际上将文件哈希保存在数据库中,因此在您的 File 对象中,您可以验证保存的哈希是否与您要上传的哈希相同。您可以获得这样的文件:
第二种方法是将文件重命名为随机名称(可能是时间戳加上其他名称),如果您不关心是否再次上传相同的文件。所以你永远不会有重复行为。
希望有帮助
Based on the comments, the goal here is to just upload some photos via the a backend Module. Assuming that you already have a Module and an action for it, for example indexAction (if not, here is how to create a module: Backend Module) here is what you can do.
HTML
This example is for one file but you can convert it to multiple as well by using the
multiple
attribute on the input. Nevertheless, nothing special here. A simple form with one field that selects a file(s). Remember: TYPO3 uses Bootstrap, so you can just copy paste the code so you can have a nice form.UploadAction
We defined on which action will the upload take place and in this case is the uploadAction. What you need to do here, is to get the form arguments and do something with them, like validating if they are images or csv or any other file kind.
This will give you the values inside the 'file' arguments.
The next step is to actually save the file. I am using Dependency Injection but for the sake of this answer, i will just use the GeneralUtility::makeInstance variant.
Breaking it down:
where the files going to be uploaded to
The function addUploadedFile accepts two arguments. First the file to be uploaded and the second is the Duplication behaviour. What it should do if the file already exists. By default, it cancels the upload. But you can either rename it, or replace it. So it would look like this:
And that is it!
Tipps:
You can not always trust what comes from the client, so i would first save the files to a temp folder, check the CONTENT and not the extension of the file. Anyone could just rename a php file to file.png and still pass the extension validation (assuming that you check if the extension of the file is png or jpg etc). If everything checks out, you can just copy the files to the destination folder and delete the temp. You can find how to do that here: Working with files
If you want to avoid/check duplication behaviour (if i am not wrong, the DuplicationBehaviour only checks the name but not the content), you can do it with two ways. First you read the content of the file and generate a hash from it. Then you can check all the other files if they have the same hash. If i remember correctly, TYPO3 actually saves the file hash in the database, so in your File object you can validate if the saved hash is the same with the one you are about to upload. You can get the files like this:
The second way it is just to rename the file to a random name (maybe timestamp plus something else) if you do not care if the same file is uploaded again. So you ll never have a duplication behaviour.
Hope it was helpful