papaface Posted August 13, 2007 Share Posted August 13, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/64706-solved-problem-with-classes/ Share on other sites More sharing options...
frost Posted August 13, 2007 Share Posted August 13, 2007 var $filesize = $_FILES['upload']['size']; var $filetype = $_FILES['upload']['type']; var $filename = $_FILES['upload']['name']; var $tmp = $_FILES['upload']['tmp_name']; Try taking out the "var"s i dont think you need them. Quote Link to comment https://forums.phpfreaks.com/topic/64706-solved-problem-with-classes/#findComment-322678 Share on other sites More sharing options...
papaface Posted August 13, 2007 Author Share Posted August 13, 2007 I get Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in class.upload.php on line 5 the var's are required when you are working with classes. Thanks for your input though. Quote Link to comment https://forums.phpfreaks.com/topic/64706-solved-problem-with-classes/#findComment-322679 Share on other sites More sharing options...
Guest Posted August 13, 2007 Share Posted August 13, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/64706-solved-problem-with-classes/#findComment-322684 Share on other sites More sharing options...
papaface Posted August 13, 2007 Author Share Posted August 13, 2007 I get Fatal error: Non-abstract method upload::checkType() must contain body in class.upload.php on line 25 now The class works without this problem area, so I know my checkType function is fine. Quote Link to comment https://forums.phpfreaks.com/topic/64706-solved-problem-with-classes/#findComment-322689 Share on other sites More sharing options...
Guest Posted August 13, 2007 Share Posted August 13, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/64706-solved-problem-with-classes/#findComment-322691 Share on other sites More sharing options...
papaface Posted August 13, 2007 Author Share Posted August 13, 2007 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; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/64706-solved-problem-with-classes/#findComment-322692 Share on other sites More sharing options...
Guest Posted August 13, 2007 Share Posted August 13, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/64706-solved-problem-with-classes/#findComment-322694 Share on other sites More sharing options...
papaface Posted August 13, 2007 Author Share Posted August 13, 2007 ^^ modified, and I am using 5.2.1 Quote Link to comment https://forums.phpfreaks.com/topic/64706-solved-problem-with-classes/#findComment-322696 Share on other sites More sharing options...
Guest Posted August 13, 2007 Share Posted August 13, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/64706-solved-problem-with-classes/#findComment-322697 Share on other sites More sharing options...
papaface Posted August 13, 2007 Author Share Posted August 13, 2007 Thanks lol. Seems to have worked. Quote Link to comment https://forums.phpfreaks.com/topic/64706-solved-problem-with-classes/#findComment-322699 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.