Jump to content

[SOLVED] Problem with classes


papaface

Recommended Posts

class.upload.php

<?php

class upload
{
var $filesize = $_FILES['upload']['size'];
var $filetype = $_FILES['upload']['type'];
var $filename = $_FILES['upload']['name'];
var $tmp = $_FILES['upload']['tmp_name'];

function checkFileSize()
	{
	if	($this->filesize < 20000)
		{
		return true;
		}
	else
		{
		return false;
		}
	}

function checkType();
	{
	if	($this->filetype == "image/jpg" || $this->filetype == "image/gif" || $this->filetype == "image/jpeg")
		{
		return true;
		}
	else
		{
		return false;
		}
	}

}

?>

 

index.php

<?php

if	($_POST['submit'])
{
include("class.upload.php");//include uploading class file

$upload = new upload;//get the upload class from the included class file
if	($upload->checkFileSize())//check filesize
	{
	if	($upload->checkType())//check file type
		{

		}
	else
		{
		//do something if the file is the wrong type
		}
	}
else
	{
	//do something if the file is to large
	}

}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<table border="1">
  <tr>
    <td><input name="upload" type="file" /></td>
    <td><input name="submit" type="submit" value="Upload!" /></td>
  </tr>
</table>
</form>
</body>
</html>

 

For some reason I get this error:

Parse error: syntax error, unexpected T_VARIABLE in class.upload.php on line 5

 

Does anyone know why?

 

Any help would be appreciated!

Link to comment
Share on other sites

Actually, its because you can't use Arrays or array values as property defaults in classes.

 

// WITHIN CLASS

// won't work!
var $myProperty = $array['value'];

 

A better way to do this is set them in the constructor.

 


class upload
{
      var $filesize;
      var $filetype;
      var $filename;
      var $tmp;

      function upload()
      {
            $this->filesize = $_FILES['upload']['size'];
            $this->filetype = $_FILES['upload']['type'];
            $this->filename = $_FILES['upload']['name'];
            $this->tmp = $_FILES['upload']['tmp_name'];
      }

      // ... so on

}

 

Same effect, but correct implementation.

Link to comment
Share on other sites

No, you do need the vars before those class properties. Otherwise it will throw an error.

 

Put them back and follow my suggestion above and it should work.

Link to comment
Share on other sites

I did. Didnt work.

Class atm:

<?php

class upload
{
      var $filesize;
      var $filetype;
      var $filename;
      var $tmp;

      function upload()
      {
            $this->filesize = $_FILES['upload']['size'];
            $this->filetype = $_FILES['upload']['type'];
            $this->filename = $_FILES['upload']['name'];
            $this->tmp = $_FILES['upload']['tmp_name'];
      }

function checkFileSize()
	{
	if	($this->filesize < 20000)
		{
		return true;
		}
	else
		{
		return false;
		}
	}

function checkType();
	{
	if	($this->filetype == "image/jpg" || $this->filetype == "image/gif" || $this->filetype == "image/jpeg")
		{
		return true;
		}
	else
		{
		return false;
		}
	}

}

?>

Link to comment
Share on other sites

Oh my mistake, I thought you were following up on frost's reply.

 

But that's a pretty strange error to get. First of all, what version of PHP are you running (4?), and second, could you repost the modified script? Maybe there's something one of us overlooked.

 

EDIT:

Oh you just did it... reading, one sec

Link to comment
Share on other sites

OH, i see it.

 

function checkType();
	{
	if	($this->filetype == "image/jpg" || $this->filetype == "image/gif" || $this->filetype == "image/jpeg")
		{
		return true;
		}
	else
		{
		return false;
		}
	}

 

Look closely, there's a semicolon after checkType(), hehe.

 

Yea, that would cause that kind of error.

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.