Connecting Tech Pros Worldwide Help | Site Map

File upload from a form / upload_tmp_dir query

Tim218
Guest
 
Posts: n/a
#1: Jul 17 '05
Hi Everyone

This is my first day with PHP and, not surprisingly, I've run into a problem
:-)

I want to allow file uploads to the server without exposing the
non-technical end-users to FTP settings, file naming protocols, etc. I've
found the following from http://www.zend.com/manual/features.file-upload.php
....

HTML FILE (uploadtest.html)

<form enctype="multipart/form-data" action="uploadtest.asp" method="POST">
Send this file: <input name="userfile" type="file">
<input type="submit" value="Send File">
</form>

PHP FILE (uploadtest.php)

<?php
// In PHP earlier then 4.1.0, $HTTP_POST_FILES should be used instead of
// $_FILES. In PHP earlier then 4.0.3, use copy() and is_uploaded_file()
// instead of move_uploaded_file

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir. $_FILES['userfile']['name'];

print "<pre>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
print "File is valid, and was successfully uploaded. ";
print "Here's some more debugging info:\n";
print_r($_FILES);
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($_FILES);
}
print "</pre>";
?>

END OF CODE

I select a file to upload and the delay in submitting the form suggests that
the file has been sent. But I always get the "possible file upload attack"
result. The file info shows the correct filename and type but the filesize
is always '0' and the 'tmp_name' is always 'none'.

Assuming this was the problem I did some digging using phpinfo(). The
version is 4.1.2 so I seem to be using the right commands as per the
instructions with the code. But the 'upload_tmp_dir' variable is NOT SET
which I think might be the problem.

So, with apologies for taking so long to get here, I have two queries...

1) Is there anything wrong with the code I'm using?
2) Where do uploaded files go if 'upload_tmp_dir' is not set and how can I
bypass this without access to the server (shared hosting)?

Any help would be appreciated.

Many thanks

Tim.

--
My real e-mail address is tim218 before the at followed by supermail.org.uk.


Tim218
Guest
 
Posts: n/a
#2: Jul 17 '05

re: File upload from a form / upload_tmp_dir query


Sorry the form action should be uploadtest.php not uploadtest.asp (still
getting the bad M$ habits out of my head!!).

Tim.

--
My real e-mail address is tim218 before the at followed by supermail.org.uk.

"Tim218" <see.signature@for.email.address.invalid> wrote in message
news:bj7eio$424$1@hercules.btinternet.com...[color=blue]
> Hi Everyone
>
> This is my first day with PHP and, not surprisingly, I've run into a[/color]
problem[color=blue]
> :-)
>
> I want to allow file uploads to the server without exposing the
> non-technical end-users to FTP settings, file naming protocols, etc. I've
> found the following from[/color]
http://www.zend.com/manual/features.file-upload.php[color=blue]
> ...
>
> HTML FILE (uploadtest.html)
>
> <form enctype="multipart/form-data" action="uploadtest.asp" method="POST">
> Send this file: <input name="userfile" type="file">
> <input type="submit" value="Send File">
> </form>
>
> PHP FILE (uploadtest.php)
>
> <?php
> // In PHP earlier then 4.1.0, $HTTP_POST_FILES should be used instead of
> // $_FILES. In PHP earlier then 4.0.3, use copy() and is_uploaded_file()
> // instead of move_uploaded_file
>
> $uploaddir = '/var/www/uploads/';
> $uploadfile = $uploaddir. $_FILES['userfile']['name'];
>
> print "<pre>";
> if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
> print "File is valid, and was successfully uploaded. ";
> print "Here's some more debugging info:\n";
> print_r($_FILES);
> } else {
> print "Possible file upload attack! Here's some debugging info:\n";
> print_r($_FILES);
> }
> print "</pre>";
> ?>
>
> END OF CODE
>
> I select a file to upload and the delay in submitting the form suggests[/color]
that[color=blue]
> the file has been sent. But I always get the "possible file upload[/color]
attack"[color=blue]
> result. The file info shows the correct filename and type but the[/color]
filesize[color=blue]
> is always '0' and the 'tmp_name' is always 'none'.
>
> Assuming this was the problem I did some digging using phpinfo(). The
> version is 4.1.2 so I seem to be using the right commands as per the
> instructions with the code. But the 'upload_tmp_dir' variable is NOT SET
> which I think might be the problem.
>
> So, with apologies for taking so long to get here, I have two queries...
>
> 1) Is there anything wrong with the code I'm using?
> 2) Where do uploaded files go if 'upload_tmp_dir' is not set and how can I
> bypass this without access to the server (shared hosting)?
>
> Any help would be appreciated.
>
> Many thanks
>
> Tim.
>
> --
> My real e-mail address is tim218 before the at followed by[/color]
supermail.org.uk.[color=blue]
>
>[/color]


Tim218
Guest
 
Posts: n/a
#3: Jul 17 '05

re: File upload from a form / upload_tmp_dir query


Hi Everyone

I've now solved the problem.

Many thanks

Tim.

