attachment size validation

the mail() function

Moderators: macek, egami, gesf

hamwisk2012
New php-forum User
New php-forum User
Posts: 4
Joined: Sat Apr 28, 2012 6:58 pm

attachment size validation

Postby hamwisk2012 » Sat Apr 28, 2012 7:34 pm

Hello, I have been trying to learn PHP and I'm starting to understastand most of it now. I made a simple email page that has an upload folder, for the attachments. It all works except, when i try to upload a file larger than the upload limit, although it will not move the uploaded file it does return the wrong error message. It doesnt seem to mater how i write the if statments it never works. please someone help me.

attach.php

Code: Select all

<head><!-- set style and make upload form -->
<style type='text/css'>
a{text-decoration:none}
#msg{height:30px; float:right; background-color:yellow; box-shadow: 10px 10px 5px #888888; opacity:0.6;}</style>
<script src='http://code.jquery.com/jquery-latest.js'></script>
<script>$('#msg').fadeOut(5000);</script></head>
Choose a file to attach:<br />
<form enctype='multipart/form-data' action='upload.php' method='post'>
<input name='uploaded_file' type='file' size='3' /><br />
<input type='submit' value='Upload' />
</form><hr />
<?php
// open upload directory and get each entry
$upDirectory = opendir("/***/***/website/test/email/upload");
while($entryName = readdir($upDirectory)) {
   $dirArray[] = $entryName;
closedir($upDirectory);
// count
$indexCount = count($dirArray);
$num = $indexCount-2;
if ($num == '1'){
   $plu = '';}
else {$plural = 's'};
sort($dirArray);
// make upload contents table
print   ("<table border='1' cellpadding='2' cellspacing='0'>\n");
print   ("<tr><th>$num File$plural <a href='del.php'><button>Delete</button></a></th></tr>\n");
// loop through the array of files and print them all
for   ($index=0; $index < $indexCount; $index++) {
        if (substr("$dirArray[$index]", 0, 1) != "."){
print   ("<tr><td><a href='upload/$dirArray[$index]' target='_blank'>$dirArray[$index]</a></td></tr>\n");
   }
}
print   ("</table>\n");
?>


So then if you then pressed 'upload' the form will submit the data. It sends the file to a temporary directory and from there I want to check a, is it there (in the tmp dir)? b, is it too big? c, does the filename already exist? If all these conditons are satisfied then move the file to the 'upload' folder. If any condition is not met, it should print the error, then run the attach.php again.

upload.php

Code: Select all

<?php
$filename = basename($_FILES['uploaded_file']['name']);
//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  //Check if the file's size is 2000Kb or less
  if ($_FILES["uploaded_file"]["size"] <= 2000000) {
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/upload/'.$filename;
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
           echo "<div id='msg'><p>Upload success!</p></div>";
include("attach.php");
        }
      } else {
         echo "<div id='msg'><p>File already exists!</p></div>";
include("attach.php");
      }
  } else {
     echo "<div id='msg'><p>File over 2Mb!</p></div>";
include("attach.php");
  }
} else {
 echo "<div id='msg'><p>No file uploaded!</p></div>";
include("attach.php");
}
?>


If I try to upload a file thats too big it doesn't move it from the tmp folder, but it returns the error message "No file uploaded!" instead of the 2Mb message :-x

I thought maybe I messed up the else if nesting, I re-wrote them over and over, didn't help.

This is my first post, so I don't know the rules yet (sorry) can I post a link to the offending php?

Thanking you in advance, Kim

User avatar
minimihi
New php-forum User
New php-forum User
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: attachment size validation

Postby minimihi » Sat Apr 28, 2012 7:45 pm

What is value of $_FILES['uploaded_file']['error']?

hamwisk2012
New php-forum User
New php-forum User
Posts: 4
Joined: Sat Apr 28, 2012 6:58 pm

Re: attachment size validation

Postby hamwisk2012 » Sat Apr 28, 2012 7:50 pm

I changed 'upload.php' to:

Code: Select all

<?php
$val = ($_FILES['uploaded_file']['error']);
echo "$val";
?>


and the result was '1'

User avatar
minimihi
New php-forum User
New php-forum User
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: attachment size validation

Postby minimihi » Sat Apr 28, 2012 7:51 pm

"Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini."

Reference:
Handling file uploads: Error Messages Explained

hamwisk2012
New php-forum User
New php-forum User
Posts: 4
Joined: Sat Apr 28, 2012 6:58 pm

Re: attachment size validation

Postby hamwisk2012 » Sat Apr 28, 2012 8:02 pm

sweeeeeeeeet, so I just did this:

Code: Select all

<?php
$filename = basename($_FILES['uploaded_file']['name']);

if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
       
   if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
           echo "<div id='msg'><p>Upload success!</p></div>";
      include("attach.php");}

} else {
echo "<div id='msg'><p>Error!</p></div>";
include("attach.php");}
?>


The error message returned. Confusion over now, so if I move the size validation before the upload error like:

Code: Select all

<?php
$filename = basename($_FILES['uploaded_file']['name']);
 //Check if the file's size is 2000Kb or less
if ($_FILES["uploaded_file"]["size"] <= 2000000) {
  //Сheck that we have a file
  if ((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
      //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/upload/'.$filename;
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
           echo "<div id='msg'><p>Upload success!</p></div>";
include("attach.php");
        }
      } else {
         echo "<div id='msg'><p>File already exists!</p></div>";
include("attach.php");
      }
  } else {
     echo "<div id='msg'><p>No file uploaded!</p></div>";
include("attach.php");
  }
} else {
 echo "<div id='msg'><p>File over 2Mb!</p></div>";
include("attach.php");
}
?>

It should work...

User avatar
minimihi
New php-forum User
New php-forum User
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: attachment size validation

Postby minimihi » Sat Apr 28, 2012 8:14 pm

No, your problem is PHP configuration (php.ini file).
In that file you need to change 'upload_max_filesize' value to a greater one.
By default it is set to '2M' (which is 2MB), you need to increase it

hamwisk2012
New php-forum User
New php-forum User
Posts: 4
Joined: Sat Apr 28, 2012 6:58 pm

Re: attachment size validation

Postby hamwisk2012 » Sat Apr 28, 2012 8:26 pm

I want the upload limit to be 2Mb, It's just that the way the upload.php was made was returning the eror before its time. I need the script to check the size first, but I don't think thats the best way.
so if I discounted the error code '1' then the correct message will display?
something like?

Code: Select all

$filename = basename($_FILES['uploaded_file']['name']);
$servEr = ($_FILES['uploaded_file']['error']);
if ($servEr = 0||1){
echo "<div id='msg'><p>Upload error!</p></div>";
include("attach.php");}
else {
//Сheck that we have a file
if(!empty($_FILES["uploaded_file"])) {.........


It might just be easier to change the settings though, actually, thats got to be the best solution.

!!Solved!!


Return to “PHP coding => Mail”

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest