gianniskpf
gianniskpf

Reputation: 93

File upload script doesn't work when moved into a different function

I have some PHP file upload function that worked perfectly well. For some reason I trigger the upload function from a different spot of my site. That is a different function from the one I used to trigger it from. HTML works just fine but:

$_FILES['uploadedfile']['name']

returns "null".

What could possibly be wrong?

here's the code:

if (isset($_POST['upload'])) {

    $target_path = "uploads/";

    $target_path = $target_path.basename($_FILES['uploadedfile']['name']); 

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$target_path)) echo "ok";
    else echo "problem";
}

else {
    echo '<form enctype="multipart/form-data" method="POST">';
    echo '<input type="hidden" name="MAX_FILE_SIZE" value="100000" />';
    echo 'Choose a file to upload: <input name="uploadedfile" type="file" /><br />';
    echo '<input type="submit" name="upload" value="Upload File" />';
    echo '</form>'';
}

Upvotes: 0

Views: 669

Answers (3)

gianniskpf
gianniskpf

Reputation: 93

I found what was wrong. It was a childish mistake actually. By moving the upload script into a new function I accidentally included the "multipart/form-data" form into a simple form. That caused the submit buttons to be "confused" and leaving the $_FILES variable null.

I'd like to thank the people that posted on this topic! Cheers

Upvotes: 0

genesis
genesis

Reputation: 50974

When you're not sure, just use

var_dump($var);

in this case,

var_dump($_FILES['uploadedfile']);

or even better

var_dump($_FILES);

Upvotes: 0

Mob
Mob

Reputation: 11106

Shouldn't it be

$_FILES['uploadedfile']['name']

Note the upper-case variable.

Upvotes: 1

Related Questions