PHP File Uploading
I'm getting index not found errors on a processing page for $_FILES. So far as I know my code is technically correct (at least the two othe开发者_运维问答r people who've looked at it can't find any errors either).
So first, the function that is called that displays the form with the file upload:
function portfolioEditor($p) {
echo "<form method=\"post\" action=\"" . siteurl . "/manage/update.php\">";
echo '<input type="text" name="name" id="name" class="grid4 first" value="' . $p['name'] . '" />';
echo '<input type="text" name="posttype" id="posttype" class="grid4" value="' . $p['posttype'] . '" />';
echo "\n<br />\n";
echo '<textarea name="content" id="content" class="grid8 first">' . $p['content'] . '</textarea>';
echo "\n<br />\n";
echo '<input type="hidden" name="MAX_FILE_SIZE" value="30000" />';
echo '<input name="file" value="' . $p['image'] . '" id="file" type="file" />';
echo '<input type="submit" value="Submit" name="submit" id="submit" />';
echo '<input type="hidden" value="true" id="fileup" name="fileup" />';
echo '</form>';
}
(take it as a given that the page with the form calls portfolioEditor($p) with details for $p filled in, or blanks for a new item.)
This is the update page (without the database insert yet)
$p = $_POST;
$p['url'] = str_replace(" ", "-", $p['name']);
foreach ($p as $k => $v) {
$p[$k] = addslashes($v);
//echo $v;
}
// FILE UPLOAD IF NEEDED
if(isset($p['fileup']) && $p['fileup'] == "true") {
$loc = sitepath . "/files";
$loc = $loc . basename( $_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $loc);
}
I have no idea why this isn't working, every resource I've seen on writing your own upload script uses almost the exact same code.
You need to add this to your form:
enctype='multipart/form-data'
So your form tag becomes:
echo "<form method=\"post\" enctype='multipart/form-data' action=\"" . siteurl . "/manage/update.php\">";
add as form
tag attribute encytype="multipart/form-data"
echo "<form method=\"post\" action=\"" . siteurl . "/manage/update.php\" encytype=\"multipart/form-data\">";
When you are submitting a file, the form should have enctype="multipart/form-data"
defined such as
echo "<form
method=\"post\"
enctype=\"multipart/form-data\"
action=\"" . siteurl . "/manage/update.php\">";
精彩评论