Jump to content

Returning a single error from an array


perky416

Recommended Posts

Hi Guys,

 

I have 6 file upload fileds in an array. When i try to return the error messages at this link: http://www.php.net/manual/en/features.file-upload.errors.php i get an error displayed for every upload box. I was wondering if it is possible to simply return 1 error instead of the 6? For example rather than getting "No File Uploaded No File Uploaded No File Uploaded No File Uploaded No File Uploaded No File Uploaded" i simply get "No File Uploaded". Also If only some files are uploaded no error should be displayed.

 

The code i am using is:

foreach ($_FILES['image']['error'] as $key => $error)
{
	if ($error == UPLOAD_ERR_NO_FILE)
	{
		echo "No File Uploaded";
	}

 

I have tried using "break;" which works in leaving only 1 error, however the error is still displayed if less than 6 files are uploaded.

 

All help greatly appreciated.

 

Thanks

Link to comment
Share on other sites

Please do var_dump($_FILES), and then submit your form, once you have that array dumped, please post that so we can further assist with the issue. 

 

What we are going to do is only process the foreach loop for forms that have an image uploaded, if there are blank fields, we will not even check those, because we will get an error saying nothing uploaded. 

 

If you upload two images, that have two separate errors, you'll definitely want to get a notice for each one, after we get that first part working, we'll see what other errors we don't want showing up twice and get them grouped together.

Link to comment
Share on other sites

Hi creata.physics,

 

I places the var_dump($_Files) in the foreach and this is what i got:

 

array(1) {

["image"]=> array(5) {

["name"]=> array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" }

["type"]=> array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" }

["tmp_name"]=> array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" }

["error"]=> array(6) { [0]=> int(4) [1]=> int(4) [2]=> int(4) [3]=> int(4) [4]=> int(4) [5]=> int(4) }

["size"]=> array(6) { [0]=> int(0) [1]=> int(0) [2]=> int(0) [3]=> int(0) [4]=> int(0) [5]=> int(0) } } } No File Uploaded

 

array(1) {

["image"]=> array(5) { ["name"]=> array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" } ["type"]=> array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" }

["tmp_name"]=> array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" }

["error"]=> array(6) { [0]=> int(4) [1]=> int(4) [2]=> int(4) [3]=> int(4) [4]=> int(4) [5]=> int(4) }

["size"]=> array(6) { [0]=> int(0) [1]=> int(0) [2]=> int(0) [3]=> int(0) [4]=> int(0) [5]=> int(0) } } } No File Uploaded

 

array(1) {

["image"]=> array(5) {

["name"]=> array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" }

["type"]=> array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" }

["tmp_name"]=> array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" }

["error"]=> array(6) { [0]=> int(4) [1]=> int(4) [2]=> int(4) [3]=> int(4) [4]=> int(4) [5]=> int(4) }

["size"]=> array(6) { [0]=> int(0) [1]=> int(0) [2]=> int(0) [3]=> int(0) [4]=> int(0) [5]=> int(0) } } } No File Uploaded

 

array(1) { ["image"]=> array(5) {

["name"]=> array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" }

["type"]=> array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" }

["tmp_name"]=> array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" }

["error"]=> array(6) { [0]=> int(4) [1]=> int(4) [2]=> int(4) [3]=> int(4) [4]=> int(4) [5]=> int(4) }

["size"]=> array(6) { [0]=> int(0) [1]=> int(0) [2]=> int(0) [3]=> int(0) [4]=> int(0) [5]=> int(0) } } } No File Uploaded

 

array(1) {

["image"]=> array(5) {

["name"]=> array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" }

["type"]=> array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" }

["tmp_name"]=> array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" }

["error"]=> array(6) { [0]=> int(4) [1]=> int(4) [2]=> int(4) [3]=> int(4) [4]=> int(4) [5]=> int(4) }

["size"]=> array(6) { [0]=> int(0) [1]=> int(0) [2]=> int(0) [3]=> int(0) [4]=> int(0) [5]=> int(0) } } } No File Uploaded

 

array(1) { ["image"]=> array(5) {

["name"]=> array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" }

["type"]=> array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" }

["tmp_name"]=> array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" }

["error"]=> array(6) { [0]=> int(4) [1]=> int(4) [2]=> int(4) [3]=> int(4) [4]=> int(4) [5]=> int(4) }

["size"]=> array(6) { [0]=> int(0) [1]=> int(0) [2]=> int(0) [3]=> int(0) [4]=> int(0) [5]=> int(0) } } } No File Uploaded

 

Thanks mate

Link to comment
Share on other sites

Hello, sorry, can you place it before the foreach loop? I want to see how your keys are set up.

I don't know why i asked for var_dump, it's too messy for me.

just add this above your loop:

?><pre><?php print_r($_FILES);?></pre><?php

I'll get a rough idea of how the keys are set up for our inital checks, and i'll have a better understanding of the array.

 

Basically, what we'll do is check the first array, if that is clear we'll start looping the $_FILES, inside that loop we'll add some additional checks to see what we can do about grouping errors, and displaying the necessary files.

Link to comment
Share on other sites

No need to be sorry I appreciate your help :)

 

Here is the info:

 

Array

