By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,666 Members | 1,784 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,666 IT Pros & Developers. It's quick & easy.

[FAQ] File uploading

P: n/a
Q: How should I handle file upload?
A: File uploading requires HTML form of content type
"multipart/form-data". The file content has to be POSTed/submitted via
the form and once the file is uploaded, it will be available at the
"upload_tmp_dir" (usually /tmp) directory. Then you may move that file
to another directory using move_uploaded_file(); file name will be
available in the superglobal $_FILES['userfile']['tmp_name'].

Refer:
http://www.php.net/features.file-upload
http://www.faqs.org/rfcs/rfc1867.html

Q: How to upload a huge file to the server?
A: Uploading depends on various factors:
1. Browser timeout/capacity/configuration
2. Available space and memory at the server
3. PHP's setting to handle uploads and POST data

In most of the cases, you could be able to adjust only the (3)

Refer:
http://www.php.net/ini.core#ini.sect.file-uploads
Q: How to implement progress/status bar for file uploading?
A: This is impossible in PHP as it doesn't populate HTTP_RAW_POST_DATA
natively. Anyway, there are few hacks available:
1. Patching PHP <http://pdoru.from.ro/upload-progress-meter/>
2. Using Perl <http://www.raditha.com/php/progress.php>
3. Finding the size of "upload_tmp_dir" directory at some interval,
originally suggested by Reader Ron in
<news:42**************************@posting.google. com>
Caveats:
There is no speculation that patching solution will be implemented
natively in PHP.
(3) seems to be the only pure PHP hack; however it won't work when
multiple files are been uploaded at the same time.

Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> wrote in message
news:11********************@o13g2000cwo.googlegrou ps.com...
Q: How to implement progress/status bar for file uploading?
A: This is impossible in PHP as it doesn't populate HTTP_RAW_POST_DATA
natively. Anyway, there are few hacks available:
1. Patching PHP <http://pdoru.from.ro/upload-progress-meter/>
2. Using Perl <http://www.raditha.com/php/progress.php>
3. Finding the size of "upload_tmp_dir" directory at some interval,
originally suggested by Reader Ron in
<news:42**************************@posting.google. com>


I wouldn't say it's impossible in PHP. It's possible to use PHP CLI to
implement same technique used as the Perl script. It would actually be
fairly easy:

1. Set up PHP CLI as CGI
2. In the upload script, open php://stdin
3. Get the size of the request from the HTTP header
4. Save the body of the request to a temp file somewhere, saving the
progress info in a file
5. Open a socket connection to localhost and send the saved request body to
a regular server script (that is, handled by mod_php)

This setup is nice since the end point of the operation is back at the PHP
application, so it can be implemented fairly painlessly.
Jul 17 '05 #2

P: n/a
Chung Leong wrote:
Q: How to implement progress/status bar for file uploading?
A: This is impossible in PHP as it doesn't populate HTTP_RAW_POST_DATA natively. Anyway, there are few hacks available:
1. Patching PHP <http://pdoru.from.ro/upload-progress-meter/>
2. Using Perl <http://www.raditha.com/php/progress.php>
3. Finding the size of "upload_tmp_dir" directory at some interval, originally suggested by Reader Ron in
<news:42**************************@posting.google. com>
I wouldn't say it's impossible in PHP. It's possible to use PHP CLI

to implement same technique used as the Perl script. It would actually be fairly easy:

1. Set up PHP CLI as CGI
2. In the upload script, open php://stdin

<snip>

Sounds convincing. I don't have time to test and see. Also, the
links I've looked at say that we cannot read the post data (even via
php://stdin (?)) when the form data encoding is "multipart/form-data".
If you have time, you may release this plug in your webpage so that we
may easily link to that.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #3

P: n/a
"R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Sounds convincing. I don't have time to test and see. Also, the
links I've looked at say that we cannot read the post data (even via
php://stdin (?)) when the form data encoding is "multipart/form-data".
If you have time, you may release this plug in your webpage so that we
may easily link to that.


The CLI version of PHP doesn't process the request, that's why we use that
instead of the PHP CGI.

I just got the script working. Unfortunately, I couldn't get it to work on
my ISP account. Apache is set up to use suExec so the CGI script can't write
to session files created by the main application. In order to use this
technique you need to have full control over the server. For one thing, CLI
might not be present. For another, you have to use conditional logging.
Otherwise repeated requests to stats page (every second) will very quickly
jam up your access log.

If anyone is interested I can post the code.
Jul 17 '05 #4

P: n/a
Chung Leong wrote:
"R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> wrote in message news:11**********************@o13g2000cwo.googlegr oups.com... <snip> If anyone is interested I can post the code.


Yes, me. Would be better if you could document and release the plug
in your webpage also (apart from posting here), so that we may easily
link to that from c.l.php faq.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #5

P: n/a
"R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Chung Leong wrote:
"R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> wrote in

message
news:11**********************@o13g2000cwo.googlegr oups.com...

<snip>
If anyone is interested I can post the code.


Yes, me. Would be better if you could document and release the plug
in your webpage also (apart from posting here), so that we may easily
link to that from c.l.php faq.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/


Okay, I just got it to work at my ISP account. Turns out having the CGI PHP
access the same session files as mod_php was a bad idea. Occasionally a
session would get corrupted for some reason.

Here's a little demo:
http://www.conradish.net/bobo/upload.php

I will have to spend some time writing up some instructions and explanation
on how it works.
Jul 17 '05 #6

P: n/a
Chung Leong wrote:
<snip>
Here's a little demo:
http://www.conradish.net/bobo/upload.php

I will have to spend some time writing up some instructions and explanation on how it works.


Kudos! looks like a cool hack. Once you document it, we may amend
the FAQ entry.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #7

P: n/a
hello
i see the your class
how i can read and recive your class php for uploading file
good bye

Jul 17 '05 #8

P: n/a
How can I set up PHP CLI as CGI?
Do I have to change Apaache's httpd.conf?
The she-bang only works under *nix, can it work on a windows machine?
http://eye.cc -php- web design
Jul 24 '05 #9

P: n/a
joppeaarts wrote:
How can I set up PHP CLI as CGI?
Do I have to change Apaache's httpd.conf?
The she-bang only works under *nix, can it work on a windows machine?
http://eye.cc -php- web design


The PHP CLI works without the intervention of Apache; therefore, you cannot
set it up as CGI, nor can you modify its behaviour by editing the http.conf
file. The she-bang line is ignored under Windows, unless using CygWin
(http://www.cygwin.com/).
JW

Jul 24 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.