O'Reilly Forums: Chapter 5: - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Chapter 5: I can not tget the $screenshot variable to show up.

#1 User is offline   mmarkym 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 13
  • Joined: 12-February 11

Posted 06 March 2014 - 02:57 AM

I'm trying to upload a file to the server as the book does. I've created a form with a file input field but the PHP script can't seem to find $_FILES['screenshot']['name']. I've tried inserting it into a db but only the other fields make it in. If I echo it nothing. The code is below.

<?php
include('php/header.php');
include('php/navigation.php');
?>
<h3>Upload an image to the gallery:</h3>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="text" name="name" id="name" /><br />
    <input type="text" name="date" id="date" /><br />
    <input type="text" name="desc" id="desc" /><br />
    <input type="file" name="screenshot" id="screenshot" /><br />
    <input type="submit" name="submit" id="submit" />
</form>
<?php
define('UPLOADPATH', 'images2/');
	if (isset($_POST['submit']))
	{
		$name = $_POST['name'];
		$date = $_POST['date'];
		$desc = $_POST['desc'];
		$screenshot = $_FILES['screenshot']['name'];
	
		if (!empty($name) && !empty($date) && !empty($desc) && !empty($screenshot))
		{
			$target = UPLOADPATH . $screenshot;
			if (move_uploaded_file($_FILES['screenshot']['tmp_name'], $target))
			{
				
				$connection = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME)or die('error with the connection');
				$query = "INSERT INTO photos VALUES (0, '$name', '$date', '$desc', '$screenshot')";
				mysqli_query($connection, $query)or die("error with the query");
			}
			else
			{
				echo "move file failed";	
			}
		}
		else
		{
			echo "fields are empty";	
		}
	}//end if submit
	
?>
<?php
include('php/footer.php');
?>

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users