help with a uppload form (mysql)

Discussion in 'Programming' started by izlik, Aug 31, 2009.

  1. #1
    (my question is in short, how can i with this solution write the size of the file im uploading aswell as the date it was uploaded to the database ? into "size" and "date" ?)

    Hello, i have a table i created like this

    CREATE TABLE movies (name VARCHAR(30), size VARCHAR(30), Description VARCHAR(30), date VARCHAR(30))

    then i have this form

    <form enctype="multipart/form-data" action="add.php" method="POST">
    movie: <input type="file" name="name"><br>
    <input type="submit" value="Add">
    </form>

    then i have this in add.php

    <?php
    
    //This is the directory where movies will be saved
    $target = "movies/";
    $target = $target . basename( $_FILES['name']);
    
    //This gets all the other information from the form
    $name=$_POST['name'];
    
    // Connects to your Database
    mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()) ;
    mysql_select_db("Database_Name") or die(mysql_error()) ;
    
    //Writes the information to the database
    mysql_query("INSERT INTO `movies` VALUES ('$name')") ;
    
    //Writes to the server
    if(move_uploaded_file($_FILES['tmp_name'], $target))
    {
    
    //Tells you if its all ok
    echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
    }
    else {
    
    //Gives and error if its not
    echo "Sorry, there was a problem uploading your file.";
    }
    ?> 
    PHP:
     
    izlik, Aug 31, 2009 IP
  2. jamespv85

    jamespv85 Peon

    Messages:
    238
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Before the file gets uploaded, somewhere in your code, get the size of the file using filesize() function and send it along with the other information in your form. I believe the user would put in the full path of the file to be uploaded, so you have a way to know which file is this and use the function accordingly.

    On the date, well the date() function will help you with this. Have your script generate the time once the files have been uploaded, doesn't have to be at the time of insert (to database), but once everything is Ok, file is saved at right path on server and all that, then that would be a good time to generate 'the time.'

    You can read up on both functions at http://www.php.net.

    BTW, are you sure you want size and date columns as VARCHARS? You can change it to INT and DATE for example.
     
    jamespv85, Sep 1, 2009 IP
  3. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #3
    Hello. i fixed the date, but for filesize, do you mean someting like $size=filesize('name'); ?
     
    izlik, Sep 1, 2009 IP
  4. pneulameiro

    pneulameiro Peon

    Messages:
    440
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes :)
    But name must be the entire path of the file and the returned filesize might be an unexpected result, because it is a 32 bit signed integer, so files with more than 2 Gb size can cause errors.
     
    pneulameiro, Sep 1, 2009 IP
  5. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #5
    the files are just going to be a few kilobytes so that is no problem! ;)

    i have another question. bellow is the new code of add.php. when i upload the .swf files it's working without a problem, however when i check the database nothing was added to it, why??

    <?php
    
    //This is the directory where flahsfiles will be saved
    $target = $_SERVER['DOCUMENT_ROOT'] . "/test/";
    $target = $target . basename( $_FILES['name']['name']); 
    
    //This gets all the other information from the form
    $name=$_POST['name'];
    $date = date("m.d.y");
    $size=filesize('name');
    
    // Connects to your Database
    mysql_connect("localhost", "user", "pass") or die(mysql_error()) ;
    mysql_select_db("database") or die(mysql_error()) ;
    
    //Writes the information to the database
    mysql_query("INSERT INTO `flash` VALUES ('$name','$size',,'$date')") ;
    
    //Writes to the server
    if(move_uploaded_file($_FILES['name']['tmp_name'], $target))
    {
    
    //Tells you if its all ok
    echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
    }
    else {
    
    //Gives and error if its not
    echo "Sorry, there was a problem uploading your file.";
    }
    
    error_reporting(E_ALL);
    ?>
    PHP:
     
    izlik, Sep 1, 2009 IP
  6. jamespv85

    jamespv85 Peon

    Messages:
    238
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Make sure you got the correct database user/password.

    Then it would help if you test it, like say for example, make a standard insert query with pre-made and correct values to that table you are using now.

    Does mysql_error() return something, some kind of error? Checking the logs might be a good idea too. :)
     
    jamespv85, Sep 2, 2009 IP