上传文件夹php

发布于 2024-11-19 15:02:53 字数 234 浏览 1 评论 0 原文

假设我有一个文件夹,里面装满了大约五十张我刚刚拍摄的照片,我想将它们全部上传到我的网站。因此,我不必选择每一个并上传它,我可以只上传它们所在的文件夹。然而,由于浏览器不允许您选择文件夹,我知道我可以这样做,以便您复制并粘贴该文件夹的路径,但这将是丑陋的、黑客的和复杂的。有没有更好的方法来避免上传文件夹而不必这样做?如果我确实必须这样做,那么 copy() 是否可以在文件夹上工作,或者我是否必须使用递归循环来从该文件夹及其中的任何文件夹中获取所有图片?

Lets say I have a folder full of about fifty pictures I'd just taken on a photo shoot and I wanted to upload all of them to my website. So instead of having to select every single one and upload it, I could just upload the folder that they're in. Yet being that the browsers won't let you select folders, I know I could make it so you copy and paste the path to that folder, yet that would be ugly,hackish and complicated. Is there a better way to get around uploading a folder instead of having to do that? And if I do have to do that, will copy() work on folders or will I have to use a recursive loop to get all the pictures out of that folder and any folders within it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

人生戏 2024-11-26 15:02:53

您应该将 multiple 属性添加到 input:file 元素。这样您就可以通过单个 input:file 字段选择任意数量的文件。

you should add the multiple attribute to the input:file element. That way you can select as many files as you want via a single input:file field.

我的影子我的梦 2024-11-26 15:02:53

PHP 本身无法处理这样的任务,因为它必须依赖 HTML 界面与客户端进行通信。如果您使用的是 HTML5,或者您的客户端使用具有适当支持的浏览器,则可以使用文件 input 元素的 multiple 属性来处理此任务。

对于 HTML5 之前的 HTML 版本,您需要添加一个可以处理多个文件上传的工具,例如 SWFUpload( Flash)、jUpload(Java 小程序)或 上传 (jQuery)。

PHP itself is unable to handle such a task because it must rely on the HTML interface to communicate with the client. If you are using HTML5, or if your clients use browsers with the appropriate support, you can use the multiple attribute for the file input element to handle this task.

For versions of HTML prior to HTML5, You will need to add a tool that can handle multiple file uploads, such as SWFUpload(Flash), jUpload(Java applet), or Uploadify (jQuery).

冰雪之触 2024-11-26 15:02:53

简单地说:仅使用 html 表单是不可能的。只需将它们压缩并在服务器端解压缩即可。

Simply: It isn't possible just with html form. Just zip them and unzip them on server side.

看轻我的陪伴 2024-11-26 15:02:53

在 HTML5 浏览器占领世界之前,或者您使用基于 Flash 或 Java 的客户端解决方案之前,您上传多个文件的唯一方法是提供 对于您要上传的每个文件。

允许从浏览器上传文件是一个主要的安全风险,因此文件输入是浏览器中监管/监管最严格的元素之一 - 允许任何类型的可编程性为能够吸收任何文件进行攻击的漏洞利用打开了大门想要从客户端机器。

Until HTML5 browsers take over the world, or you use client-side solutions based on Flash or Java, the ONLY way you'll be able to upload multiple files is by providing a <input type="file" ...> for each file you want uploaded.

Allowing file uploads from browsers is a major security risk, so the file inputs are one of the most heavily policed/regulated elements in browsers - allowing any kind of programmability opens the door to an exploit being able to suck any file(s) the attack wants from the client machine.

沦落红尘 2024-11-26 15:02:53

我为我妻子的网站 http://www.beccablake.com 执行此操作。她也是一名摄影师。我创建了一个自定义模块(她使用 drupal 作为 CMS)来上传包含所有图片的 zip 文件。上传后,我提取 zip 并迭代所有文件,并为每个文件创建新节点。这是该模块中的一段代码,可以帮助您完成您想要的操作。

<?php
$file = 'path/to/zip/pictures.zip';
$dest = 'path/to/pictures/directory';
exec('unzip "'. $file .'" -d '. $dest);
// If you need to do something with each file (like I do)
$files = scandir($dest);
?>

这假设您可以在服务器上执行“exec”来解压缩。我的代码有很大不同,因为我在 Drupal 中对图片做了更多处理,我创建了一个校对图库,每张图片都有自己的节点,以便用户可以评论/评分/收藏照片...等等。

祝你好运!

I do this for my wifes website http://www.beccablake.com. She is also a photographer. I created a custom module (she uses drupal as the CMS) to upload a zip file containing all the pictures. Once uploaded I extract the zip and iterate through all the files and create new nodes for each one. Here is a snippet of code from that module that may help you do what you want.

<?php
$file = 'path/to/zip/pictures.zip';
$dest = 'path/to/pictures/directory';
exec('unzip "'. $file .'" -d '. $dest);
// If you need to do something with each file (like I do)
$files = scandir($dest);
?>

This assumes you can perform an 'exec' on your server to unzip. My code is a lot different as I do more with the pics since its in Drupal, I create a proofing gallery and each picture is its own node so users can comment/rate/favorite the photos... etc.

Good Luck!

空气里的味道 2024-11-26 15:02:53

浏览器永远不会传输有关客户端目录结构的任何信息,因此您想象的这种通用解决方案在目录方面是不可能的。您可以希望客户端将提供一种功能,将用户的目录选择转换为包含文件的列表,但无论如何,这在服务器端都不会引起您的关注。

The browser will never transmit any information about the client's directory structure, so such a general solution as you envision it is not possible in terms of directories. What you can hope for is that clients will offer a facility to turn a user's directory selection into a list of contained files, but in any event this would not be of any concern to you on the server side.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文