Jump to content

Understanding custom functions


graham23s

Recommended Posts

Hi Guys,

 

i am starting to use more and more functions now but was curious on a few things, this function i wrote quickly:

 

function is_valid_image_type($image_file_type)
{
if($image_file_type != "image/pjpeg")
{
  return false;
} else {
  return true;
}
}

 

part of the code that i have implemented it in:

 

  if(isset($_POST['submit']))
  {
   // misc vars - max file size 1mb //
   $max_size = 1048576;
   
   // POST vars for the image //
   $image_file_name = $_FILES['image']['name'];
   $image_file_size = $_FILES['image']['size'];
   $image_file_temp = $_FILES['image']['tmp_name'];
   $image_file_type = $_FILES['image']['type'];
   
   // POST for the usual data //
   $product_name = mysql_real_escape_string(ucwords(strtolower($_POST['product_name'])));
   $product_desc = $_POST['product_desc'];
   $product_caid = $_POST['product_cat_id'];
   $product_pice = $_POST['product_price'];
   
   // empty fields //
   if($image_file_size <= 0)
   {
    standard_message("Error","You never uploaded a product image!");
   }
   if(empty($product_name))
   {
    standard_message("Error","You never entered the products name!");
   }
   if(empty($product_desc))
   {
    standard_message("Error","You never entered a description about the product!");
   }
   // file type //
   //if($image_file_type != 'image/pjpeg')
   //{
   // standard_message("Error","Your uploaded image file doesn't seem to be a .jpg");
   //}
   // file size //
   if($image_file_size > $max_size)
   {
    standard_message("Error","Your uploaded image file is bigger than 1mb");   
   }
   
   // get the files extension //
   //$extension = substr(strrchr($image_file_name, '.'),0);
   
   // lower case the extension //
   //$extension = strtolower($extension);
   
   // check the file type //
   $test = is_valid_image_type($image_file_type);
   print $test;

 

so in the function when i return false, i assume that returns 0 and true a 1?, also once the function returns either a 0 or a 1, is it up to me to do something with the digit to stop the script?

 

thanks for any help guys

 

Graham

 

Link to comment
Share on other sites

so in the function when i return false, i assume that returns 0 and true a 1?, also once the function returns either a 0 or a 1, is it up to me to do something with the digit to stop the script?

 

Most functions (built in or custom) return true or false. So you would call your function like any other....

 

<?php
if (is_valid_image_type($image_file_type)) {
  echo "Image is valid";
} else {
  echo "Image not valid";
}
?>

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.