2016-02-05 69 views
-5

嗨iam試圖上傳到數據庫的圖像,但它插入tmp_name在數據庫中。任何人都可以幫助我。如何上傳圖像在php mysql

這裏是我的代碼

<?php 
$connection = mysql_connect("localhost", "root", "") or die(mysql_error()); 
$db = mysql_select_db("accountant", $connection); 
$title=$_POST['blog_title']; 
$description=$_POST['blog_description']; 
$name=$_FILES["image"]["tmp_name"]; 
$type=$_FILES["image"]["type"]; 
$size=$_FILES["image"]["size"]; 
$temp=$_FILES["image"]["tmp_name"]; 
$error=$_FILES["image"]["error"]; 
if($error>0) 
die("error while uploading"); 
else 
{ 
if($type == "image/png" || $type == "image/jpeg" ||$type == "image/jpg" || $type == "image/svg" || $size >2000000) 
{  
move_uploaded_file($temp,"upload/".$name); 
$sql=mysql_query("INSERT INTO blogs(image,blog_title,blog_description)values('$name','$title','$description')"); 
echo "upload complete"; 
} 
else 
{ 
die("Format not allowed or file size too big!"); 
} 
} 

蔭試圖插入的圖像得到錯誤的

警告:move_uploaded_file(上傳/ C:\ XAMPP \ tmp目錄\ php1908.tmp):未能打開流:第21行的C:\ xampp \ htdocs \ accounting \ admin \ blogs.php中的無效參數

Warning:move_uploaded_file():無法將'C:\ xampp \ tmp \ php1908.tmp'移動到'upload/C:\ xampp \ tmp \ php1908.tmp'

在數據庫它被插入爲C:XAMPP mpphp1908.tmp

+0

請[停止使用'mysql_ *'功能](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use -mysql函數功能於PHP)。 [這些擴展](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中刪除。瞭解[編寫]​​(http://en.wikipedia.org/ wiki/Prepared_statement)語句[PDO](http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart .prepared-statements.php)並考慮使用PDO,[這真的很簡單](http://jayblanchard.net/demystifying_php_pdo.html)。 –

+0

@ Fred-ii-謝謝弗雷德......但我不配得到一個downvote \ – user3284463

+0

@ user3284463你很受歡迎。這些都是無關緊要的,他們最終被刪除。 –

回答

0
<?php 
$connection = mysql_connect("localhost", "root", "") or die(mysql_error()); 
$db = mysql_select_db("accountant", $connection); 
$title=$_POST['blog_title']; 
$description=$_POST['blog_description']; 
$name=$_FILES["image"]["name"]; 
$type=$_FILES["image"]["type"]; 
$size=$_FILES["image"]["size"]; 
$temp=$_FILES["image"]["tmp_name"]; 
$error=$_FILES["image"]["error"]; 
if($error>0) 
die("error while uploading"); 
else 
{ 
if($type == "image/png" || $type == "image/jpeg" ||$type == "image/jpg" || $type == "image/svg" || $size >2000000) 
{  
move_uploaded_file($temp,"upload/".$name); 
$sql=mysql_query("INSERT INTO blogs(image,blog_title,blog_description)values('$name','$title','$description')" ); 
echo "upload complete"; 
} 
else 
{ 
die("Format not allowed or file size too big!"); 
} 
}