This is my first post in a long time, glad to say I have gained a lot of experience in basic programming but I have got an issue, and remembered the great community here.

I am trying to code a gallery for my girlfriends website but I want everything automated, so I'm looking for using a multi file upload to allow her to select multiple files, the script will then upload them and add information to a database ready for further manipulation.

First of all I was going to use around 5 individual file fields and work from there, but then I came across the <input type=file multiple> form element. The problem is working from the data after its submitted, I understand that to access the data I use $_Files['file']['tmp_name'][0] to access the first file of the selection of files.

The problem starts when you don't enter any files and press upload, the standard !empty() tests or even using the ['errors'] selector to tell the script that nothing has been selected, even using the upload file size but PHP doesn't seem to have a straitforward way of checking this.

I am very much a person who want to know and understand how a script works, my coding may not be the greatest but I understand what its doing, I have looked all over the internet but it just keeps saying to use JavaScript uploaders etc, I am starting to use bits of jQuery but would like the hard coding in the PHP file anyway. My desire to write and understand the scripts makes me feel like I've cheated by using other peoples work or alternative scripting.

Thanks.

Sorry if there are mistakes I wrote this on my phone...

    stormofsilence;11007147 wrote:

    I understand that to access the data I use $Files['file']['tmp_name'][0] to access the first file of the selection of files.

    Actually, I personally would probably just use a [man]foreach/man loop (using the $key => $value syntax to capture both the array indexes and their values) to loop over $FILES['file']['error'], checking to see if the value is equal to UPLOAD_ERR_OK and, if so, processing that file upload. In other words, it would look similar to the code found in this user-contributed note by 'Bob Doe' on the [man]features.file-upload.multiple[/man] manual page (with better error checking/handling).

    stormofsilence;11007147 wrote:

    The problem starts when you don't enter any files and press upload, the standard !empty() tests or even using the ['errors'] selector to tell the script that nothing has been selected, even using the upload file size but PHP doesn't seem to have a straitforward way of checking this.

    Can you show us some code examples? Using [man]empty/man to check if $_FILES['file']['error'] exists is probably the way I'd go.

      Ok so this is the code for the form:

      <form enctype="multipart/form-data" action="adminx.php?edit=gallery&mode=add" method="post" name="addimages">
      Select Image(s): <input name="image[]" type="file" multiple />
      <input type="hidden" name="submitted" value="true" /><br />
      <input name="submit" type="submit" value="Upload" />
      </form>

      And after the check for is Submitted=true,

      if(empty($_FILES['image']['error'])){

      echo 'Files OK';

      }else{

      echo 'No File Selected or other error';

      }

      I have tried putting the [0] after the $_FILES superglobal, to just check the first file, but it either always fails to give any useful information, If you select nothing and click upload, I would like the script to stop and return a message, I know I could just forget the check (as its not a public script) but one day I may want to use this somewhere. I understand the concept of cycling through with a foreach() but could you give me an example as with this extra dimension to the superglobal array [0][1] etc I'm just getting myself confused.

      The script I have put always returns 'No File selected or other error'.

      Thanks for getting back to me so quickly, sorry for the slow reply.

      Si

        Used the script you linked:

        foreach($_FILES["image"]["error"] as $key => $error){

        if($error == UPLOAD_ERR_OK){
        	echo "$error_codes[$error]";
        	move_uploaded_file($_FILES["image"]["tmp_name"][$key], $_FILES["image"]["name"][$key]) or die("Problems with upload");
        }

        }

        Doesn't seem to do anything, returns no error with no file selected, doesn't upload the files either...

          Try using php, code and/or html tags around code sections when posting. It's pretty!

          stormofsilence;11007265 wrote:

          The script I have put always returns 'No File selected or other error'.

          No, just when the user uploads a file! Check your logic...

          Check what you have in $FILES after post to begin with. That way you will both confirm that you are getting thefiles and you will also see the format of the files array.

          printf('<pre>%s</pre>', print_r($_FILES,1));
          foreach($_FILES["image"]["error"] as $key => $error)
          {
          
            
            printf('<pre>%s</pre>', print_r($_FILES,1)); //this works great, I wish there was a concise book to tell you tricks like this, sorry I learnt by trial and error (lots) and a couple books. Recommendations of a good manual??
            
            

            Ok so I tried this and it gave me a breakdown of the individual array elements, and the fact $_FILES was containing arrays, as expected. The thing was that even with no files selected there was still the additional level of arrays.

            Array
            (
                [image] => Array
                    (
                        [name] => Array
                            (
                                [0] => 
                            )
            
                    [type] => Array
                        (
                            [0] => 
                        )
            
                    [tmp_name] => Array
                        (
                            [0] => 
                        )
            
                    [error] => Array
                        (
                            [0] => 4
                        )
            
                    [size] => Array
                        (
                            [0] => 0
                        )
            
                )
            
            )
            

            This is the form submitted with nothing.

            Array
            (
                [image] => Array
                    (
                        [name] => Array
                            (
                                [0] => IMG_2968.JPG
                                [1] => IMG_2969.JPG
                            )
            
                    [type] => Array
                        (
                            [0] => 
                            [1] => 
                        )
            
                    [tmp_name] => Array
                        (
                            [0] => 
                            [1] => 
                        )
            
                    [error] => Array
                        (
                            [0] => 2
                            [1] => 2
                        )
            
                    [size] => Array
                        (
                            [0] => 0
                            [1] => 0
                        )
            
                )
            
            )
            

            This is with two files, the "tmp_name" is blank, so my earlier checks to use this to see if anything was selected would obviously fail.
            However the "name" array contains at least something to check, its null if no file selected and something if a file is selected, allowing me to then have picture checks etc etc.

            I checked with this:

            if($_FILES["image"]["name"][0] != null){
            
            echo 'you selected at least one file<br />';
            echo count($_FILES["image"]["name"]);
            }else{
            
            echo 'you must select at least one file';
            
            }
            

            This works great and gives me what I was expecting initially. All I can say is thank you for the help, but if you have suggestions on a good reference manual to good php functions I would appreciate it to build my knowledge with more advanced coding, it would probably save me a lot of coding with if's....

              Any ideas why the temp_name, size and type values are either zero or null?? The files should have some size value, not 0....

              Thanks for the help.

              Si

                stormofsilence;11007473 wrote:

                Any ideas why the temp_name, size and type values are either zero or null??

                See: [man]features.file-upload.errors[/man]

                  Ok I'm so stupid. I had set a max file size.

                  <input type="hidden" name="MAX_FILE_SIZE" class="formBox" value="2097152" />

                  So yeah, I was getting an error of 2, and after checking on-line figured out the mistake (and why the files were not being uploaded and the missing array information).

                  I still don't understand why the check of error didn't work before, the script I was shown that was meant to return error messages didn't.

                  Here's me thinking my photos were less than 2Mb...where they were more like 5.4Mb >.<

                  Now its just to hoping that the server this script will reside on will allow for multi-upload, I have had problems with an image adjustment script as the server only has 32Mb of RAM per user...

                  Sorry for the stupidity!!

                  Si

                    RAM isn't an issue when uploading huge files. The files get saved to disk. Disk space could of course be an issue.

                    What will be an issue is if you try to read those large files into memory, which will happen if you try to create an image resource with for example GD. Also note that a jpg image can be much large as an image resource than on disk. The image reousrce will be approx

                    width * height * (bpp / 8) * 1.7
                    

                    Width and height in pixesl, bpp is bits per pixel which divided by 8 gives bytes per pixel, and 1.7 is a rough approximation of additional overhead needed as far as I remember it. Thus, with 32MB RAM and a 24 bpp image, you get 32 / (3 * 1.7), or slightly less than 6.3 million pixels maximum, which in turn is roughly an image of dimensions 3000 x 2000.

                    Restricting uploaded images to these dimensions would allow you to create the resource, but leaving only 1.4 million bytes for other things. This may or may note be enough to create a thumbnail of 500 x 500 depending on how much is used up by other things. If you go this close to the limit, do this operation before anything else, and then don't forget to free the image resources again. A limit of 2000 x 2000 on uploaded images might be a better idea though, without leaving the process and desotry strategy for image reousrces.

                      Thanks for getting back to me, that's a good basic formula that I will write down. I have since checked and its not 32 its 64, its a pain as my local machine running XAMPP has 128Mb. What I am looking to do is have the script create thumbnails, and reduced size images on the fly, I created a cool little function that resizes to a specific length or height and retains aspect and trimmed the edges (quite proud), so an landscape or portrait image fits to say 150x200px. I am going to loop through this function to create a couple, what should I put at the end of the function to clear all memory, I'd like it to handle all images upto ~15MP so conserving memory is important. Disk space is fine, its unlimited.

                      Just need to find some time to get some serious back end coding done!

                      Again, any good reference books for php would be awesome...easy to read, small examples?
                      Thanks for all the help!

                        stormofsilence;11007685 wrote:

                        what should I put at the end of the function to clear all memory

                        Once you leave the scope of the function (aka once its done executing) all the resources declared and used only within the scope of the function will automatically be released. Take the simplistic example below:

                        function getnothing() {
                           $a = 2;
                           $b = "A string";
                           $c = new object();
                        
                           return NULL;
                        }

                        As soon as the return statement is finished the $a, $b, and $c will be released from memory as they are no longer able to be referenced.

                          Write a Reply...