--
My real e-mail address is tim218 before the at followed by supermail.org.uk.

"Tim218" <see.signature@for.email.address.invalid> wrote in message
news:bj7epf$4eh$1@hercules.btinternet.com...[color=blue]
> Sorry the form action should be uploadtest.php not uploadtest.asp (still
> getting the bad M$ habits out of my head!!).
>
> Tim.
>
> --
> My real e-mail address is tim218 before the at followed by[/color]
supermail.org.uk.[color=blue]
>
> "Tim218" <see.signature@for.email.address.invalid> wrote in message
> news:bj7eio$424$1@hercules.btinternet.com...[color=green]
> > Hi Everyone
> >
> > This is my first day with PHP and, not surprisingly, I've run into a[/color]
> problem[color=green]
> > :-)
> >
> > I want to allow file uploads to the server without exposing the
> > non-technical end-users to FTP settings, file naming protocols, etc.[/color][/color]
I've[color=blue][color=green]
> > found the following from[/color]
> http://www.zend.com/manual/features.file-upload.php[color=green]
> > ...
> >
> > HTML FILE (uploadtest.html)
> >
> > <form enctype="multipart/form-data" action="uploadtest.asp"[/color][/color]
method="POST">[color=blue][color=green]
> > Send this file: <input name="userfile" type="file">
> > <input type="submit" value="Send File">
> > </form>
> >
> > PHP FILE (uploadtest.php)
> >
> > <?php
> > // In PHP earlier then 4.1.0, $HTTP_POST_FILES should be used instead of
> > // $_FILES. In PHP earlier then 4.0.3, use copy() and[/color][/color]
is_uploaded_file()[color=blue][color=green]
> > // instead of move_uploaded_file
> >
> > $uploaddir = '/var/www/uploads/';
> > $uploadfile = $uploaddir. $_FILES['userfile']['name'];
> >
> > print "<pre>";
> > if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
> > print "File is valid, and was successfully uploaded. ";
> > print "Here's some more debugging info:\n";
> > print_r($_FILES);
> > } else {
> > print "Possible file upload attack! Here's some debugging info:\n";
> > print_r($_FILES);
> > }
> > print "</pre>";
> > ?>
> >
> > END OF CODE
> >
> > I select a file to upload and the delay in submitting the form suggests[/color]
> that[color=green]
> > the file has been sent. But I always get the "possible file upload[/color]
> attack"[color=green]
> > result. The file info shows the correct filename and type but the[/color]
> filesize[color=green]
> > is always '0' and the 'tmp_name' is always 'none'.
> >
> > Assuming this was the problem I did some digging using phpinfo(). The
> > version is 4.1.2 so I seem to be using the right commands as per the
> > instructions with the code. But the 'upload_tmp_dir' variable is NOT[/color][/color]
SET[color=blue][color=green]
> > which I think might be the problem.
> >
> > So, with apologies for taking so long to get here, I have two queries...
> >
> > 1) Is there anything wrong with the code I'm using?
> > 2) Where do uploaded files go if 'upload_tmp_dir' is not set and how can[/color][/color]
I[color=blue][color=green]
> > bypass this without access to the server (shared hosting)?
> >
> > Any help would be appreciated.
> >
> > Many thanks
> >
> > Tim.
> >
> > --
> > My real e-mail address is tim218 before the at followed by[/color]
> supermail.org.uk.[color=green]
> >
> >[/color]
>
>[/color]


Thomas Mlynarczyk
Guest
 
Posts: n/a
#4: Jul 17 '05

re: File upload from a form / upload_tmp_dir query


Also sprach Tim218:
[color=blue]
> Hi Everyone[/color]

Hi Tim,
[color=blue]
> I've now solved the problem.[/color]

Would you be so kind as to let us in on the details of your solution?
[color=blue]
> Many thanks[/color]

You're welcome. :-)



Tim218
Guest
 
Posts: n/a
#5: Jul 17 '05

re: File upload from a form / upload_tmp_dir query


Hi

I'm afraid I couldn't resolve the PHP problem which I think was caused by
the temporary directory setting on the server not being set (I don't have
admin control over the server).

On this occasion I went back to an ASP solution which had been my original
plan. The phpinfo() function proved useful as it let me know the server was
running Chili!ASP and once I knew this I was able to find details of its
built-in file upload function.

But my brief introduction to PHP has convinced me that it is the way forward
and that my first major ASP project should be my last. So hopefully I will
see you all in here again soon.

Best wishes

Tim.

--
My real e-mail address is tim218 before the at followed by supermail.org.uk.

"Thomas Mlynarczyk" <blue_elephant55@hotmail.com> wrote in message
news:bj840j$2c7$00$1@news.t-online.com...[color=blue]
> Also sprach Tim218:
>[color=green]
> > Hi Everyone[/color]
>
> Hi Tim,
>[color=green]
> > I've now solved the problem.[/color]
>
> Would you be so kind as to let us in on the details of your solution?
>[color=green]
> > Many thanks[/color]
>
> You're welcome. :-)[/color]



Closed Thread