Uploading multiple files to server & MySQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • w33nie
    New Member
    • Jan 2007
    • 56

    Uploading multiple files to server & MySQL

    What I'm trying to do here, is upload a video to the ../video/ folder, and up to 5 images to the ../images/ folder. As well as the database information like title, content and each file's file information.
    But I really have no idea how to do this. The whole move_uploaded_f ile part is obviously wrong, but I'm not sure where to go from here to achieve my ends.

    [php]
    $videoUploadDir = '../video/';
    $imageUploadDir = '../images/';

    if(isset($_POST['upload']))
    {
    $title = $_POST['title'];
    $content = $_POST['content'];
    $image_number = $_POST['image_number'];
    $imageName1 = $_FILES['image1']['name'];
    $tmpImageName1 = $_FILES['image1']['tmp_name'];
    $imageName2 = $_FILES['image2']['name'];
    $tmpImageName2 = $_FILES['image2']['tmp_name'];
    $imageName3 = $_FILES['image3']['name'];
    $tmpImageName3 = $_FILES['image3']['tmp_name'];
    $imageName4 = $_FILES['image4']['name'];
    $tmpImageName4 = $_FILES['image4']['tmp_name'];
    $imageName5 = $_FILES['image5']['name'];
    $tmpImageName5 = $_FILES['image5']['tmp_name'];
    $fileName = $_FILES['video']['name'];
    $tmpName = $_FILES['video']['tmp_name'];
    $fileSize = $_FILES['video']['size'];
    $fileType = $_FILES['video']['type'];

    // the files will be saved in filePath
    $filePath = $videoUploadDir . $fileName;
    $imagePath1 = $imageUploadDir . $imageName1;
    $imagePath2 = $imageUploadDir . $imageName2;
    $imagePath3 = $imageUploadDir . $imageName3;
    $imagePath4 = $imageUploadDir . $imageName4;
    $imagePath5 = $imageUploadDir . $imageName5;
    $imageName1 = 'images/'. $imageName1;
    $imageName2 = 'images/'. $imageName2;
    $imageName3 = 'images/'. $imageName3;
    $imageName4 = 'images/'. $imageName4;
    $imageName5 = 'images/'. $imageName5;

    // move the files to the specified directory
    // if the upload directory is not writable or
    // something else went wrong $result will be false
    $result = (move_uploaded_ file($tmpName, $filePath) && ($tmpImageName1 , $imagePath1) && ($tmpImageName2 , $imagePath2) && ($tmpImageName3 , $imagePath3) && ($tmpImageName4 , $imagePath4) && ($tmpImageName5 , $imagePath5));
    if (!$result) {
    echo "Error uploading file";
    exit;
    }

    if(!get_magic_q uotes_gpc())
    {
    $fileName = addslashes($fil eName);
    $filePath = addslashes($fil ePath);
    }

    $query = "INSERT INTO video (title, content, image_number, image1, image2, image3, image4, image5, file_name, file_size, file_type, file_path)".
    "VALUES ('$title', '$content', '$image_number' , '$imageName1', '$imageName2', '$imageName3', '$imageName4', '$imageName5', '$fileName', '$fileSize', '$fileType', '$filePath')";

    mysql_query($qu ery) or die('Error, query failed : ' . mysql_error());

    echo "SUCCESS";
    }
    [/php]
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, w33nie.

    You'll want to call move_uploaded_f ile() once for each file:
    [code=php]
    if
    (
    !
    (
    move_uploaded_f ile( $tmpName, $filePath )
    &&
    move_uploaded_f ile( $tmpName2, $filePath2 )
    &&
    .
    .
    .
    )
    )
    {
    echo ' ... ';
    exit;
    }
    [/code]

    Comment

    • w33nie
      New Member
      • Jan 2007
      • 56

      #3
      Okay, tried that but when I click Upload, it came back with 'Error uploading file'

      [php]
      $videoUploadDir = '../video/';
      $imageUploadDir = '../images/';

      if(isset($_POST['upload']))
      {
      $title = $_POST['title'];
      $content = $_POST['content'];
      $image_number = $_POST['image_number'];
      $imageName1 = $_FILES['image1']['name'];
      $tmpImageName1 = $_FILES['image1']['tmp_name'];
      $imageName2 = $_FILES['image2']['name'];
      $tmpImageName2 = $_FILES['image2']['tmp_name'];
      $imageName3 = $_FILES['image3']['name'];
      $tmpImageName3 = $_FILES['image3']['tmp_name'];
      $imageName4 = $_FILES['image4']['name'];
      $tmpImageName4 = $_FILES['image4']['tmp_name'];
      $imageName5 = $_FILES['image5']['name'];
      $tmpImageName5 = $_FILES['image5']['tmp_name'];
      $fileName = $_FILES['video']['name'];
      $tmpName = $_FILES['video']['tmp_name'];
      $fileSize = $_FILES['video']['size'];
      $fileType = $_FILES['video']['type'];

      // the files will be saved in filePath
      $filePath = $videoUploadDir . $fileName;
      $imagePath1 = $imageUploadDir . $imageName1;
      $imagePath2 = $imageUploadDir . $imageName2;
      $imagePath3 = $imageUploadDir . $imageName3;
      $imagePath4 = $imageUploadDir . $imageName4;
      $imagePath5 = $imageUploadDir . $imageName5;
      $imageName1 = 'images/'. $imageName1;
      $imageName2 = 'images/'. $imageName2;
      $imageName3 = 'images/'. $imageName3;
      $imageName4 = 'images/'. $imageName4;
      $imageName5 = 'images/'. $imageName5;

      // move the files to the specified directory
      // if the upload directory is not writable or
      // something else went wrong $result will be false 1.
      if (!( move_uploaded_f ile($tmpName, $filePath) && move_uploaded_f ile($tmpImageNa me1, $imagePath1) && move_uploaded_f ile($tmpImageNa me2, $imagePath2) && move_uploaded_f ile($tmpImageNa me3, $imagePath3) && move_uploaded_f ile($tmpImageNa me4, $imagePath4) && move_uploaded_f ile($tmpImageNa me5, $imagePath5) ))
      {
      echo 'Error uploading file';
      exit;
      }

      if(!get_magic_q uotes_gpc())
      {
      $fileName = addslashes($fil eName);
      $filePath = addslashes($fil ePath);
      }

      $query = "INSERT INTO video (title, content, image_number, image1, image2, image3, image4, image5, file_name, file_size, file_type, file_path)".
      "VALUES ('$title', '$content', '$image_number' , '$imageName1', '$imageName2', '$imageName3', '$imageName4', '$imageName5', '$fileName', '$fileSize', '$fileType', '$filePath')";

      mysql_query($qu ery) or die('Error, query failed : ' . mysql_error());

      echo "SUCCESS";
      }
      [/php]

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, w33nie.

        Got anything in your error log?

        Check out this article to find out what is going on.

        Comment

        • w33nie
          New Member
          • Jan 2007
          • 56

          #5
          Apologies for not doing that in the first place.
          I'm not seeing anything different with that on now though.

          Comment

          • w33nie
            New Member
            • Jan 2007
            • 56

            #6
            Anyone know of a solution?

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, w33nie.

              Try doing one at a time.

              [code=php]
              if( ! move_uploaded_f ile($tmpName, $filePath) )
              {
              echo 'Unable to move ', $tmpName, ' to ', $filePath, '.';
              exit;
              }

              if( ! move_uploaded_f ile($tmpImageNa me1, $imagePath1) )
              {
              echo 'Unable to move ', $tmpImageName1, ' to ', $imagePath1, '.';
              exit;
              }
              [/code]

              and so on.

              Comment

              • w33nie
                New Member
                • Jan 2007
                • 56

                #8
                Yeah, when I uploaded 1 image, and it came up with "Unable to move to ../images/."

                But when I uploaded 5, it all worked.
                So how would I then prevent this from happening when I don't upload all 5, because I wont be nearly every time.

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Heya, w33nie.

                  Check for a valid filename before moving the file:
                  [code=php]
                  if( file_exists($tm pImageName1) )
                  {
                  move_uploaded_f ile($tmpImageNa me1, $imagePath1);
                  }
                  [/code]

                  Comment

                  • w33nie
                    New Member
                    • Jan 2007
                    • 56

                    #10
                    Works perfectly, thanks

                    Here's the finished php for anyone interested,

                    [php]
                    $videoUploadDir = '../video/';
                    $imageUploadDir = '../images/';

                    if(isset($_POST['upload']))
                    {
                    $title = $_POST['title'];
                    $content = $_POST['content'];
                    $image_number = $_POST['image_number'];
                    $imageName1 = $_FILES['image1']['name'];
                    $tmpImageName1 = $_FILES['image1']['tmp_name'];
                    $imageName2 = $_FILES['image2']['name'];
                    $tmpImageName2 = $_FILES['image2']['tmp_name'];
                    $imageName3 = $_FILES['image3']['name'];
                    $tmpImageName3 = $_FILES['image3']['tmp_name'];
                    $imageName4 = $_FILES['image4']['name'];
                    $tmpImageName4 = $_FILES['image4']['tmp_name'];
                    $imageName5 = $_FILES['image5']['name'];
                    $tmpImageName5 = $_FILES['image5']['tmp_name'];
                    $fileName = $_FILES['video']['name'];
                    $tmpName = $_FILES['video']['tmp_name'];
                    $fileSize = $_FILES['video']['size'];
                    $fileType = $_FILES['video']['type'];

                    // the files will be saved in filePath
                    $filePath = $videoUploadDir . $fileName;
                    $imagePath1 = $imageUploadDir . $imageName1;
                    $imagePath2 = $imageUploadDir . $imageName2;
                    $imagePath3 = $imageUploadDir . $imageName3;
                    $imagePath4 = $imageUploadDir . $imageName4;
                    $imagePath5 = $imageUploadDir . $imageName5;
                    $imageName1 = 'images/'. $imageName1;
                    $imageName2 = 'images/'. $imageName2;
                    $imageName3 = 'images/'. $imageName3;
                    $imageName4 = 'images/'. $imageName4;
                    $imageName5 = 'images/'. $imageName5;

                    // move the files to the specified directory
                    // if the upload directory is not writable or
                    // something else went wrong $result will be false
                    if( file_exists($tm pName) ){
                    move_uploaded_f ile($tmpName, $filePath);
                    }
                    if( file_exists($tm pImageName1) ){
                    move_uploaded_f ile($tmpImageNa me1, $imagePath1);
                    }
                    if( file_exists($tm pImageName2) ){
                    move_uploaded_f ile($tmpImageNa me2, $imagePath2);
                    }
                    if( file_exists($tm pImageName3) ){
                    move_uploaded_f ile($tmpImageNa me3, $imagePath3);
                    }
                    if( file_exists($tm pImageName4) ){
                    move_uploaded_f ile($tmpImageNa me4, $imagePath4);
                    }
                    if( file_exists($tm pImageName5) ){
                    move_uploaded_f ile($tmpImageNa me5, $imagePath5);
                    }

                    if(!get_magic_q uotes_gpc())
                    {
                    $fileName = addslashes($fil eName);
                    $filePath = addslashes($fil ePath);
                    }

                    $query = "INSERT INTO video (title, content, image_number, image1, image2, image3, image4, image5, file_name, file_size, file_type, file_path)".
                    "VALUES ('$title', '$content', '$image_number' , '$imageName1', '$imageName2', '$imageName3', '$imageName4', '$imageName5', '$fileName', '$fileSize', '$fileType', '$filePath')";

                    mysql_query($qu ery) or die('<hr />Error, query failed : ' . mysql_error(). '<hr />');

                    echo "<h4>Articl e uploaded</h4><br />";
                    }
                    [/php]

                    Comment

                    • pbmods
                      Recognized Expert Expert
                      • Apr 2007
                      • 5821

                      #11
                      Heya, w33nie.

                      Thanks for posting your solution.

                      Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)

                      Comment

                      • daav31
                        New Member
                        • Dec 2007
                        • 4

                        #12
                        Originally posted by pbmods
                        Heya, w33nie.

                        Thanks for posting your solution.

                        Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
                        Hi:

                        I have a problem along the same lines, but more specifically I am trying to do a loop to determine obatain the files individually that were uploaded to a directory, unlike the previous situation where each $_FILES is manually inserted into a different variable. I am not sure how I should be able to loop through $_FILES['Filedata'] and subsequently $_FILES['Filedata'] ['temp_name'] and $_FILES['Filedata'] ['name']

                        beginning with:

                        [PHP]
                        if ($_REQUEST['uploadDir']){
                        if(isset($_FILE S['Filedata']) && is_array($_FILE S['Filedata']) && isset ($_FILES['Filedata']['tmp_name']
                        ,$_FILES['Filedata']['name'], $_FILES['Filedata']['size'], $_FILES['Filedata']['error']) && intVal(
                        $_FILES['Filedata']['error']) === 0) {
                        if(move_uploade d_file($_FILES['Filedata']['tmp_name'], $upld_dir.$_FIL ES['Filedata']['name'])) {
                        $result = "Date: ".date('Y-m-d H:i:s')." File: {$_FILES['Filedata']['name']} Size:
                        {$_FILES['Filedata']['size']} Successfull uploaded.";
                        } else {
                        $result = "Date: ".date('Y-m-d H:i:s')."File: {$_FILES['Filedata']['name']}Size:
                        {$_FILES['Filedata']['size']}Error: {$_FILES['Filedata']['error']}Unable to move file.";
                        }
                        [/PHP]


                        Thanks much for any help...

                        Comment

                        • pbmods
                          Recognized Expert Expert
                          • Apr 2007
                          • 5821

                          #13
                          Heya, Daav. Welcome to TSDN!

                          What do you get if you print_r($_FILES )?

                          Comment

                          • daav31
                            New Member
                            • Dec 2007
                            • 4

                            #14
                            Originally posted by pbmods
                            Heya, Daav. Welcome to TSDN!

                            What do you get if you print_r($_FILES )?
                            Thanx for the prompt response,
                            I get [PHP]Array()[/PHP]

                            But I should also mention that the files are initially uploaded all at once via a multiple file upload flash tool into the directory uploadDir through the script I posted. Ultimately, I want to put each uploaded file as a separate record in the database, but I first can't figure out how to process each uploaded file separately with a loop (using foreach or a for loop) in the php script.

                            Comment

                            • pbmods
                              Recognized Expert Expert
                              • Apr 2007
                              • 5821

                              #15
                              Heya, Daav.

                              Looks like no files are getting uploaded.

                              The usual candidate: Did you remember to add the enctype attribute to your form?

                              [code=html]
                              <form method="post" enctype="multip art/form-data" action="...">
                              [/code]

                              Comment

                              Working...