Multiple File Upload Code...

Discussion in 'PHP' started by JSellnau_TSCC, Aug 16, 2010.

  1. #1
    I found this code on another forum and well I am trying I am having sql problems (which I know is a different subject and is already posted in the proper forum).

    What I am asking here is whether or not this php code is actually functional for what I am trying to do.

    
    $filename=($_FILES['filename']['name']);
    $filename2=($_FILES['filename2']['name']);
    $filename3=($_FILES['filename3']['name']);
    
    
    $filedescription1 = $_POST['filedescription1'];
    $filedescription2 = $_POST['filedescription2'];
    $filedescription3 = $_POST['filedescription3'];
    
    
    
    
    
    //This is the directory where images will be saved
    
    $target = "../photos/";
    $targetx = $target . basename( $_FILES['filename']['name']);
    
    //Writes the photo to the server
    if(move_uploaded_file($_FILES['filename']['tmp_name'], $targetx))
    {
    
    //Tells you if its all ok
    }
    else {
    //Gives and error if its not
    }
    
    $targetx = $target . basename( $_FILES['filename2']['name']);
    
    //Writes the photo to the server
    if(move_uploaded_file($_FILES['filename2']['tmp_name'], $targetx))
    {
    
    //Tells you if its all ok
    }
    else {
    //Gives and error if its not
    }
    
    $targetx = $target . basename( $_FILES['filename3']['name']);
    
    //Writes the photo to the server
    if(move_uploaded_file($_FILES['filename3']['tmp_name'], $targetx))
    {
    
    //Tells you if its all ok
    }
    else {
    //Gives and error if its not
    }
    
    
    PHP:
    Thank you for your advice in advance.
     
    JSellnau_TSCC, Aug 16, 2010 IP
  2. cuteprince85

    cuteprince85 Member

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #2
    its not full script dude!!
    it just gets from another file !!!
    pm if questions
     
    cuteprince85, Aug 16, 2010 IP
  3. JSellnau_TSCC

    JSellnau_TSCC Peon

    Messages:
    166
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I know that, it is processing info from a form that I have.
     
    JSellnau_TSCC, Aug 16, 2010 IP
  4. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #4
    Assuming that the form you are posting to this from has all the inputs named properly, then this looks good to me.
     
    Thorlax402, Aug 16, 2010 IP
  5. Narrator

    Narrator Active Member

    Messages:
    392
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    80
    #5
    The code looks good, but I would rename the files rather than using the existing file names. So you don't run into issues with overwriting files.
     
    Narrator, Aug 16, 2010 IP
  6. JSellnau_TSCC

    JSellnau_TSCC Peon

    Messages:
    166
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yeah I need to add a renaming function; and a way to check for an empty upload variable to keep it from overwriting db data with empty fields. ( what I mean by this is that when i use a form to edit the db listing and leave the file upload field blank it overwrites the existing field data with the blank field info instead of leaving the existing info there if the field is left blank.)

    Been having trouble doing this...any ideas?

    Thanks for the help in advance.
     
    Last edited: Aug 19, 2010
    JSellnau_TSCC, Aug 19, 2010 IP
  7. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #7
    Use this condition to check if the file field was empty:

    
    if ($_FILES['filename']['size'] > 0) {
        // File is full so do DB updates.
    } else {
       // File is empty so skip updates.
    }
    
    PHP:
     
    Thorlax402, Aug 19, 2010 IP
  8. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #8
    Looks good to me assuming there are no problems with your HTML Form.
     
    Pudge1, Aug 19, 2010 IP