Chapter 5:

  • Please log in to reply
No replies to this topic

#1 mmarkym


    Active Member

  • Members
  • PipPip
  • 13 posts

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.

<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" />
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");
				echo "move file failed";	
			echo "fields are empty";	
	}//end if submit

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users