SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict Phil-man's Avatar
    Join Date
    Nov 2000
    Posts
    291
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi. I'm trying to use PHP to limit the size of files that are uploaded with a particular form. I don't want to mess with the upload_max_filesize setting in php.ini because I just want the limit to be specific to this particular page/form. I've tried including a hidden input field with the form, called "MAX_FILE_SIZE" and with the value of "1024", but I'm still able to upload very large files when I test it. Am I doing this wrong? Or is there something on the PHP side that needs to be turned on to allow passing MAX_FILE_SIZE from the form?

    Thanks!

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure about the MAX_FIEL_SIZE in the form but you can do this

    As you may or may not know when you upload a file you automatrically have access to three vars regarding the uploaded file. Let's say you have a file field named userfile
    <input type="file" name="userfile">

    After submission you have
    $userfile_name
    $userfile_type
    $userfile_size

    So here you can say
    if ($userfile_size > 1024) {
    print "Your file is too large";
    }
    else {
    copy($userfile, "/path/to/destination/".$userfile_name);
    }
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The hidden MAX_FILE_SIZE field should work as advertised. Here's a simple example from the pages of "Professional PHP Programming":

    Code:
    <HTML>
      <FORM ACTION=upload.php METHOD=POST ENCTYPE="multipart/form-data">
        <INPUT TYPE=HIDDEN NAME=MAX_FILE_SIZE VALUE=1024>
        Sumbit theis file: <INPUT TYPE=FILE NAME=userfile><BR>
        <INPUT TYPE=SUBMIT><BR>
      </FORM>
    </HTML>
    Note that the MAX_FILE_SIZE field must precede the FILE <INPUT> tag. Are you perhaps placing yours after it?
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •