Jump to content
Sign in to follow this  

PHP File Upload - Bugged? Won't Save

Recommended Posts


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!

Share this post

Link to post
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.


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

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

  • Like 1

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...