Jump to content

[SOLVED] checking if files have uploaded error?


shadiadiph

Recommended Posts

I have a page which can submit up to 4 files I am checking to see if they have been uploaded using the following but keep getting the error Array to string conversion in

 

if (is_uploaded_file($_FILES['fileatt']['name'])) 
{
$fileatt_name = $_FILES['fileatt']['name']; 
$attachments1 = stripslashes($fileatt_name[0]);
$attachments2 = stripslashes($fileatt_name[1]);
$attachments3 = stripslashes($fileatt_name[2]);
$attachments4 = stripslashes($fileatt_name[3]);
}
else
{
$error ="No files were uploaded";
}

 

why am i getting the error?

Link to comment
Share on other sites

sorry not sure how to do this i need to checK first if the files have been uploaded and if they are below the MAX_FILE_SIZE if they are ok do an sql insert then create directories to save them to then upload the files to the directories i know how to do the later its just checking the file part i am not sure about

 

i tried

 

if ($_FILES["fileatt"]["error"]  == UPLOAD_ERR_OK && $_FILES["fileatt"]["size"] >0) 
{ 
// take action
}
else
{
// display error
}

 

 

but it didn't work

Link to comment
Share on other sites

thanks i shall read it all again for the 20th time lol problem with the upload error is the don't have one for if the MAX_FILE_SIZE is ok only if it isn't they have an error for if the file has uploaded its just one of those things they haven't thought of for example if this worked

 

