我已经查看了许多其他关于同一问题的帖子,但我似乎仍然无法找出我的代码有什么问题。请注意,我这周开始学习 PHP 和 SQL,所以如果我犯了一个愚蠢的错误,请原谅。另外,我确信值得注意的是,在标题中发布的错误消息出现之前,还有另一个错误(在我的代码中定义)。那个错误是:
“ 已经存在。”
这告诉我该$_POST["file"]["name"]
变量未出于任何原因设置。任何帮助表示赞赏。但是,如果您要告诉我我的代码易受 SQL 注入攻击,我知道。上传脚本只能通过我的(安全)登录脚本访问。再次,欢迎所有帮助!
<?php
/* Process uploaded file */
if ($_FILES["file"]["size"] < 2097152)
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
if (file_exists($_POST["class"] . "/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists.";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$_POST["class"] . "/" . $_FILES["file"]["name"]);
$newpath = mysql_real_escape_string("uploads/" . $_POST['class'] . "/" . $_FILES['file']['name']);
$filename = mysql_real_escape_string($_FILES['file']['name']);
$description = mysql_real_escape_string($_POST["description"]);
echo "Stored in: " . $newpath;
}
}
}
else
{
echo "File must be less than two megabytes.";
}
/* Create and store information in MySQL Database */
$con = mysql_connect("localhost","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("1169030_dsp", $con);
$sql="INSERT INTO $_POST[class] (Description, Filename, File)
VALUES
('$description','$filename','$newpath')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Your file was successfully uploaded.";
mysql_close($con);
?>
这是我的输入表单 HTML:
<center>
<form action="uploadfile.php" method="post" style="background-color:#009933;color:#FFFFFF;width:320px;text-align:left;">
Description: <input type="text" name="description"><br>
File: <input type="file" name="file"><br>
Class:<br>
<Select name="Class">
<option value="period1">APES : period1</option>
<option value="period2">Chemistry I : period2</option>
<option value="period3">Lab Assist/Sci : period 3</option>
<option value="period4">Lab Assist/Sci : period 4</option>
<option value="period4">APES : period 4</option>
<option value="period5">APES : period 5</option>
<option value="period6">Lab Assist/Sci : period 6</option>
<option value="period7">Chemistry I : period 7</option>
</Select><br>
<input type="submit" name="Upload">
</form>
<center>