PHP + Forms = nothing...

Status
Not open for further replies.

nelsonbt

Ars Scholae Palatinae
891
Slowly getting moving on one of those sorts ideas we've all batted around in the back of our heads but never get around to.

Trying to set up a dynamic http file server. Got downloads working wonderfully with a little PHP script I knocked together (except one little thing I'm still investigating), but uploads have me stymied.
I've found numerous bits of sample code that all look like they should work, but they don't for me. Decided to let that sit while I started working on a login screen (don't care if it really works, just want it to keep out the casuals...). Well, found that all the sample login stuff wouldn't work either. Same problem even.

Ok, so what's the problem? It seems like submit buttons etc. don't work. They reload the page, and no file upload or login success/failure. Seems like they aren't causing the values to get passed to the next page.

Any suggestions or ideas? It's quite possible that PHP or apache aren't configured right, so defintiely make suggestions there...

Stats: latest Apache 1.3 (though I'll move it over to 2.0 when I can) and the latest PHP4 release candidate (got it because it said it might work with Apache 2.0 -- it didn't. the "stable" PHP4 release does the same thing--apache problem maybe?) running on top of win2k
 
Check the php configuration file to see if Register Globals is on or off. Some of the examples you are looking at probably assume that Register Globals is on, but if it is off the script will probably work but with strange results. What will happen is that variables that should have form data in them will instead contain nothing, and you'll wonder where the data went.
 

nelsonbt

Ars Scholae Palatinae
891
well...what I was getting at is that there seems to be a problem other that maybe code (so maybe I ought to have posted in SC)
but here's the upload.php I like best of all the ones I tried:

<HTML>
<?php
$uploadDir = './New/';
if (isset($submit)){
if ($upload != 'none'){
$dest = $uploadDir . $upload_name;
if (@copy($upload, $dest)){
echo "Successfully uploaded $dest
\n";
}
else{
echo "File Upload Failed
\n";
$perms = @fileperms($uploadDir);
$owner = @fileowner($uploadDir);
if (!$perms){
echo "Directory does not exist: $uploadDir
\n";
}
else{
$myuid = getmyuid();
if (!($perms & 2) && !(($owner == $myuid) && ($perms & 128))){
echo get_current_user(), " doesn't have permission to write in $uploadDir
\n";
}
}
}
}
else{
echo "File Upload Failed
\n";
echo "Filesize exceeds limit in FORM or php.ini
\n";
}
}
?>
<FORM ENCTYPE=multipart/form-data ACTION=upload.php METHOD=POST>
<INPUT TYPE=HIDDEN NAME=MAX_FILE_SIZE VALUE=100000000>
Upload File: <INPUT NAME=upload TYPE=FILE>

<INPUT TYPE=SUBMIT NAME=submit VALUE="upload">
</FORM>
</BODY></HTML>



After clicking submit, the page reloads, but no file is uploaded. Neither $submit or $upload contain any values.
 

CanSpice

Ars Tribunus Angusticlavius
7,147
I'm concerned that that setting is going to be removed from later versions though, and that it describes it as a security hole.
Could you explain the better way of doing this?

What Register Globals does is turn your form variables directly into PHP variables. Supposed you have a textbox whose name is "entryText". PHP will then give you a variable called "$entryText", whose value is whatever was entered into the textbox.

With Register Globals off, PHP doesn't create this variable. Instead, it gives you some arrays with all the data in it, called $HTTP_POST_VARS, $HTTP_GET_VARS, and $HTTP_COOKIE_VARS. For the above example, if your form was submitted via POST, then you'd have a variable called "$HTTP_POST_VARS[entryText]" instead.

The reason this is good is because of variable tainting. Someone could submit a form with an action of your page (so that all the variables would be submitted to your script), and set up variables of their own. Suppose you were doing some password checking with this code:

Code:
if($password eq "hello") { 
  $loggedIn = 1;
}
if($loggedIn) {
  print "Whee!\n";
}

If someone were to submit a form with "password=foo&loggedIn=1", then they could completely circumvent your password checking.

Changing Register Globals to off will make your scripts more secure, but they won't be completely secure. Only by checking all input variables first can you be sure that things are secure enough.

Just as a note for the code snippet you posted, you should do some sanity checking on $upload_name. What if someone submits a form with "upload_name=../../../../../../../../etc/passwd"? File permissions would probably not allow them to overwrite, but it's best to be safe, especially when dealing with filenames.
 

nelsonbt

Ars Scholae Palatinae
891
thank you for the explanation, but could you delve a little deeper for a PHP newbie?

fixing the "submit" part was easy enough, but now I can't figure out how the uploaded file would be stored/referenced in the array.

it looks like I need to somehow use the function move_uploaded file() (and use is_uploaded_file() for sanity check) but $HTTP_POST_VARS["upload"] has no data. Seems like it ought to return either a path to the uploaded file, or the file as an object.
 

CanSpice

Ars Tribunus Angusticlavius
7,147
Uploaded files are a little different. They don't get stored in that $HTTP_POST_VARS array, they have their own little beast called $HTTP_POST_FILES. You'll want to look at $HTTP_POST_FILES['INPUT_NAME']['tmpname'] for the temporary name of the file on your server, where INPUT_NAME is the name of the input field on the form.

There's a fairly good page that explains it a lot better than I could here, particularly since I've never used file uploading with PHP before. :)
 

nelsonbt

Ars Scholae Palatinae
891
thanks a bunch Can Spice :)