(

    [image] => Array

        (

            [name] => Array

                (

                    [ 0 ] =>

                    [ 1 ] =>

                    [ 2 ] =>

                    [ 3 ] =>

                    [ 4 ] =>

                    [ 5 ] =>

                )

 

            [type] => Array

                (

                    [ 0 ] =>

                    [ 1 ] =>

                    [ 2 ] =>

                    [ 3 ] =>

                    [ 4 ] =>

                    [ 5 ] =>

                )

 

            [tmp_name] => Array

                (

                    [ 0 ] =>

                    [ 1 ] =>

                    [ 2 ] =>

                    [ 3 ] =>

                    [ 4 ] =>

                    [ 5 ] =>

                )

 

            [error] => Array

                (

                    [ 0 ] => 4

                    [ 1 ] => 4

                    [ 2 ] => 4

                    [ 3 ] => 4

                    [ 4 ] => 4

                    [ 5 ] => 4

                )

 

            => Array

                (

                    [ 0 ] => 0

                    [ 1 ] => 0

                    [ 2 ] => 0

                    [ 3 ] => 0

                    [ 4 ] => 0

                    [ 5 ] => 0

                )

 

        )

 

)

 

Thanks

Link to comment
Share on other sites

That is where our issue stands, your $_FILES array would need to look something like this

$_FILES => Array (1)
(
    ['file_1'] => Array (5)
    (
        ['name'] = String(23) "application_cascade.png"
        ['type'] = String(9) "image/png"
        ['tmp_name'] = String(22) "C:\xampp\tmp\phpA2.tmp"
        ['error'] = Integer(1) 0
        ['size'] = Integer(3) 524
    )
    ['file_2'] => Array (5)
    (
        ['name'] = String(23) "application.png"
        ['type'] = String(9) "image/png"
        ['tmp_name'] = String(22) "C:\xampp\tmp\phpA3.tmp"
        ['error'] = Integer(1) 0
        ['size'] = Integer(3) 524
    )
)

 

So that way, when we loop it, we have the keys from file_1 and file_1 separate, so we can establish what needs to go where, and how we can check everything.

I'm assuming the array is improperly set up because of how you have your html?

To further diagnose the issue, we need to find out where the script goes wrong.

 

First, I would check and see if $_FILES is altered in any way before your $_FILES loop.

so if you have $_FILES = anything. go ahead and comment that out, if you need to change $_FILES in any way, make another variable, such as, $var = $_FILES['needed_key'];

Once I've made sure there is no issue with $_FILES, i would then check my html and see if that processes the array proeprly.

So if you can, check and see if you alter $_FILES anywhere, if not, go ahead and post your html code for processing the upload form.

Link to comment
Share on other sites

Hi creata.physics,

 

Iv just gone through all my code, the first place $_FILES is even mentioned is the loop, and its not changed anywhere.

 

The html for the array is as follows:

 

<html>
<form method="POST" enctype="multipart/form-data" action="">
<input type="file" name="image[]" />
<input type="file" name="image[]" />
<input type="file" name="image[]" />
<input type="file" name="image[]" />
<input type="file" name="image[]" />
<input type="file" name="image[]" />
<input type="submit" name="gallery_submit" value="Upload" />
</form>
</html>

 

Thanks

Link to comment
Share on other sites

I see what i did to set up my array differently.  If you want to make it easier on yourself, you can always try this:

http://the-stickman.com/web-development/javascript/upload-multiple-files-with-a-single-file-element/

Pretty nice multiple file uploader that builds the same array structure as what i provided with a few posts ago.

 

After doing research, your array seems to be set up the standard way, which seems to be correct.

So your foreach loop is correct, you would do

if ( isSet ($_POST['gallery_submit']) )
{
// then, if the array is correct, start the loop
foreach ($_FILES['image']['error'] as $key => $error)
{
	// this will produce all errors that is not NO FILE UPLOADED.
	if ($error != UPLOAD_ERR_NO_FILE)
	{
		echo 'File '.$key.' encountered error: '.$error;
	}
	// you should add more arguments until there are no more errors to check
	//  once the script determins there aren't any errors in any of the uploaded files.
	//  then you can start with checking for proper file extensions and build sql queries.
}
}
?>
<form method="POST" enctype="multipart/form-data" action="">
<input type="file" name="image[]" id='image[]' />
<input type="file" name="image[]" id='image[]' />
<input type="file" name="image[]" id='image[]' />
<input type="submit" name="gallery_submit" value="Upload" />
</form>

and build from there, that script checks for errors, and if the error is NOT from a no upload, it will be returned. so yes, you had it correct the whole time, i was just confused on how html and php build the array, i thought the structure was wrong, which it wasn't.

 

So you'll want your script to return all errors except the no file uploaded error, because the script will tell you which file was either partially uploaded, or which file has an incorrect extension or which one was partially uploaded.  I believe all errors except for no file uploaded on your script will be completely necessary.  Sorry it took so long to figure out what was going on.  Again, if you need any further assistance, feel free to ask.

Link to comment
Share on other sites

Hi Mate,

 

Im going to do a javascript/ajax uploader as well however i wanted a php backup incase the user has javascript disabled.

 

I just tried what you said however im still getting an error for each upload field. Im trying to get it so that out of my 6 upload fields, if none of them have a file selected it should return only one UPLOAD_ERR_NO_FILE error instead of 6 errors, if only one field has a file selected, it should return no UPLOAD_ERR_NO_FILE errors.

 

Its just so that i can say to my users something like "Please select a file to upload" if they click the upload button and no file has been selected.

 

Any ideas?

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.