开发者

Uploading folders php

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 fold开发者_如何学Goers within it?


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.


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).


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


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.


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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