if ($_FILES["fileatt"]["error"]  == UPLOAD_ERR_OK && $_FILES["fileatt"]["size"] >0) 
{ 

 

i would not have a problem it would be easy

Link to comment
Share on other sites

thanks that makes it clearerish

 

so if i was to use something like this

 

foreach ($_FILES["fileatt"]["error"] as $key => $error) { 
if ($error ==UPLOAD_ERR_OK)
{ 

 

UPLOAD_ERR_OK  Value: 0; There is no error, the file uploaded with success.

 

does this mean that the file size is ok too or just that a file was uploaded doesn't say if its size is ok doesn't clearly state this in the manual if i the answer is the filesize is ok i would have had this finished hours ago.

 

 

sorry i am just too tired 18 hours already today think i'll get some sleep and start again in about 4-5 hours

 

 

Link to comment
Share on other sites

mm now i am having the problem the error message is always retuning $error=="4"

 

foreach ($_FILES["fileatt"]["error"] as $key => $error) { 
if ($error == UPLOAD_ERR_OK)
{ 
$fileatt_name = $_FILES['fileatt']['name']; 
$attachments1 = stripslashes($fileatt_name[0]);
$attachments2 = stripslashes($fileatt_name[1]);
$attachments3 = stripslashes($fileatt_name[2]);
$attachments4 = stripslashes($fileatt_name[3]);
}
if ($error=="4")
{
$action="reply";
$sideaction="";
$error ="You did not upload any file attachment do you still wish to proceed?";
}
if ($error=="2")
{
$action="reply";
$sideaction="addattachments";
$error ="An error occured, you can only upload files with a maximum filesize of 200KB please try again";
}
}

 

I  only want to get the value of the first $error passed i tried $error[0] but it didn't work?

Link to comment
Share on other sites

This may not solve your problem, but it might help move you forward. You are executing your first if statement for every file you attempt to upload. So, if one file out of all fails, your code will sort of act as though they all failed... 

Link to comment
Share on other sites

Also, in response to:

I only want to get the value of the first $error passed i tried $error[0] but it didn't work?

 

In your foreach loop, $error is returning an element from the array: $_FILES["fileatt"]["error"] (not the array itself), so if you want the first error, then you will want to check $_FILES["fileatt"]["error"][0]. Or, you could just write $error = $_FILES["fileatt"]["error"];, and then check $error[0].

Link to comment
Share on other sites

Thanks for trying but i just tried this did not upload any files should have returned error 4  but it returned the error value of ==2

 

if ($_FILES["fileatt"]["error"][0]==4); 
{
$action="reply";
$sideaction="";
$error ="You did not upload any file attachment do you still wish to proceed?";
}
if ($_FILES["fileatt"]["error"][0]==2); 
{
$action="reply";
$sideaction="addattachments";
$error ="An error occured, you can only upload files with a maximum filesize of 200KB please try again";
}

Link to comment
Share on other sites

Are you sure? The code you posted has semicolons at the end of the if lines, which means any code within the brackets ({}) will be executed anyways. So, that code will always result in the $error message resulting in "An error occurred, you can only upload files..."

Now, I am at a loss if that is just a mistake in inputting to the website here...

Hope that helps.

Link to comment
Share on other sites

i saw that and took the ; out had the same problem i have now taken out the for each loop at the start
almost have it working

[with 
code]
if ($_FILES["fileatt"]["error"] == 0)  
{  
$fileatt_name = $_FILES['fileatt']['name']; 
$attachments1 = stripslashes($fileatt_name[0]);
$attachments2 = stripslashes($fileatt_name[1]);
$attachments3 = stripslashes($fileatt_name[2]);
$attachments4 = stripslashes($fileatt_name[3]);
}
else
{
$error = ($_FILES["fileatt"]["error"]);
for ($i=0; $i<count($error); $i++)
{
if ($error[$i] == 4)  
{ 
$action="reply";
$sideaction="";
$error ="You did not upload any file attachment do you still wish to proceed?";
}
if ($error[$i] == 2)
{ 
$action="reply";
$sideaction="addattachments";
$error ="An error occured, you can only upload files with a maximum filesize of 200KB please try again";
}
}
}

 

works fine but if the user uploads no file in attachment 1 and uploads one in attachment2 it returns the error of no file uploaded

Link to comment
Share on other sites

Where you have:

$error = ($_FILES["fileatt"]["error"]);
for ($i=0; $i<count($error); $i++)
{

You may want rather, something like:

$error = ($_FILES["fileatt"]["error"]);
$errorcode = true;
for ($i=0; $i<count($error); $i++)
{
if ($error[$i] == UPLOAD_ERR_OK)
{
$errorcode = false;
}
...

What may help is to look at the results of a print_r($_FILES); . You'll see that anywhere you haven't uploaded a file in the four fields, the error code will be set to 4. What you desire (with regard to error code 4), I believe, is that if any one field is UPLOAD_ERR_OK, ignore the fact that some return error code 4. That's what my code above attempts to handle.

 

But, in addition ($_FILES["fileatt"]["error"] == 0) shouldn't work. It's an array, not a numeric variable.

Link to comment
Share on other sites

The original foreach() loop from the example in the php manual will do everything you want. The loop iterates over each of the upload file fields that exist in the form. You only need to add an else {} statement to the existing if(){} test to handle the case where the file did not upload. You should also probably use a switch/case statement with one value for each possible error number so that you don't need to use a bunch of if() tests.

Link to comment
Share on other sites

mm still a problem with the file if i upload a valid file in fileatt 2 and no file in fileatt 1 it doesn't work

 

$error = ($_FILES["fileatt"]["error"]);
$errorcode = true;
for ($i=0; $i<count($error); $i++)
{
if ($error[$i] == UPLOAD_ERR_OK)
{
$errorcode = false;
}
if ($errorcode ==false)
{
$fileatt_name = $_FILES['fileatt']['name']; 
$attachments1 = stripslashes($fileatt_name[0]);
$attachments2 = stripslashes($fileatt_name[1]);
$attachments3 = stripslashes($fileatt_name[2]);
$attachments4 = stripslashes($fileatt_name[3]);
}
else
{
if ($error[$i] == 4)  
{ 
$action="reply";
$sideaction="";
$error ="You did not upload any file attachment do you still wish to proceed?";
}
if ($error[$i] == 2)
{ 
$action="reply";
$sideaction="addattachments";
$error ="An error occured, you can only upload files with a maximum filesize of 200KB please try again";
}
}

 

only seems to work if a file is uploaded in the first filebox

Link to comment
Share on other sites

Try having two loops. First, you check to see if any files succeeded. If they did, decide you don't care about whether any didn't.

Then, second loop only if no files succeeded, then try and determine why they didn't.

Only one downside, if some files succeed and others fail, you won't have any response.

$error = ($_FILES["fileatt"]["error"]);
$errorcode = true;
for ($i=0; $i<count($error); $i++)
{
if ($error[$i] == UPLOAD_ERR_OK)
{
$errorcode = false;
}
}
if ($errorcode ==false)
{
$fileatt_name = $_FILES['fileatt']['name'];
$attachments1 = stripslashes($fileatt_name[0]);
$attachments2 = stripslashes($fileatt_name[1]);
$attachments3 = stripslashes($fileatt_name[2]);
$attachments4 = stripslashes($fileatt_name[3]);
}
else
{
for ($i=0; $i<count($error); $i++)
{
if ($error[$i] == 4) 
{
$action="reply";
$sideaction="";
$error ="You did not upload any file attachment do you still wish to proceed?";
}
if ($error[$i] == 2)
{
$action="reply";
$sideaction="addattachments";
$error ="An error occured, you can only upload files with a maximum filesize of 200KB please try again";
}
}

Link to comment
Share on other sites

Threw this together based on the information in the php manual -

<?php
if(isset($_POST['submit'])){
$number_of_files = 0; // count of the number of uploaded files
$error_array = array(); // array to hold the error messages
foreach ($_FILES["fileatt"]["error"] as $key => $error) {
	if ($error == UPLOAD_ERR_OK) {
		$tmp_name = $_FILES["fileatt"]["tmp_name"][$key];
		$name = $_FILES["fileatt"]["name"][$key];
		move_uploaded_file($tmp_name, "data/$name");
		$number_of_files++; // increment the number of successful files
	} else {
		// there was an error for the current $key
/*
UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini. 
UPLOAD_ERR_FORM_SIZE Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form. 
UPLOAD_ERR_PARTIAL Value: 3; The uploaded file was only partially uploaded. 
UPLOAD_ERR_NO_FILE Value: 4; No file was uploaded. 
UPLOAD_ERR_NO_TMP_DIR Value: 6; Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3. 
UPLOAD_ERR_CANT_WRITE Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0. 
UPLOAD_ERR_EXTENSION Value: 8; File upload stopped by extension. Introduced in PHP 5.2.0. 
*/
		switch ($error) {
			case 1:
				$error_array[] = "The size of {$_FILES["fileatt"]["name"][$key]} exceeded the upload_max_filesize setting";
				break;
			case 2:
				$error_array[] = "The size of {$_FILES["fileatt"]["name"][$key]} exceeded the form's MAX_FILE_SIZE setting";
				break;
			case 3:
				$error_array[] = "The file {$_FILES["fileatt"]["name"][$key]} was only partially uploaded";
				break;		
			case 4:
				// no file was selected, if all files are requireed, generate an appropiate user error message here
				break;
			case 6:
				$error_array[] = "The server is not configureed to upload files";
				break;
			case 7:
				$error_array[] = "Could not save uploaded file due to a problem on the server";
				break;		
			case 8:
				// since there is no documentation for this error number, it probably won't be triggered for current vserions of php -
				$error_array[] = "The extension of {$_FILES["fileatt"]["name"][$key]} prevented the upload";
				break;				
		}
	}
}

// check for zero files uploaded (assuming at least one should have been uploaded) -
if($number_of_files === 0){
	$error_array[] = "No files were uploaded...";
}

// output the errors -
if(!empty($error_array)){
	echo "The following errors occured-<br />";
	foreach($error_array as $error){
		echo $error . "<br />";
	}
} else {
	echo "Thank you, your form data was processed, $number_of_files file(s) were uploaded";
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
<p>files:<br />
<input type="file" name="fileatt[]" /><br />
<input type="file" name="fileatt[]" /><br />
<input type="file" name="fileatt[]" /><br />
<input type="submit" value="Send" name="submit"/>
</p>
</form>

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.