Uploading Files: Definitive answer required. ;-)

This is a discussion on Uploading Files: Definitive answer required. ;-) within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi there. I've spent the weekend getting ever more frustrated, trying to get an upload file function working on ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-02-2004
dickiedyce
 
Posts: n/a
Default Uploading Files: Definitive answer required. ;-)

Hi there.

I've spent the weekend getting ever more frustrated, trying to get an
upload file function working on a website.

The site is hosted by a company called oneandone. They're using PHP 4.2.
3, *not* in safe mode. I think that the basic problem is the set-up for
the temp folder, but I don't have access to the .ini settings.

I'm pretty sure it's possible, because I've got MyPHPAdmin running, and
it's happily accepting uploaded files (although I can't make head nor
tail of their code).

I've read through the manuals online (most of which assume 'it just
sorta works' or that you have access to the .ini files), and I've tried
setting the values for upload_tmp_dir using ini_set() but I just can't
get it to work.

Here's the code I'm stuck with at the moment...

file 1: upload.php

******

<?php
$root = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
ini_set('upload_tmp_
dir', $root.'tmp');
?> <html>
<head>
<title>Administration - file
upload</title>
</head>
<body>
<h1>Upload new files</h1>
<form
enctype="multipart/form-data" action="upload2.php" method="post">

<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
Upload
this file: <input name="userfile" type="file">
<input type="submit"
value="Send File">
</form>
</body>
</html>

*****

file 2: upload2.php

*****


<?php
$root = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
ini_set('upload_tmp_
dir', $root.'tmp');
?> <html>
<head>
<title>Uploading...</title>
</
head>
<body>
<h1>Uploading file...</h1>
<?php
echo 'Document
root is '.$root."<br />\n";
echo 'Upload directory: '.ini_get('upload_
tmp_dir')."<br />\n";
echo 'Upload directory: '.get_cfg_var('upload_tmp_dir')."\n";
?>
</body>
</html>

*****

(I realise that this doesn't actually do any uploading, but I've rolled
it back as far as I can, as a follower of a walk then run school of
philosophy, just to get it to tell me where it might deign to upload
stuff, were it to work ;-) )

When I execute these two pages, the second page jsut coughs up a blank
Upload directory, as if it hasn't been set. Can somebody *please* point
me in the right direction?!?!? My desk is starting to get a forehead
shapped dent in it... I just want to upload some plain text files and a
few 4k jpgs for goodness sake!

Aaarrrgh.


Thanks.

dd
Reply With Quote
  #2 (permalink)  
Old 02-02-2004
Doug Bonomo
 
Posts: n/a
Default Re: Uploading Files: Definitive answer required. ;-)

Sorry I don't have the "definitive" answer, however I have a couple points
for you to check or consider.

First the form stuff looks fine, you have the MAX_FILE_SIZE and multipart
gibberish all just fine.

Second, there is another variable in the ini you need to set or override and
that is the upload_max_filesize, example values would be 10K 1M 100K and so
on. It is typiclaly set too small to be useful thus preventing large file
uploads until you change it.

Third, permissions on the destination directory. The web server user needs
write perms, I believe, to actually do the file copy from the temp to the
permanent home of the new file.

Finally, there is a "file_uploads" in the ini with values of 1 or 0 for on
or off. Make sure it is on.

I hope this helps you some. I struggled with file uploads a long while also
but once it works it is not too difficult. The biggest hangup is often with
permissions.

Doug B
"dickiedyce" <dickiedyce@btinernet.com> wrote in message
news:20040202141357862+0000@news.btopenworld.com.. .
> Hi there.
>
> I've spent the weekend getting ever more frustrated, trying to get an
> upload file function working on a website.
>
> The site is hosted by a company called oneandone. They're using PHP 4.2.
> 3, *not* in safe mode. I think that the basic problem is the set-up for
> the temp folder, but I don't have access to the .ini settings.
>
> I'm pretty sure it's possible, because I've got MyPHPAdmin running, and
> it's happily accepting uploaded files (although I can't make head nor
> tail of their code).
>
> I've read through the manuals online (most of which assume 'it just
> sorta works' or that you have access to the .ini files), and I've tried
> setting the values for upload_tmp_dir using ini_set() but I just can't
> get it to work.
>
> Here's the code I'm stuck with at the moment...
>
> file 1: upload.php
>
> ******
>
> <?php
> $root = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
> ini_set('upload_tmp_
> dir', $root.'tmp');
> ?> <html>
> <head>
> <title>Administration - file
> upload</title>
> </head>
> <body>
> <h1>Upload new files</h1>
> <form
> enctype="multipart/form-data" action="upload2.php" method="post">
>
> <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
> Upload
> this file: <input name="userfile" type="file">
> <input type="submit"
> value="Send File">
> </form>
> </body>
> </html>
>
> *****
>
> file 2: upload2.php
>
> *****
>
>
> <?php
> $root = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
> ini_set('upload_tmp_
> dir', $root.'tmp');
> ?> <html>
> <head>
> <title>Uploading...</title>
> </
> head>
> <body>
> <h1>Uploading file...</h1>
> <?php
> echo 'Document
> root is '.$root."<br />\n";
> echo 'Upload directory: '.ini_get('upload_
> tmp_dir')."<br />\n";
> echo 'Upload directory: '.get_cfg_var('upload_tmp_dir')."\n";
> ?>
> </body>
> </html>
>
> *****
>
> (I realise that this doesn't actually do any uploading, but I've rolled
> it back as far as I can, as a follower of a walk then run school of
> philosophy, just to get it to tell me where it might deign to upload
> stuff, were it to work ;-) )
>
> When I execute these two pages, the second page jsut coughs up a blank
> Upload directory, as if it hasn't been set. Can somebody *please* point
> me in the right direction?!?!? My desk is starting to get a forehead
> shapped dent in it... I just want to upload some plain text files and a
> few 4k jpgs for goodness sake!
>
> Aaarrrgh.
>
>
> Thanks.
>
> dd



Reply With Quote
  #3 (permalink)  
Old 02-02-2004
Duyet The Vo
 
Posts: n/a
Default Re: Uploading Files: Definitive answer required. ;-)

Hi,

You might want to put phpinfo() at the beginning of the
upload2.php file. This will show in $_FILE either it has
received the uploaded file. When there is one, the filename
is set and also a temp file is shown. You need to copy/move
the temp file to the destination folder.

This works for me ;)

