親愛的朋友們,我無法弄清楚我的代碼有什麼問題,請好好看一下。 當我發佈頁面時,它不會上傳文件,甚至$ _FILES [「file」] [「name」]爲空。php MySQLi文件上傳
由於我使用包含一次的技術,添加類別文件被稱爲模塊,因此表單action ='index.php?addcategory'只是再次調用同一個文件。
// FILE UPLOAD
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
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>";
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "img/" . $_FILES["file"]["name"];
}
}
} else {
echo "Invalid file";
}
// FILE UPLOAD
$displayname = mysqli_real_escape_string($con, $_GET['displayname']);
$categoryname = mysqli_real_escape_string($con, $_GET['categoryname']);
$meta = mysqli_real_escape_string($con, $_GET['meta']);
//$file = mysqli_real_escape_string($con, $_GET['file']);
$file = $_FILES["file"]["name"];
if ($displayname == null || $categoryname == null || $file == null)
{
$msg = '<div class="msg error"><span>ERROR:</span> Please fill in all required fields!</div>';
} else {
$insert_row = $con->query("INSERT INTO category (displayname, categoryname, meta, icon)
VALUES($displayname, $categoryname, $meta, $file)");
$sql="INSERT INTO category (displayname, categoryname, meta, icon)
VALUES ('$displayname', '$categoryname', '$meta', '$file')";
if (!mysqli_query($con,$sql)) {
$msg ='<div class="msg error"><span>ERROR:</span> Category Added Failed</div>';
} else {
$msg ='<div class="msg success">Category Added Success</div>';
}}}
echo $msg;
?>
<form action='index.php?addcategory' enctype="multipart/form-data" method=」post」>
<input name="addcategory" type="hidden" value="addcategory">
<div class="formbox"><label>Category Display Name</label>
<input name="displayname" type="text"></div>
<div class="formbox"><label>Short Name</label>
<input name="categoryname" type="text"></div>
<div class="formbox formbox_full"><label>Meta Description</label>
<textarea name="meta"></textarea></div>
<div class="formbox"><label>Category Name</label>
<input name="file" id="file" type="file"></div>
<div class="formbox formbox_full"><input type="submit" value="Add Category" /></div>
</form>
你在'method =「post」'中有聰明/誇張的引號,這就是原因。用'method ='post'替換' – 2014-10-07 02:36:57
if method =「post」然後它根本不發佈,URL也沒有顯示變量 – user1231648 2014-10-07 02:40:52
你有2個INSERT查詢,我很困惑。刪除第一個。另外,你爲什麼使用GET? – 2014-10-07 02:44:52