for the lurkers out there, the script now looks a lot like:
(sorry about long lines wrapping)

Code:
<HTML>
<?php
    $uploadDir = './New/';
    if (isset($HTTP_POST_VARS["submit"])){
	if ($HTTP_POST_FILES["userfile"]['name'] != ''){
	    if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {
	        if (file_exists($uploadDir.$HTTP_POST_FILES['userfile']['name'])) {
                        echo "File already exists on server
\n";
		    } else {
                    copy($HTTP_POST_FILES['userfile']['tmp_name'], $uploadDir.$HTTP_POST_FILES['userfile']['name']);                	   

 echo "Successfully uploaded " . $HTTP_POST_FILES['userfile']['name'] . "
\n";
                }
	    } else {
	    	echo "Possible file upload attack. Filename: " . $HTTP_POST_FILES['userfile']['name']. "
\n";
	    }

        } else {
	    echo "Please include a filename
\n";
        }
    }
?>
<FORM ENCTYPE=multipart/form-data ACTION=upload.php METHOD=POST>
<INPUT TYPE=HIDDEN NAME=MAX_FILE_SIZE VALUE=800000000>
Upload File: <INPUT NAME="userfile" TYPE="file">

<INPUT TYPE=SUBMIT NAME=submit VALUE="submit">
</FORM>
</BODY></HTML>
 

Statik

Ars Scholae Palatinae
1,451
Here's a snippet from my site, first, a piece from my defaults.php which is an inclusion:
Code:
//Server information
    $AbsolutePath      = "/hdd/2/home/hawkeye/hawk-eyedesigns.com/";
  //Links Database
    $DBLinksCats       = "LinkCategories";
    $DBLinks           = "Links";
    $LinksImgDir       = "/hdd/2/home/hawkeye/hawk-eyedesigns.com/images/links/";
    $linksimgdir       = "/images/links/";

Now the code that does the upload:
Code:
  //Check to see if we are inserting a category
    if ($sub == "insertc") {
        $cimage = $HTTP_POST_FILES['image']['tmp_name'];
        $image_name = $HTTP_POST_FILES['image']['name'];
        $image_size = $HTTP_POST_FILES['image']['size'];
        $image_type = $HTTP_POST_FILES['image']['type'];
        if (is_uploaded_file($cimage)) {
            copy($image, "$LinksImgDir".$image_name);
        } else {
            echo "Possible file upload attack: filename '$image'.";
        }
        $sql = "select MAX(id) from $DBLinksCats";
        $result = mysql_query($sql);
        $row = mysql_fetch_array($result);
        $id = $row[0];
        $id = $id + 1;
        $sql = "insert into $DBLinksCats (id, parent_id, name, description, image) values ('$id', '$parent_id', '$name', '$description', '$image_name')";
        $result = mysql_query($sql);
    }

This uploads the file and stores the item name in the database.

Statik
 
Status
Not open for further replies.