Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    239
    Thanks
    47
    Thanked 3 Times in 3 Posts

    RESOLVED Multi upload script

    Can anyone please help figure out how to get the 3 upload names out of the array and into a mysql database.?

    or is there a better way of doing this altogether?


    PHP Code:
    <?php
    //set where you want to store files
    //in this example we keep file in folder upload 
    //$HTTP_POST_FILES['ufile']['name']; = upload file name
    //for example upload file name cartoon.gif . $path will be upload/cartoon.gif
    $path1"../files/".$HTTP_POST_FILES['ufile']['name'][0];
    $path2"../files/".$HTTP_POST_FILES['ufile']['name'][1];
    $path3"../files/".$HTTP_POST_FILES['ufile']['name'][2];

    //copy file to where you want to store file
    copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
    copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
    copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);


    //$HTTP_POST_FILES['ufile']['name'] = file name
    //$HTTP_POST_FILES['ufile']['size'] = file size
    //$HTTP_POST_FILES['ufile']['type'] = type of file
    echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>"
    echo 
    "File Size :".$HTTP_POST_FILES['ufile']['size'][0]."<BR/>"
    echo 
    "File Type :".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>"
    echo 
    "<img src=\"$path1\" width=\"150\" height=\"150\">";
    echo 
    "<P>";

    echo 
    "File Name :".$HTTP_POST_FILES['ufile']['name'][1]."<BR/>"
    echo 
    "File Size :".$HTTP_POST_FILES['ufile']['size'][1]."<BR/>";
    echo 
    "File Type :".$HTTP_POST_FILES['ufile']['type'][1]."<BR/>"
    echo 
    "<img src=\"$path2\" width=\"150\" height=\"150\">";
    echo 
    "<P>";

    echo 
    "File Name :".$HTTP_POST_FILES['ufile']['name'][2]."<BR/>"
    echo 
    "File Size :".$HTTP_POST_FILES['ufile']['size'][2]."<BR/>";
    echo 
    "File Type :".$HTTP_POST_FILES['ufile']['type'][2]."<BR/>"
    echo 
    "<img src=\"$path3\" width=\"150\" height=\"150\">";

    ///////////////////////////////////////////////////////

    // Use this code to display the error or success.

    $filesize1=$HTTP_POST_FILES['ufile']['size'][0];
    $filesize2=$HTTP_POST_FILES['ufile']['size'][1];
    $filesize3=$HTTP_POST_FILES['ufile']['size'][2];

    if(
    $filesize1 && $filesize2 && $filesize3 != 0)
    {
    echo 
    "We have recieved your files";
    }

    else {
    echo 
    "ERROR.....";
    }

    //////////////////////////////////////////////

    // What files that have a problem? (if found)

    if($filesize1==0) {
    echo 
    "There're something error in your first file";
    echo 
    "<BR />";
    }

    if(
    $filesize2==0) {
    echo 
    "There're something error in your second file";
    echo 
    "<BR />";
    }

    if(
    $filesize3==0) {
    echo 
    "There're something error in your third file";
    echo 
    "<BR />";
    }

    }

    ?>
    Thanks in advance.
    Last edited by SlayerACC; 05-01-2012 at 02:47 PM.

  • #2
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    239
    Thanks
    47
    Thanked 3 Times in 3 Posts
    Nevermind...

    I figured it out... shock of shocks.


    Call a parade..


    Thanks anyway.


    Slayer.

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    What was your solution? Did it involve some kind of loop? This is how I would have done it.
    PHP Code:
    <?php 
    $errors 
    = array();
    foreach(
    $_FILES["ufile"]["error"] as $key => $error)
    {
        if (
    $error == UPLOAD_ERR_OK)
        {
            
    // common variables local to this loop
            
    $filename $_FILES['ufile']['name'][$key];
            
    $filesize $_FILES['ufile']['size'][$key];
            
    $filetype $_FILES['ufile']['type'][$key];
            
    $path "../files/".$filename;
            
            
    // does the file have a filesize? If so attempty to upload it
            
    if(move_uploaded_file($_FILES['ufile']['tmp_name'][$key], $path))
            {
                echo 
    "File Name :".$filename."<br/>";  
                echo 
    "File Size :".$filesize."<br/>";  
                echo 
    "File Type :".$filetype."<br/>";  
                echo 
    "<img src=\"$path\" width=\"150\" height=\"150\"><br/>";
            }
            else 
    // there was a problem with a file, add the problem file to errors array
            
    {
                
    $errors[] = $filename." was not received.<br>";    
            }
        }
    }

    if(
    count($errors) == 0// if the errors array size is 0 all files were uploaded.

        echo 
    "We have recieved your files"

    else 
    // echo out the files that had problems

        foreach(
    $errors as $error)
        {
            echo 
    $error;    
        }

    ?>
    move_uploaded_file is better to use as it does some validation to be sure it was a valid file upload.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #4
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    239
    Thanks
    47
    Thanked 3 Times in 3 Posts
    However not as pretty as yours.. thanks by the way... very clean.

    this is what got mine to work,

    PHP Code:
    <?php

    $path1
    "../files/".$HTTP_POST_FILES['ufile']['name'][0];
    $path2"../files/".$HTTP_POST_FILES['ufile']['name'][1];
    $path3"../files/".$HTTP_POST_FILES['ufile']['name'][2];

    //copy file to where you want to store file
    copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
    copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
    copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);



    function 
    reArrayFiles(&$file_post) {

        
    $file_ary = array();
        
    $file_count count($file_post['name']);
        
    $file_keys array_keys($file_post);

        for (
    $i=0$i<$file_count$i++) {
            foreach (
    $file_keys as $key) {
                
    $file_ary[$i][$key] = $file_post[$key][$i];
            }
        }

        return 
    $file_ary;
    }

    if (
    $HTTP_POST_FILES['ufile']['name']) {
        
    $file_ary reArrayFiles($_FILES['ufile']);

        foreach (
    $file_ary as $file) {
            print 
    'File Name: ' $file['name'];
            echo 
    "<br/>";
            print 
    'File Type: ' $file['type'];
            echo 
    "<br/>";
            print 
    'File Size: ' $file['size'];
            echo 
    "<br/>";
            print 
    "<img src=\"../files/".$file['name']."\" width=\"150\" height=\"150\">";
            echo 
    "<br/>";
        }
    }

    // Use this code to display the error or success.

    $filesize1=$HTTP_POST_FILES['ufile']['size'][0];
    $filesize2=$HTTP_POST_FILES['ufile']['size'][1];
    $filesize3=$HTTP_POST_FILES['ufile']['size'][2];

    if(
    $filesize1 && $filesize2 && $filesize3 != 0)
    {
    echo 
    "We have recieved your files";

    $array $HTTP_POST_FILES['ufile']['name'];
    foreach(
    $array as $files)
    {
      if (
    strlen($files)>0)
     {
    $con mysql_connect("localhost","root","pass");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }
    mysql_select_db("database"$con);$sql="INSERT INTO `files` (`files_id`, `filename`)
    VALUES
    ('$_POST[files_id]','$files')"
    ;if (!mysql_query($sql,$con))
      {
      die(
    'Error: ' mysql_error());
      }
    echo 
    "1 record added";
    print 
    "<br><br><a href=../index.php>back</a>";
    mysql_close($con);

     }
    }
    }

    else {
    echo 
    "ERROR.....";
    }

    //////////////////////////////////////////////

    // What files that have a problem? (if found)

    if($filesize1==0) {
    echo 
    "There're something error in your first file";
    echo 
    "<BR />";
    }

    if(
    $filesize2==0) {
    echo 
    "There're something error in your second file";
    echo 
    "<BR />";
    }

    if(
    $filesize3==0) {
    echo 
    "There're something error in your third file";
    echo 
    "<BR />";
    }



    ?>
    Now to try to figure out how to add a button to create 10 browse boxes.. or one at a time dynamically.


    Slayer.
    Last edited by SlayerACC; 05-01-2012 at 01:28 PM.

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,341
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    $HTTP_POST_FILES is deprecated - OLD, very old. You shouldn't be using it as it's rumoured that it will be withdrawn in future versions of PHP.

    Instead you should be using a different superglobal called $_FILES instead. If you don't and the servers PHP version is upgraded with a version that no longer supports $HTTP_POST_FILES then your code will stop working.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    SlayerACC (05-01-2012)

  • #6
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    239
    Thanks
    47
    Thanked 3 Times in 3 Posts
    That is great info.. I would have been mad if it stopped working.


    Thanks Tango...

    I will change some things around....

    Slayer.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    To further expand, its currently disable-able. Registered long arrays are a directive that I believe was introduced in 5.0, and is now gone as of 5.4. I can test it when I get home tonight, but I'm betting that the RAW is still available since there is no superglobal for it anyway (although it'd probably be better to pull from php://input instead), but the rest from get, post, cookie, files, etc are all gone.

    So in other words, if you leave it as HTTP_POST_FILES, I'd expect that it'd fail to work much sooner than later.

  • #8
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    239
    Thanks
    47
    Thanked 3 Times in 3 Posts
    Thanks all,


    You are great as usual ... I would love to know how you guys seem to know everything..

    >>

    anyway thanks.

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,341
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Quote Originally Posted by SlayerACC View Post
    You are great as usual ... I would love to know how you guys seem to know everything..
    3 key ingredients:

    1) Time
    2) Headscratching*
    3) Experimenting
    4) Learning to debug - theorise what might be happening and then find a way to test it somehow to see if you're right or wrong.

    Thats all there is to it

    * = Use of php.net to lookup what functions do, google to get inspiration for writing code and tizag.com for help with other things such as mysql
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

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