Jump to content

error message with array


simcoweb

Recommended Posts

I'm doing a simple file upload with some modest validation parameters. I thought I had this down but I get this error message when using this code:

 

// let's do some validation for file type
if(isset($_POST['submitted'])){
$file_type = array(gif, jpg);
$file_upload = $_FILES['uploadedfile']['type'];
$max_size = "100000";
// create an error message array
$errors = array();
if(!in_array($file_type)) {
$errors[] = "The file type you have attempted to upload is not the proper format. Only .jpg or .gif files are allowed.";
exit;
}
if($_FILES['uploadedfile']['size'] > $max_size) {
$errors[] = "The file size is too large. Reduce the number of bytes below $max_size and try again";
exit;
} else {
$target_path = "images/clients/";
// add target path and file name
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);
// get temporary name
$temp_name = $_FILES['uploadedfile']['temp_name'];
// let's move the file to permanent home
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). " has been uploaded<br>";
} else {
$errors[] = "Something went wrong with the upload. Please try again.<br> ";
}
// end of else statement
}
// end of if submitted statement
}

?>

 

 

Error message is:

 

Wrong parameter count for in_array()

Link to comment
Share on other sites

It needs 2 values, one for the string, and one for the array

 

bool in_array  ( mixed $needle  , array $haystack  [, bool $strict  ] )

<?php

$string = "hello";
$list    = array('hello','bye','good morning');

if(in_array($string,$list)){

echo  'Hello is in the array';


}

else
{
echo 'not found in array';
}


?>

 

 

 

Try

 

if(!in_array($file_upload,$file_type)) {
$errors[] = "The file type you have attempted to upload is not the proper format. Only .jpg or .gif files are allowed.";
exit;
}

Link to comment
Share on other sites

That god rid of the php error, but the image upload is producing errors:

 

# The file type you have attempted to upload is not the proper format. Only .jpg or .gif files are allowed.

# Something went wrong with the upload. Please try again.

 

Even though i've tried both .gif and .jpg neither uploads. BUT, if I try uploading a .doc file it does upload but still throws the error that only .gif or .jpg files can be uploaded. Totally confused.

Link to comment
Share on other sites

When you have a comparison in your code that fails, you need to display what the actual values being compared are so that you can see why the comparison failed.

 

The ['type'] element for .gif and .jpg files can be any of the following -

 

"image/gif", "image/jpeg", "image/pjpeg"

 

Ref: http://www.w3schools.com/media/media_mimeref.asp

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.