-5

Hi iam trying to upload an image into database but it is inserting the tmp_name in the database.Can anyone help me this .

Here is my code

<?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!");
}
}

Iam trying to insert an image getting errors as

Warning: move_uploaded_file(upload/C:\xampp\tmp\php1908.tmp): failed to open stream: Invalid argument in C:\xampp\htdocs\accounting\admin\blogs.php on line 21

Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php1908.tmp' to 'upload/C:\xampp\tmp\php1908.tmp'

In database it is inserting as C:xampp mpphp1908.tmp

Nagu
  • 9
  • 2
  • 9
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Feb 05 '16 at 18:03
  • @Fred-ii- Thanks Fred... But I didn't deserve a downvote\ – Ikhlak S. Feb 05 '16 at 18:06
  • @user3284463 You're quite welcome. These are off-topic and they eventually get deleted. – Funk Forty Niner Feb 05 '16 at 18:06
  • getting error as mov_upload_file():unable to move – Nagu Feb 05 '16 at 18:08
  • then edit your question Nagu with the code you're now using then, and see if @user3284463 can edit his answer in order to provide you with a solution. I have an idea what it is, but I'll let him go for it ;-) – Funk Forty Niner Feb 05 '16 at 18:10
  • @Nagu did you in fact change that to `$name=$_FILES["image"]["name"];`? if so, edit your question to reflect that change. – Funk Forty Niner Feb 05 '16 at 18:11
  • @user3284463 I might have even have saved you from being chased down a very deep rabbit hole here ;-) If the OP edits their question with what they're now using, then the problem lies with.................... *permissions*. ;-) most likely. If they don't, then we're still back to square one. – Funk Forty Niner Feb 05 '16 at 18:15
  • Typo. Resolved in a manner unlikely to be helpful to future readers. – Drew Feb 11 '16 at 06:19

1 Answers1

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!");
}
}
Nagu
  • 9
  • 2
  • 9