HTH,

Duyet.

"dickiedyce" <dickiedyce@btinernet.com> wrote in message
news:20040202141357862+0000@news.btopenworld.com.. .
> Hi there.
>
> I've spent the weekend getting ever more frustrated, trying to get an
> upload file function working on a website.
>
> The site is hosted by a company called oneandone. They're using PHP 4.2.
> 3, *not* in safe mode. I think that the basic problem is the set-up for
> the temp folder, but I don't have access to the .ini settings.
>
> I'm pretty sure it's possible, because I've got MyPHPAdmin running, and
> it's happily accepting uploaded files (although I can't make head nor
> tail of their code).
>
> I've read through the manuals online (most of which assume 'it just
> sorta works' or that you have access to the .ini files), and I've tried
> setting the values for upload_tmp_dir using ini_set() but I just can't
> get it to work.
>
> Here's the code I'm stuck with at the moment...
>
> file 1: upload.php
>
> ******
>
> <?php
> $root = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
> ini_set('upload_tmp_
> dir', $root.'tmp');
> ?> <html>
> <head>
> <title>Administration - file
> upload</title>
> </head>
> <body>
> <h1>Upload new files</h1>
> <form
> enctype="multipart/form-data" action="upload2.php" method="post">
>
> <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
> Upload
> this file: <input name="userfile" type="file">
> <input type="submit"
> value="Send File">
> </form>
> </body>
> </html>
>
> *****
>
> file 2: upload2.php
>
> *****
>
>
> <?php
> $root = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
> ini_set('upload_tmp_
> dir', $root.'tmp');
> ?> <html>
> <head>
> <title>Uploading...</title>
> </
> head>
> <body>
> <h1>Uploading file...</h1>
> <?php
> echo 'Document
> root is '.$root."<br />\n";
> echo 'Upload directory: '.ini_get('upload_
> tmp_dir')."<br />\n";
> echo 'Upload directory: '.get_cfg_var('upload_tmp_dir')."\n";
> ?>
> </body>
> </html>
>
> *****
>
> (I realise that this doesn't actually do any uploading, but I've rolled
> it back as far as I can, as a follower of a walk then run school of
> philosophy, just to get it to tell me where it might deign to upload
> stuff, were it to work ;-) )
>
> When I execute these two pages, the second page jsut coughs up a blank
> Upload directory, as if it hasn't been set. Can somebody *please* point
> me in the right direction?!?!? My desk is starting to get a forehead
> shapped dent in it... I just want to upload some plain text files and a
> few 4k jpgs for goodness sake!
>
> Aaarrrgh.
>
>
> Thanks.
>
> dd



Reply With Quote
  #4 (permalink)  
Old 02-02-2004
Juz
 
Posts: n/a
Default Re: Uploading Files: Definitive answer required. ;-)

Try to take it easy ;)
you just have to simplyfy your code :)
i took the code directly from php.net manual

file 1: upload.php

<html>
<head>
<title>Administration - file
upload</title>
</head>
<body>
<h1>Upload new files</h1>
<form
enctype="multipart/form-data" action="upload2.php" method="post">

<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
Upload
this file: <input name="userfile" type="file">
<input type="submit"
value="Send File">
</form>
</body>
</html>
> file 2: upload2.php
>
> *****
>
>

<html>
<head>
<title>Uploading...</title>
</ head>
<body>
<h1>Uploading file...</h1>
<?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 = '/path/to/your/directory/';
$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>";

?>

</body>
</html>
>
> *****




hope it helps


Reply With Quote
  #5 (permalink)  
Old 02-02-2004
pk
 
Posts: n/a
Default Re: Uploading Files: Definitive answer required. ;-)

and make sure with that host that your

$uploaddir = '/path/to/your/directory/'; looks like this

$uploaddir = '/homepages/xx/xxxxx/htdocs/your/directory/';

do a phpinfo() if ness to get the full path being used on your account


Reply With Quote
  #6 (permalink)  
Old 02-02-2004
dickiedyce
 
Posts: n/a
Default Re: Uploading Files: Definitive answer required. ;-) SOLVED

Thanks guys.

Given that I had already copied it from the Manual, and *Still* hadn't managed to get it to work, I thought well, why the hello not!

In fact it is now working! Hurrah!

The problem was in specifying the destination directory...

So the second file is now

****

<html>
<head>
<title>Uploading...</title>
</head>
<body>
<h1>Uploading file...</h1>
<?php
// this is the bit that made it work!
$root = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
$uploaddir = $root."/uploadedimages/";
$uploadfile = $uploaddir. $_FILES['userfile']['name'];

if ($_FILES['userfile']['type']!= 'image/jpeg') {
echo 'Sorry, please upload only jpeg files.';
}
else
{
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo '<p>File is valid, and was successfully uploaded.</p>';
echo '<img src="uploadedimages/'.$_FILES['userfile']['name'].'" />';
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print "<pre>";
print_r($_FILES);
print "</pre>";
}
}
?>
</body>
</html>

****

Thanks for spurring me on guys! If at first you don't succeed... ;-)

dd
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:12 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0