Moving temporary file to a new folder but the file....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
denniss
Forum Newbie
Posts: 9
Joined: Wed Sep 09, 2009 9:10 am

Moving temporary file to a new folder but the file....

Post by denniss »

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

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>
and this is the code to view the images

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>
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.
denniss
Forum Newbie
Posts: 9
Joined: Wed Sep 09, 2009 9:10 am

Re: Moving temporary file to a new folder but the file....

Post by denniss »

First of all thank you for your reply. I am not understanding why I cannot use 'tmp_name'. Isn't it predefine under the superglobal array $_FILES? Since I do not know the original location of the file, don't I need to use this? After I added the 'S' as you recommended me to, I got the following error.

Code: Select all

 
Warning: move_uploaded_file(/home/dennis/Documents/site/img/verified.gif) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/dennis/Documents/site/addscore.php on line 25
 
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpg24VAz' to '/home/dennis/Documents/site/img/verified.gif' in /home/dennis/Documents/site/addscore.php on line 25
previous folder was/tmp/phpg24VAzYour score has been posted under /home/dennis/Documents/site/img/verified.gif
Post Reply