Question:

PHP Filesize() function...?

by  |  earlier

0 LIKES UnLike

Hi, I'm using a filesize() function I saw online (see very bottom of script) and it keeps generating the following error...

Warning: filesize() [function.filesize]: stat failed for example.jpg

Apparently this happens if it's not writeable, however if I take out the check and stick in the code to upload the file it uploads without any problems.

What the script should do is redirect to an error page should the file be larger than 50kb, if not it should just end and do nothing (at least for now).

Does anyone have any idea? I took the filesize bit from here http://www.zymic.com/tutorials/php/creating-a-file-upload-form-with-php/ and nobody else seems to be reporting the same error.

<?php

$target_path = "images/uploads/";

$username = $_POST['username'];

$email = $_POST['email'];

$password = $_POST['password'];

$quote = $_POST['quote'];

$filename = $_FILES['uploadedfile']['name'];

$allowed_filetypes = array('jpg','gif','bmp','png');

$date = date("Y-m-d");

$max_filesize = 55000;

// Checks all fields have been completed.

if (!trim($username) || !trim($email) || !trim($password) || !trim($quote)) {

header('Location: error.php?code=1');

exit();

}

else{

include("config.php");

$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Ah **** - MySQL error!');

mysql_select_db($dbname) or die ('Ah **** - MySQL error!');

// Checks username hasn't already been taken

$query = mysql_query("SELECT username FROM members WHERE username = '$username'");

$check = mysql_num_rows($query);

if ($check != 0) {

header('Location: error.php?code=2');

exit();

}

// Check if user wants default avatar, if so just add 'em to

// database and get rid of them...

if (!trim($filename)) {

$query = "INSERT INTO members (username,email,password,status,type,quo... VALUES ('$username','$email','$password','user'...

mysql_query($query);

mysql_close($connect);

setcookie("Username", $username, 0);

header('Location: index.php');

}

// Check the filetype isn't fucked...

$filename = strtolower($filename) ;

$exts = split("[/\\.]", $filename) ;

$n = count($exts)-1;

$exts = $exts[$n];

$username2 = $username.".";

$username2 = $username2.$ext;

if(!in_array($exts,$allowed_filetypes...

header('Location: error.php?code=8');

exit();

}

// It's all good so check file size...

$target_path = $target_path . $username2;

if(filesize($_FILES['uploadedfile']['... > $max_filesize){

header('Location: error.php?code=9');

exit();

}

}

?>

 Tags:

   Report

3 ANSWERS


  1. Move the file to its permanent location, then get its size.


  2. You should just deal with the error message by writing error catching code. See here: http://uk3.php.net/errorfunc

    If you catch the error you can determine what to do with it, if you want to suppress it from displaying or not.

  3. You don&#039;t need to use filesize().

    The $_FILES global array holds it for you automagically after the upload is completed.There&#039;s a &quot;size&quot; element in the array, along with &quot;name&quot;, &quot;tmp_name&quot; and others.

    Try this:

    if ($_FILES[&#039;uploadedfile&#039;][&#039;size&#039;] &gt; $max_filesize) {

       blah, blah

    See the link below.

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.