PHP 上传大约一千张图片?
我正在开发一个网站,我需要一个客户向我发送近千张图片。我们之间有一点地理距离,因此虽然邮寄 CD 或 SD 卡并非不可能,但不是首选。我可以写一个PHP上传器让客户端提交图片吗?或者有更好的方法吗?我不想订阅任何文件共享网站。
假设我正在编写这样的上传器,有没有办法编写进度条和/或其他一些功能来防止上传中断而损坏文件?
任何有帮助的资源链接都是一个优点。
谢谢
编辑:
这是一次性的事情,这些是我们正在讨论的高质量图像。大概有几GB吧
FTP 空间是指我的 FTP 服务器的用户名和密码吗?这是可以做到的,但是(为什么)FTP 客户端比基于 Web 的上传器更好?我不确定客户是否知道如何或想要下载 FTP 程序。客户端位于 Mac 上,我不信任我所见过的少数 Mac FTP 程序。
假设我采取“一切都在一个 zip 中”的路线,PHP 可以工作吗?
再次感谢。
编辑:
好的,到目前为止,我已经确信不要使用 PHP,除非我们使用 .zip 文件。另外,我现在可能会取消进度条。 正在等待客户,这几天会回复大家。
编辑:
在某种活动中与客户会面。客户将照片保存在闪存驱动器上。问题解决了。 所有输入+1。
I am working on a websitwe and I need a client to send me close to a thousand pictures. There is a bit of a geographical distance between us, so while mailing a CD or SD card is not impossible, it is not preferred. Can I write a PHP uploader to have the client submit the pictures, or is there a better way? I do not want to subscribe to any file sharing sites.
Assuming I am writing such an uploader, is there a way to code a progresbar and/or some other features that protect against corrupted files by interrupted upload?
Any links to resources that help are a plus.
Thanks
EDIT:
This is a one time thing and these are high quality images we are discussing. It's gonna be a few GB.
By FTP Space, you mean a username and pwd to my FTP server? It can be done, but (why) is an FTP client better than a web based uploader? I am not sure that the client knows how to or wants to download an FTP program. The client is on a Mac and I don't trust the few FTP programs for Mac that I've seen.
Assuming I take the "everything in a single zip" route, will PHP work?
Thanks again.
EDIT:
Okay, so far I've been convinced not to use PHP unless we use .zip files. Also, I probably will nix the progress bar for now.
Waiting for client, will get back to everyone in the next few days.
EDIT:
Met the client at some sort of function. The client had the photos there on a flash drive. Problem solved.
+1 for all of the input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会说设置一些 FTP 空间,或者 WebDAV 共享。如果你不能这样做,那么我会上传单个(或少量)zip 文件中的图像,而不是尝试处理数百个独特的上传,这将非常耗时,因为浏览器只会选择一个文件在文件上传对话框中一次(即您不能只选择整个目录来上传)。
为什么在这种情况下不使用 PHP:
在 PHP 中编写一个简单的文件上传处理程序很容易,但如果您确实想处理非常大的上传,那就有点麻烦了,因为您需要警惕文件上传大小限制、脚本执行时间限制和内存限制。第一次设置可能会有点令人沮丧。
如果您想要处理一个多 GB 文件,那么 HTTP 上传不是您想要的方式(基于此,如果您必须问为什么,让它按您想要的方式工作可能对您来说是一个非常令人沮丧的过程)。
Mac OS 上有几个优秀的 FTP 客户端,没有理由相信 Web 浏览器是比 FTP 客户端更好的文件传输方法。
关于进度条支持:
目前 PHP 中的进度条支持非常有限(目前仅通过第 3 方模块,这有点毛茸茸的),尽管我相信有计划将该模块包含在标准发行版中。谷歌搜索 uploadprogress.so 应该会给出一些相关结果。
回复littlegreen:
我使用了这个扩展 http://pecl.php.net/package/uploadprogress (它设法与我已经安装的至少一个扩展发生冲突 - 尽管我认为他们现在已经解决了这个问题)并编写了一个自定义 JavaScript 处理程序(http://iaincollins.com/javascript/FileUploader.js)基于网络上的多个示例,力求融合每个示例的优点。例如,使用不可见的 iframe 透明地返回上传进度,创建 DHTML 文件上传对话框,并通过使其透明并将其覆盖在我自己的控件之上来替换丑陋的基于浏览器的文件上传控件(因此它仍然是可单击的,因为您尽管您可以获取已选择上传的文件的名称,但无法从 JavaScript 调用文件上传对话框)。正确设置 CSS 样式对此尤为重要 - 它是特定于浏览器的。
遗憾的是,我编写的 JS 是针对具体情况的,但它可能有一些有用的元素 - 抱歉,我没有任何可以链接到的公共内容,因为它是为私有接口创建的。
我看到现在有一个用于 uploadprogress 的 jQuery 插件 - 它并不能完成所有操作,但这将是一个好的开始: http://nixbox.com/demos/jquery-uploadprogress.php ...我记得有更好的例子,包括处理多个文件上传的例子(通过对它们进行排队并按顺序执行它们 -您仍然必须一次选择一个文件)但不幸的是我似乎找不到它们。
结论:
除非您有时间,否则我会尝试避免麻烦并执行 FTP 空间或私人/点对点文件传输应用程序之类的操作。
文件传输协议(顾名思义)是专门为大文件传输而设计的,是一个更好的选择,因为它只需要很少的配置(您不会遇到资源限制)并且您可以合理地期望客户端具有 FTP 恢复支持,假设您的服务器确实如此(即,如果下载停止,他们应该能够轻松地从中断处恢复下载,而不是像基于 PHP 文件的 HTTP 上传一样从头开始)。
虽然 HTTP 能够处理上传文件,但它并不那么强大,并且需要显式配置各种资源限制(在 Web 服务器中和 PHP 中 - 如上所述)才能上传多 GB 文件。即使使用 WebDAV(使用 HTTP 服务器远程文件系统),它也是透明的,但它往往不像 FTP 那样强大,主要是由于客户端实现有缺陷 - 特别是在 Windows 和 Mac OS 上传输非常大的文件时可能会出现问题,尽管它通常适合小文件(例如<500 MB)传输。
顺便说一句,HTML5 规范将改进文件上传的处理方式 - 可以一次上传多个文件,并且文件上传进度作为规范的一部分,但目前还不是真正的选择。
I would say set up some FTP space, or a WebDAV share. If you can't do that then I would upload the images in a single (or small number) of zip files, rather than trying to deal with hundreds of unique uploads, which would be very time consuming as browsers will only select a single file at a time in a file upload dialog (i.e. you can't just select an entire directory to upload).
Why not to use PHP in this instance:
Writing a simple file upload handler is easy in PHP but if you do want to handle very large uploads it's quite a bit tricker as you will need to be wary of file size upload limits, script excution time limits and memory limits. It can be a bit frustrating to setup the first time.
If it's a multi GB file you want to handle then an HTTP upload is not the way you want to go (based on, that if you have to ask why, getting it working as you want is probably going to be very frustrating process for you).
There are several excellent FTP clients on Mac OS, and there is no reason to trust a web browser to be better method of transferring files than an FTP Client.
Regarding progress bar support:
Currenly progress bar support is very limited in PHP (currently only via 3rd party modules which are a little hairy) though I belive there is a plan to include the module in the standard distribution. Googling for uploadprogress.so should give some relevant results.
In reply to littlegreen:
I used this extension http://pecl.php.net/package/uploadprogress (which managed to conflict with at least one extension I had installed already - although I think they have fixed that issue now) and wrote a custom javascript handler (http://iaincollins.com/javascript/FileUploader.js) based on some multiple examples on the web, in an effort to incorporate the best of each. e.g. Using an invisible iframe to return the upload progress transparently, creating a DHTML file upload dialog box and replacing the ugly browser based file upload control by making it transparent and overlaying it on top of my own control (so it was still clickable, as you can't invoke the file upload dialog from JavaScript, although you can get the name of the file that has been selected for upload). Getting the CSS styling right was particularly important for this - it's quite browser specific.
The JS I wrote is sadly very situation specific, but it might have some helpful elements - sorry I don't have anything public I can link to as it was created for a private interface.
I see there is a jQuery plugin for uploadprogress now - it doesn't do everything, but it would be a good start: http://nixbox.com/demos/jquery-uploadprogress.php ... I recall better examples being out there, including one that handled multiple file uploads (by queuing them and performing them sequentially - you still had to select one file at a time) but unfortunately I can't seem to find them.
Conclusion:
I would try and avoid the hassle and do something like FTP space or a private / peer to peer file transfer application unless you have the time.
File Transfer Protocol (as the name implies) is specifically designed for large file transfer and is a better option as it will need little configuration (you won't run into resource limits) and you can reasonably expect the client to have FTP resume support, assuming your server does (i.e. if the download stalls, they should be easily able to resume it from where they left off, rather than starting from scratch again as they would have to do with a PHP file based HTTP upload).
While HTTP has the ability to handle uploading files it's not as robust, and various resource limits (in the web server, and in PHP - as mentioned above) would need to be explicitly configured to make the uploading of a multi GB file possible. Even with WebDAV (using HTTP sever a remote file system) it's transparent but it tends not to be as robust as FTP, chiefly due to buggy client implementations - specifically there can be problems when transferring very large files on both Windows and Mac OS, although it's typically fine for small (e.g. <500 MB) file transfers.
As a side note, the HTML5 specification will improve how file uploads are handled - with the possibility to upload multiple files at once, and with file upload progress as part of the specification, but for now it's not really an option.
我建议使用 FTP,因为您可以进行批量上传、启动/暂停/停止和保留文件夹层次结构(如果需要)。适用于 Mac 的 FTP 解决方案非常棒;我使用 Transmission 和 Cyberduck。 Filezilla 也不错。
另一个 FTP 选项是让您的客户使用他的 Mac 建立本地 FTP 服务器,您可以从中下载照片。这需要一些配置,但绝对是可能的。
无论如何,对于如此数量的文件,我不建议使用基于浏览器的文件上传。如果您逐一(甚至批量)上传它们,您可能会给您的客户带来很多麻烦。 PHP 的 post 参数和上传文件大小限制将使将所有图像压缩到一个存档中变得困难,并且传输大文件可能会导致超时问题,因为浏览器通常无法处理暂停和恢复。
基于浏览器的上传的例外情况是使用基于 Flash 的文件上传器,例如 SWFUpload。有了它,您可以让您的客户端一次性选择所有文件,然后文件将排队并一一上传到您的服务器端脚本(可以是 PHP)。事实上,我使用它的目的与让客户向我发送文件的目的相同。
话虽如此,在这种情况下我仍然推荐FTP。
I would suggest FTP, with the reason being that you can do bulk uploads, start/pause/stop and preserve folder hierarchy (if necessary). The FTP solutions for Mac are great; I use Transmission and Cyberduck. Filezilla is also good.
Another FTP option would be for your client to use his Mac to set up a local FTP server, from which you can download the photos. This would require a bit of configuration, but is definitely possible.
In any case, I wouldn't recommend a browser-based file upload for this amount of files. If you upload them one-by-one (or even in batches), you're likely to create a whole lot of hassle for your client. PHP's post parameters and upload file size restrictions will make compressing all images into one archive difficult, and transferring large files can cause timeout issues as browsers aren't generally capable of handling pausing and resuming.
The exception to browser-based uploads is to use a Flash-based file uploader, such as SWFUpload. With it, you can have your client select all files in one go, after which the files will be queued and uploaded one-by-one to your server-side script (which can be in PHP). In fact, I've used this for the very same purpose of letting client send me files.
Having said that, I still recommend FTP in this instance.
不考虑文件的数量,我们谈论的是多少数据?
如果您担心文件数量,只需让客户端压缩它们即可。您可以在服务器上解压缩 zip,并将图像放在需要的位置。
另外,问问自己,这是一个定期发生的过程,还是一个不定期的维护或一次性启动任务?如果是这样,您只需通过 ftp 或其他方法交换文件即可节省您自己和您的客户的时间和金钱。
Disregarding the number of files, how much data are we talking about?
If you're concerned about the number of files, just have the client zip them. You can decompress the zip on the server and put the images wherever they need to go.
Also, ask yourself if this is a process that is going to happen regularly, or is it an irregular maintenance or one-time startup task? If so, you'll save yourself and your client time and money by just exchanging the files via ftp or some other method.
我只需为客户解决类似的问题。他们定期让客户向他们上传一堆文件,总计 300-400mb,通常通过 FTP。然而,其中一个客户端有防火墙,无法使用 FTP。所以我必须编写一个 HTTP 上传器。
我在互联网上搜索了 PHP 解决方案,发现与 Ian Collins 所说的基本相同:1)PHP 是可能的,但很麻烦;2)对 PHP 的进度条支持很糟糕。不过,我可以为您提供更多关于上传/大小限制的详细信息,只要您在 Web 目录中包含一个包含以下代码的 .htaccess 文件,该文件应该为您提供 3GB 的上传限制和无限制的脚本执行只要您的 Web 主机支持在 htaccess 文件中设置 PHP conf 值即可:
编写上传器脚本相对简单。 此处提供了一个很好的教程。
使用进度条比较棘手。如果不安装扩展,则无法实现仅 PHP 的进度条。普遍接受的选项是使用混合 Perl/PHP 解决方案,但这需要您能够在您的服务器上运行 CGI 脚本。我不能,所以我省略了进度条。不过,这对客户端非常不友好,因为他盯着不动的屏幕很长一段时间,认为浏览器已经锁定,如果他以某种方式关闭浏览器或失去互联网连接,他将不得不重新开始。
一次上传多个文件也很棘手。通过使用高级文件选择控件和 AJAX 打开多个并发上传会话,您可能会取得很大的进展,但您会遇到同样的问题:当由于某些奇怪的文件名、磁盘已满或连接丢失而失败时,客户端将必须重新开始,但服务器上已经有一堆文件。编写用户友好的简历解决方案并不是一件容易的事。我建议你让客户压缩他的文件并批量上传。
由于这是一次性的事情,我建议您不要费心编写自己的解决方案并使用已有的工具。为什么不让您的客户端使用 FTP,例如 Mac 版本的 FileZilla?或者DropBox?或者让他将文件上传到 Google 文档< /a>,最近允许上传和共享最大 100mb 的任何文件?
I just had to solve a similar problem for a client. They regularly have THEIR clients upload a bunch of files to them, totalling 300-400mb, normally through FTP. However, one client had a firewall and could not use FTP. And so I had to code a HTTP uploader.
I searched around the Internet for a PHP solution and found basically the same things as Ian Collins already said, 1) PHP is possible but hairy and 2) Progress bar support for PHP is bad. I can give you a bit more details though, about upload/size limits you should be fine as long as you include a .htaccess file in your web directory containing the following code, which should give you an upload limit of 3GB and unlimited script execution time as long as your webhost supports setting PHP conf values in htaccess files:
Coding an uploader script is relatively simple. A good tutorial is found here.
Using a progress bar is more tricky. A PHP-only progress bar is not possible without installing extensions. A generally accepted option is to use a hybrid Perl/PHP solution, which however requires you to be able to run CGI scripts on your server. I can't, so I've left the progress bar out. This is highly client unfriendly though, because he's staring at a non-moving screen for quite some time, thinking the browser has locked up, and if he somehow closes it or loses his Internet connection, he'll have to start over again.
Uploading several files at once is also tricky. By using an advanced file selection control and AJAX to open several concurrent uploading session you could come a long way, but you would be left with the same problem: when it fails because of some weird filename, disk full or connection lost, the client will have to start over again but there will be a mess of files already on the server. Coding a userfriendly resume solution is not an easy task. I'd advise you to let the client ZIP his files and upload them in a bulk.
Since this is a one-time thing, I would advise you not to bother with writing your own solution and use tools already available. Why don't you let your client use FTP, for instance the MAC version of FileZilla? Or DropBox? Or how about letting him upload the files to Google Docs, which since recently allows uploading and sharing any file up to 100mb?
您可以考虑 SWFUpload (http://code.google.com/p/swfupload/),它是一个 Flash 和 Javascript 库,可以使上传变得更加容易。它具有开箱即用的 PHP 和 .Net 支持,因此它可能会很好地满足您的需求。
我在一次上传会话中使用它处理了多达 1,000 张图像。
You might consider SWFUpload (http://code.google.com/p/swfupload/), which is a library of Flash and Javascript to make uploading a lot easier. It has PHP and .Net support out of the box, so it would likely fit your needs well.
I've used it for up to 1,000 images in a single upload session.