0

So I have a problem I want to upload a file to php but I also want to POST a key as well, you see my PHP code requires a key or "k" via POST to allow a file to be uploaded or the user will be redirected.

PHP:

<?php
error_reporting(0);
ini_set('display_errors', 0);
header("Content-Type: text/text");

$key = "Place Key Here";
$uploadhost = "http://example.com/i/index.php";
$redirect = "http://example.com/index.php";

if (isset($_POST['k'])) {
    if ($_POST['k'] == $key) {
        $target = getcwd() . "/" . basename($_FILES['d']['name']);
        if (move_uploaded_file($_FILES['d']['tmp_name'], $target)) {
            $md5 = md5_file(getcwd() . "/" . basename($_FILES['d']['name']));
            rename(getcwd() . "/" . basename($_FILES['d']['name']), getcwd() . "/" . $md5 . "." . end(explode(".", $_FILES["d"]["name"])));
            echo $uploadhost . $md5 . "." . end(explode(".", $_FILES["d"]["name"]));
        } else {
            echo "Sorry, there was a problem uploading your file.";
        }
    } else {
        header('Location: '.$redirect);
    }
} else {
    header('Location: '.$redirect);
}
?>

I have looked around for a solution but all examples are for just uploading via

My.Computer.Network.UploadFile(Label1.Text, "http://example.com/i/index.php")

I have tried to POST the Key then Upload the file with the code above but no ball. There is probably a far easier way to this that I maybe over thinking/looking.

Kind Regards,
Nimesh Patel

  • Probably need to post `multipart/form-data` - perhaps [this answer](http://stackoverflow.com/a/16925159/2278086) will help? – Mark Apr 15 '15 at 19:09
  • I have tried this http://pastebin.com/YFTFsa07 << From a forum. But I get an error when posting the data, I am thinking that on line 68 that the content type need to be changed – Nimesh Patel Apr 15 '15 at 19:37

0 Answers0