2016-02-05 88 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!"); 
} 
}