Moving temporary file to a new folder but the file....
Posted: Fri Sep 11, 2009 3:04 am
He y guys I am new here and I just started learning php recently because I am interested in creating web applications. I am a little bit confused right now about the example that i get from the book. I am using Head First PHP as my guide to learning PHP. Right now, I am on the chapter where a user gets to upload an image and then basically I need to show that image on my website.
The following is the code to add score
and this is the code to view the images
My problem is that, first, the image will not appear. Also, for some reason, when I use move_uploaded_file(), I don't see my image under the folder that I have moved it to. Can you guys please help me on this one? Thank you very much, your time is very much appreciated.
The following is the code to add score
Code: Select all
<html>
<head><title>Add your high schore</title></head>
<form method ="post" enctype = "multipart/form-data" action = "<?php echo $_SERVER['PHP_SELF'];?>">
<?php
if(!isset($_POST['submit']) && !isset($_POST['name']) && !isset($_POST['score']) ){ ?>
<label for='name'>Name</label>
<input type = 'text' name = 'name' id ='name'> <br />
<label for='score'>Score</label>
<input type = 'text' name = 'score' id ='score'> <br />
<label for='screenshot'>Screen Shot</label>
<input type = 'file' name = 'screenshot' id ='screenshot'>
<input type = 'submit' name ='submit' id='submit' value='ADD'>
</form>
<?php
}
else
{
$name = $_POST['name'];
$score = $_POST['score'];
$screenshot = $_FILES['screenshot']['name'];
$dbc = mysqli_connect('localhost','root','','guitarproj');
$query = "INSERT INTO guitarwars VALUES (0, NOW(), '$name', '$score', '$screenshot')";
$result= mysqli_query($dbc,$query);
$target = 'img/' . $name . 'verified.gif';
move_uploaded_file($_FILE['screenshot']['tmp_name'], $target);
echo 'Your score has been posted under ' . $target;
}
?>
</html>Code: Select all
<html>
<head><title>The submitted scores for guitar wars</title></head>
<body>
<?php
$dbc = mysqli_connect('localhost','root', '','guitarproj');
$query = "SELECT * FROM guitarwars";
$data = mysqli_query($dbc,$query);
while($row = mysqli_fetch_array($data))
{
echo 'Name: ';
echo $row['name'] . '<br />';
echo 'Score: ';
echo $row['score'] . '<br />';
echo '<img src ="img/' . $row['name']['tmp_name'] . '.gif" />';
echo '<br />';
}
?>
</body>
</html>