SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Addict lundberg's Avatar
    Join Date
    Mar 2003
    Location
    Sweden
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Uploading a file and adding the path of it in my database.

    Hello,

    I'm making a diary system and i want the users to be able to upload a image for every diary entry. What I want is that a use can choose a file with a <input type="file"> and then when she's filled all the fields and clicks submit, i want the file to upload to a special directory lets say ../images/diary/ and then i want it to be named after the id that the diary entry gets (for example: 23.jpg). Then in the database i just want to save the path to the picture.

    Any tips?

    Any tutorials i should read?

    I don't know anything about uploading right now.

    Thanks in advance!

    Martin Lundberg
    Sweden

  2. #2
    SitePoint Enthusiast kermit's Avatar
    Join Date
    Dec 2002
    Location
    St. Gallen Switzerland
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi here is a piece of code.

    i've not tested it. But you should get the point.

    if (is_uploaded_file($HTTP_POST_FILES['file']['tmp_name']))
    {
    $sql = "Insert into diary bla blah";
    $quer = mysql_query($sql);
    // This gives you the last inserted id
    $lastid = mysql_insert_id();

    $upfile = trim($HTTP_POST_FILES['file']['tmp_name']);
    $file_name = $HTTP_POST_FILES['file']['name'];
    $file_typ = substr($upfile, -3);

    copy($upfile,"$lastid.$file_typ");
    }

    cheers
    kermit
    I propose that the following
    character sequence for joke markers:
    :-)
    19-Sep-82 11:44 Scott E Fahlman

  3. #3
    SitePoint Addict lundberg's Avatar
    Join Date
    Mar 2003
    Location
    Sweden
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you I will test it shortly!

    Martin Lundberg
    Sweden

  4. #4
    SitePoint Addict lundberg's Avatar
    Join Date
    Mar 2003
    Location
    Sweden
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okey, this isn't working. Here is my code:

    PHP Code:
    if (isset($_POST['diary_submit'])) { 
        if (
    $_POST['action'] = 'insert') {
            if (
    $_POST['headline']=='' or $_POST['mood']=='' or $_POST['text']=='') { 
              
    error('One or more required fields were left blank.\\n'
                     
    'Please fill them in and try again.'); 
           }
            else {
                if (
    is_uploaded_file($_FILES['image']['tmp_name'])) {
                    
    // Inserting all fields but the image field.
                    
    $the_date date("Y-m-d",time());
                    
    $the_time date("G:i",time());
                    
    $sql "INSERT INTO si_diary SET 
                             headline = '
    $_POST[headline]', 
                             mood = '
    $_POST[mood]', 
                             text = '
    $_POST[text]',
                                user = '
    $uid',
                                the_time = '
    $the_time',
                                the_date = '
    $the_date'";
                   if (!
    mysql_query($sql)) {
                      
    error('1. A database error occurred in processing your '
                             
    'submission.\\nIf this error persists, please '
                             
    'contact [email]lundberg@shell.linux.se[/email].');
                    }
                    
    $lastid mysql_insert_id();
                    
                    
    // Uploading the file.
                    
    $upfile trim($_FILES['image']['tmp_name']);
                    
    $file_name $_FILES['image']['name'];
                    
    $file_typ substr($upfile, -3);

                    
    copy($upfile,"../images/diary/$lastid.$file_typ);
                    
                    
    // Updating the image field with the image path.
                    
    $image_dir 'images/diary/$lastid.$file_typ';
                    
    $sql "UPDATE si_diary SET image=$image_dir WHERE id = $lastid";
                    if (!
    mysql_query($sql)) {
                      
    error('2. A database error occurred in processing your '
                             
    'submission.\\nIf this error persists, please '
                             
    'contact [email]lundberg@shell.linux.se[/email].');
                    }
                }
                else {
                    
    error('Possible file upload attack.\\n '
                         
    'Filename:' $_FILES['userfile']['name']);
                }
            }
        }

    Problem #1.
    I get the #2 error ( error('2. A database error...); ). The database is filled with all fields but the image field.

    Problem #2.
    The file thas is uploaded is a jpg file, but the files name in the image/diary/ directory is id.tmp (for example: 10.tmp) instead of 10.jpg.


    Any good ideas? =)

    Martin Lundberg
    Sweden

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    Singapore
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    get the file name from $_FILES['userfile']['name'], not $_FILES['userfile']['tmp_name'].

    your substr() part is using the temporary file name, and FYI file extensions need not be 3 characters long.

  6. #6
    SitePoint Addict lundberg's Avatar
    Join Date
    Mar 2003
    Location
    Sweden
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where exactly? =) Should i change $upfile = trim($_FILES['image']['tmp_name']); do $upfile = trim($_FILES['image']['name']);? I've never done uploadings before.

    Martin Lundberg
    Sweden

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    Singapore
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to get the extension, you can use

    PHP Code:
    $extension explode('.'$_FILES['userfile']['name']);
    $extension end($extension); 

  8. #8
    SitePoint Addict lundberg's Avatar
    Join Date
    Mar 2003
    Location
    Sweden
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But you also said that i should use ['name] instead of [tmp_name'] to get the name, where exactly did u mean that i should change that? =)

    Martin Lundberg
    Sweden

  9. #9
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    Singapore
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    'tmp_name' is the filename of the temporary file created on the server when you upload the file, 'name' is the name of the file on the uploader's computer.

  10. #10
    SitePoint Addict lundberg's Avatar
    Join Date
    Mar 2003
    Location
    Sweden
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I changed the extension thing to what you said:

    PHP Code:
    $extension explode('.'$_FILES['userfile']['name']); 
                    
    $extension end($extension); 

                    
    copy($upfile,"../images/diary/$lastid.$extension); 
    not the file is just named id (for example 10). Without any extension at all.

    EDIT: sorry for that =) 'userfile' is wrong =P my bad (newbie)

    Martin Lundberg
    Sweden

  11. #11
    SitePoint Addict lundberg's Avatar
    Join Date
    Mar 2003
    Location
    Sweden
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks =) the uploading works now, now there is something wrong with: $sql = "UPDATE si_diary SET image=$image_dir WHERE id=$lastid"; *thinks*

    Martin Lundberg
    Sweden

  12. #12
    SitePoint Addict lundberg's Avatar
    Join Date
    Mar 2003
    Location
    Sweden
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okey It's the $image_dir that isn't working.
    I changed the:

    $sql = "UPDATE si_diary SET image=$image_dir WHERE id = $lastid";

    into:

    $sql = "UPDATE si_diary SET image='image/diary/test.jpg' WHERE id = $lastid";

    and that worked perfect.

    Martin Lundberg
    Sweden

  13. #13
    SitePoint Addict lundberg's Avatar
    Join Date
    Mar 2003
    Location
    Sweden
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There got it to work wit this: $image_dir = "images/diary/$lastid.$extension";

    Thank you very mcuh biggulp and kermit! =))

    Martin Lundberg
    Sweden

  14. #14
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    Singapore
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you're welcome...

  15. #15
    SitePoint Member
    Join Date
    Jul 2003
    Location
    Tombstone, AZ, USA
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    posting the whole code

    Quote Originally Posted by biggulp
    you're welcome...
    Martin, perhaps you could post the entire code, so that we can see how it does fit together...

    I know that this would help at least me.

    Thanks,

    Gil Nelson
    gemnut


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •