我正在使用第一個文件上傳的示例代碼。輸入數據後顯示錯誤。像文件上傳時的PHP錯誤
連接successfullyUpload:bottom.png 類型:image/PNG 尺寸:5.7373046875 KB 貯存於:C:\ XAMPP \ TMP \ phpD383.tmp 說明:未定義指數:文件在C:\ XAMPP \ htdocs中\ Deen_php \ sample.php 53行 無法輸入數據:您的SQL語法錯誤;檢查對應於你的MySQL服務器版本的在線使用近「)」正確的語法手冊1
PHP
<?php
if(isset($_POST['insert']))
{
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = 'root';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn)
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$allowedExts = array("gif", "jpeg", "jpg", "png","txt");
$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"]["type"] == "image/txt"))
&& ($_FILES["file"]["size"] < 50000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
else
{
echo "Invalid file";
}
$num = $_POST['num'];
$name = $_POST['name'];
$age = $_POST['age'];
$file = $_POST['file'];
$sql = "INSERT INTO sample ".
"(num,name,age,file1) ".
"VALUES('$num','$name',$age, $file)";
mysql_select_db('test_db');
$retval = mysql_query($sql, $conn);
if(! $retval)
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "C:/" . $_FILES["fileToUpload"]["name"]);
mysql_close($conn);
}
?>
HTML
<form action="<?php $_PHP_SELF ?>" method="POST" enctype="multipart/form-data">
Num: <input type="text" name="num" />
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
File Upload<input name="file" type="file" /><br />
<input type="submit" id="insert" name="insert" value="Submit" />
</form>
謝謝。我已經解決了這個問題。但我有另一個。文件名與數據庫中存儲的不同。我想將文件存儲到我的本地文件夾,因爲它是相同的類型,如txt,jpg等move_uploaded_file($ _ FILES [「file」] [「tmp_name」],「C:/ anonymous」。$ _ FILES [「file」 ][「名稱」]);這是正確的 – Mohaideen
C:/ anonymous /".$_ FILES [「file」] [「name」]); – user3106988