PHP filesize error

Hi. I am a php novice. I have recently coded a deadline script using php. I am trying to limit the file size and file types of uploads. The file types restriction works fine.(I have limited the file type to pptx only) However, whenever I try to upload a file, an error regarding the filesize() function will be displayed on the screen. I have attached the scripts to this post. Could someone please help me resolve this error? Thanks.

Rather than uploading the file, just let us see your php script itself and the error that you got on your screen that would be enough. File attachment takes time to be approved by admin.

Tell us, what is the exact error message you are seeing?

Sorry for my ignorance. Here is the error message:


( ! ) Warning: filesize() [function.filesize]: stat failed for GENERAL EDUCATION CA10 2011.pptx in C:\\wamp\\www\\Tutorials\\maxUpload.class.php on line 77
Call Stack
#	Time	Memory	Function	Location
1	0.0006	688104	{main}( )	..\\submission.php:0
2	0.0012	716304	maxUpload->uploadFile( )	..\\submission.php:32
3	0.0018	716512	filesize ( )	..\\maxUpload.class.php:77


According to the error message, there are two files involved in this case.
submission.php and maxUpload.class.php.

Here is the code for maxUpload.class.php:


<?php
/*************************************************
 * Max's File Uploader
 *
 * Version: 1.0
 * Date: 2007-11-26
 *
 ****************************************************/
class maxUpload{
    var $uploadLocation;
    
    /**
     * Constructor to initialize class varaibles
     * The uploadLocation will be set to the actual 
     * working directory
     *
     * @return maxUpload
     */
    function maxUpload(){
        $this->uploadLocation = getcwd().DIRECTORY_SEPARATOR;
    }

    /**
     * This function sets the directory where to upload the file
     * In case of Windows server use the form: c:\\\	emp\\\\
     * In case of Unix server use the form: /tmp/
     *
     * @param String Directory where to store the files
     */
    function setUploadLocation($dir){
        $this->uploadLocation = $dir;
    }
    
    function showUploadForm($msg='',$error=''){
?>
       <div id="container">
            <div id="header"><div id="header_left"></div>
            <div id="header_main">Assignment Uploader</div><div id="header_right"></div></div>
            <div id="content">
<?php
if ($msg != ''){
    echo '<p class="msg">'.$msg.'</p>';
} else if ($error != ''){
    echo '<p class="emsg">'.$error.'</p>';

}
?>
                <form action="" method="post" enctype="multipart/form-data" >
                     <center>
                         <label>File:
                             <input name="myfile" type="file" size="30" />
                         </label>
                         <label>
                             <input type="submit" name="submitBtn" class="sbtn" value="Upload" />
                         </label>
                     </center>
                 </form>
             </div>
             <div id="footer" style="font-size: 0.7em">Powered by PHP 5 engine</div>
         </div>
<?php
    }

    function uploadFile(){
        if (!isset($_POST['submitBtn'])){
            $this->showUploadForm();
        } else {
            $msg = '';
            $error = '';
            
            //Check destination directory
            if (!file_exists($this->uploadLocation)){
                $error = "The target directory doesn't exists!";
            } else if (!is_writeable($this->uploadLocation)) {
                $error = "The target directory is not writeable!";
            } else {
                if( filesize($_FILES['myfile']['name']) > '2097152' ) {
					echo "file exceeds 2mb, please limit your filesize!";
				} else {
					
					if( pathinfo($_FILES['myfile']['name'], PATHINFO_EXTENSION) == 'pptx'){
						
						$target_path = $this->uploadLocation . basename( $_FILES['myfile']['name']);

						if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
							$msg = basename( $_FILES['myfile']['name']).
							" was uploaded successfully!";
						} else{
							$error = "The upload process failed!";
						}
					} else {
						echo "filetype not allowed";
					}
				
				}
            }

            $this->showUploadForm($msg,$error);
        }

    }

}
?>

Here is the code for submission.php:


<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        <link href="style/style.css" rel="stylesheet" type="text/css" />
    </head>

    <body>

        <?php
        require_once("maxUpload.class.php");
        date_default_timezone_set('Asia/Singapore');

//set the deadline here
        $day = 30;
        $month = 12;
        $year = 2012;
        $hour = 22;
        $minutes = 52;
        $seconds = 55;

        $now = time();
        $today = date('d m Y', time());
        $deadline = mktime($hour, $minutes, $seconds, $month, $day, $year);

        if ($now < $deadline) {


            $myUpload = new maxUpload();
            //$myUpload->setUploadLocation(getcwd().DIRECTORY_SEPARATOR);
            $myUpload->setUploadLocation("documents/");
            $myUpload->uploadFile();
            
            echo "</br></br>" . $now;
            echo "</br></br>" . $deadline;
            echo "</br></br>" . date('d m Y c', $now);
            echo "</br></br>" . date('d m Y c', $deadline);
        } else {
            echo "<strong>Deadline is already over! Please contact group leader to submit your work.</strong>";
            echo "</br></br>" . $now;
            echo "</br></br>" . $deadline;
            echo "</br></br>" . date('d m Y c', $now);
            echo "</br></br>" . date('d m Y c', $deadline);
        }
        ?>
    </body>
</html>

Why not temporarily add the line:


var_dump($_FILES);

and actually see what you are comparing to what on this line:


if( filesize($_FILES['myfile']['name']) > '2097152' )

That you are quoting the int ‘2097152’ and turning it into a string worries me a bit too, it’d probably work fine in a comparison, but honestly does not need quoting.

I think you’ll find you should be testing against ‘size’ rather than name.

In all, the best use of your time would be to read the entire section Handling File Uploads from the manual. It might take you a day or more to ingest all the advice and gotchas, but it will be time well spent if you plan doing uploads in the future.

Sorry but I do not understand your trend of thoughts as I am a newbie in php. Could you please explain what I should do to fix the error? Thank you.

I’ll put it another way for you. You should take have the decency to to read the manual page I took the trouble to find and posted for you - then come back and tell us what you do not understand.

File uploading is not trivial to do, you can do a lot of damage to your server, your reputation and your clients reputation – and you seem not to have grasped that fact.

I would also recommend to see the manual page of each functions that you have used so that you will not get another problem once we fixed your current error.