1
<?php
ob_start();
$con=mysqli_connect("localhost","root","","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
$upload_exts = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($upload_exts, $file_exts))
{
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>";
// Enter your path to upload file here
if (file_exists("uploads".$_FILES["file"]["name"]))
{
echo "<div class='error'>"."(".$_FILES["file"]["name"].")". " already exists. "."</div>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],"uploads/" . $_FILES["file"]["name"]);
$imagepath = "uploads/" . $_FILES["file"]["name"].;
$sql = "UPDATE register SET imagepath='$imagepath' WHERE id='$user_coid' ";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
}
}
}
else
{
echo "<div class='error'>Invalid file</div>";
}
mysqli_close($con);
?>
我有這個腳本從用戶獲取輸入並上載服務器文件夾中的圖像,但我也希望保存圖像在數據庫中的路徑。但是我不能這樣做,任何人都可以請用這個代碼引導我上傳到服務器上的圖像的存儲路徑
什麼是錯誤?圖像是否上傳到正確的路徑?查詢是否給出錯誤? – slapyo 2014-11-24 17:32:37
**警告**:使用'mysqli'時,應該使用參數化查詢和['bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)來添加用戶數據到您的查詢。 **不要**使用字符串插值來實現此目的,因爲您將創建嚴重的[SQL注入漏洞](http://bobby-tables.com/)。 – tadman 2014-11-24 17:33:29
@slapyo圖像正在上傳到服務器文件夾,但在保存路徑時出現$ imagepath =「uploads /」語法錯誤。 $ _FILES [ 「文件」] [ 「名稱」]; – user3774056 2014-11-24 17:34:11