Upload script

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
ampersand
Forum Commoner
Posts: 54
Joined: Thu Nov 28, 2002 2:00 am
Location: Norway

Upload script

Post by ampersand »

Can anyone tell me whats wrong with my upload script ? When I'm choosing a file to upload it don't work...

Code: Select all

function upload()
{
//DATABASE INFO\\
require("../settings.php");
//DATABASE INFO\\
            echo "<form name="form1" enctype="multipart/form-data" method="post" action="".$PHP_SELF."">";
            echo "<table class="newsborder" border="0" cellspacing="5" cellpadding="0">";
            echo "<tr>"; 
            echo "<td><input type="file" name="file"></td>";
            echo "<td><input type="hidden" name="MAX_FILE_SIZE" value="100000"><input name="submit" type="submit" value="Upload"></td>";
            echo "</tr>";
            echo "</table>";
            echo "</form>";
                        
        if (isset ($_POST["submit"])) 
            {
                $container = array();
            
            if($_POST["file"] == "")
                {
                    array_push($container,"You must choose a file to upload.");
                } 
    
            if (!count($container))
                {
                    copy($file); 
                    unlink($file); 

                    $result = mysql_query ("INSERT INTO gallery (gallery_file) VALUES ('" . $_POST["file"] . "')", $connection); 
                
                    if(!mysql_query($result))
                        array_push($container,"Something was not added. Error: " . mysql_error());
                    
                    else
                        array_push($container,"File uploaded succesfully.");
            
                }
            } 
}
Thanks in advance
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Upload script

Post by timvw »

ampersand wrote:

Code: Select all

$result = mysql_query ("INSERT INTO gallery (gallery_file) VALUES ('" . $_POST["file"] . "')", $connection); 
                
if(!mysql_query($result)) array_push($container,"Something was not added. Error: " .mysql_error());
What does mysql_query($result) do?
ampersand
Forum Commoner
Posts: 54
Joined: Thu Nov 28, 2002 2:00 am
Location: Norway

Post by ampersand »

thats a good question :) I'm not sure, because it was in the tutorial
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I'm pretty sure that's a typo in the tutorial. (Probably they meant something as mysql_fetch_assoc($result)

Anyway, in the manual (http://www.php.net/manual) there is a nice and working example on uploading a file. There is also a nice example of inserting something into a database. combine the two, and you're done ;)
ampersand
Forum Commoner
Posts: 54
Joined: Thu Nov 28, 2002 2:00 am
Location: Norway

Post by ampersand »

okey, thanks :)
ilovetoast
Forum Contributor
Posts: 142
Joined: Thu Jan 15, 2004 7:34 pm

Post by ilovetoast »

Doh... beat me to the answer, anyways...since I typed it already....

Well, aside from my personal peeves about the naming of those form fields....

Any problem (beyond the above) is with likely with:

Code: Select all

copy($file);
unlink($file);
First, see the PHP setup info re: fopen wrappers.

Second, assuming things are properly setup this will work in place of the above code:

Code: Select all

if (filesize($file) < $MAX_FILE_SIZE) &#123;
    $file_data = addslashes(fread(fopen($file, "r"), filesize($file)));
    do your db stuff
&#125; else &#123;
    tell them their file is too big
&#125;
peace

mmmm toast
Post Reply