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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |