tags:

views:

168

answers:

2

I am using this form as a file upload form(as part of a larger php script, $pk is sanitized in the actual thing):

<?php

if (isset($_GET["pk"]))
 { $pk = $_GET["pk"];}
echo '<form action="up.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<input type="hidden" name="pk" value="$pk">
<br />
<input type="submit" name="submit" value="Submit" />
</form>';
?>

I am using the following(extremely trimmed) code to handle the upload. Any syntax errors are a result of the tabifier I used.

<?php
 if (isset($_GET["pk"])) {
    $pk = $_GET["pk"];
}
$con = mysqli_connect("localhost","x","x", "x");
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
move_uploaded_file($_FILES["file"]["tmp_name"],
      "./" . $_FILES["file"]["name"]);
echo "Stored in: " . "./" . $_FILES["file"]["name"];
$fileQuery = "INSERT INTO FILES VALUES (?, ?)";
if ($fileInsert = $con->prepare($fileQuery)) {
    $fileInsert->bind_param("ss", $pk, $_FILES["file"]["name"]);
    $fileInsert->execute();
    $fileInsert->close();
} else {
    print_r($con->error);
}
?>

What I would like to know, is how do I access $pk. Is it already passed to the handling code with the form?

+1  A: 
$_POST['pk']

but you'll see $pk I guess, because you're using single quotes to echo the string.

SilentGhost
so in the handling code, should I have (isset($_POST["pk"])) { $pk = $_POST["pk"]; ?
Joshxtothe4
yep____________
SilentGhost
+1  A: 

Suggestion: $pk is not set.

So try to use $_REQUEST['pk'] instead of _GET

Also, the single quotes in your echo won't evaluate $pk. Alter

input type="hidden" name="pk" value="$pk"

to

input type="hidden" name="pk" value="' . $pk . '"

Cheers, T.

tuergeist
so in the handling code, I should have if (isset($_REQUEST["pk"])) { $pk = $_REQUEST["pk"]; ?
Joshxtothe4
@Joshxtothe4: is your handling code different from the code that produces the form?
SilentGhost
yes, i basically have form.php and upload.php. pk is passed to form.php from one page, and then i want it passed along with the file to upload.php, where both the filename and pk will be placed into a db record.
Joshxtothe4
then it doesn't matter
SilentGhost
@Joshxtothe4: yes, use it that way. (or use $_POST)You may also want to initialize `$pk` if it's not set ;) But you don't have to.
tuergeist
When I try to echo out $pk, using either POST or REQUEST, nothing is present.
Joshxtothe4
ahh yes it does
Joshxtothe4
;) fine. If nothing is presented, it means there is nothing to present :)
tuergeist