Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2013
    Thanked 1 Time in 1 Post

    Importing $_FILES into a function

    I'm trying to streamline the entire image uploading routine by putting all of the error handling, image renaming, and uploading into a separate function. I wrongly assumed that I could just have the form post to the same page, and then call the function and within the function, the $_FILES information from the upload would all be there. But it isn't.

    I'm passing variables into the function such as where I want the image to be uploaded, the size of the image when resized, etc..., but I don't know how to pass in a $_FILES array.

    The variables I'm using in the function are $_FILES['images']['name'] and $_FILES['images']['tmp_name']

    Now I realize I could just pass those variables in, BUT the problem is that this allows for multiple image uploads, and within the function it cycles through the $_FILES array.

    Any suggestions on how to turn image/file uploading into a function?

  2. #2
    Senior Coder
    Join Date
    Sep 2010
    Thanked 275 Times in 275 Posts
    Go to this thread, it's a little single upload page that I turned into a multi upload.


    Now everything inside the if(isset wrapper is in a sense a 'function', called by the form, and you can put all kinds of executable code inside it. You can have fun and learn with it.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.

    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  3. #3
    New Coder
    Join Date
    Sep 2011
    Thanked 7 Times in 7 Posts
    You can pass in the $_FILES array like any other variable, so...

    function something ( $files ) {
    print_r( $files );

    then call it with something( $_FILES );

    Although you will be able to access $_FILES without passing it in as it is a superglobal array, so redefining it inside a function is bad practice.

    Hope this helps out!


Posting Permissions

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