Jump to content

PHP File Upload - Bugged? Won't Save


MarkT

Recommended Posts

Hello,

I have the following code in a file called upload_file.php:

<?php  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>";	$filename = $_FILES["file"]["name"];	$filetype =	$_FILES["file"]["type"];	$title = $_POST['title'];	$content = $_POST['content'];	$image_pos = $_POST['image_pos'];	$publisher = $_POST['publisher'];	mysqli_query($con,"INSERT INTO news (title,content,image,image_pos,publisher) VALUES ('{$title}','{$content}','{$filename}','{$image_pos}','{$publisher}')");    if (file_exists("upload/" . $_FILES["file"]["name"]))      {      echo $_FILES["file"]["name"] . " already exists. ";      }    else      {      move_uploaded_file($_FILES["file"]["tmp_name"],      "upload/" . $_FILES["file"]["name"]);      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];      }    }?>

and I get the following error:

 

 

Upload: Type: Size: 0 kBTemp file: PHP Error Message Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/a7017672/public_html/upload_file.php on line 18 already exists.

 

Can someone please help me, my file permissions in the upload folder are 777 and the directory exists.

 

Can you please help with the SQL problem and the File upload?

 

Thanks in advance!

Link to comment
Share on other sites

First of all, $con doesn't have a value, so mysqli_query is failing.

 

The other problem is that $_FILES is empty.

 

Try this to see if you're getting the file.

print_r($_FILES);

Don't forget that the <form> element must have the enctype attribute:

<form enctype="multipart/form-data" ...

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...