Results 1 to 13 of 13

Thread: validating file upload.

  1. #1
    Join Date
    Feb 2009

    validating file upload.

    Hi, I have a file upload function, but it's only permitted to upload max 2 mb.

    First of all, is it possible to change that anywhere?

    And second of all, i'm having some trouble validating it so it will come with an error if the file exceeds 2 MB.

    Hope someone can help, so far it's only validating if a file is beeing uploaded, but not sure how to validate the size.
    This is my javascript code thanks:

    <script language="javascript" type="text/javascript">
    function validateForm()
    var x=document.forms["profile"]["file_name"].value;
    if (x==null || x=="")
      alert("An image must be uploadet");
      return false;
    And this is my form
    HTML Code:
    <table width="450">
    <div id="form">
        <form method="post" action="../somefile.php" name="profile" enctype="multipart/form-data" onsubmit="return validateForm(this)">
      <tr>  <td width=20>
    	    <label for="Upload Image">Upload image</label>
    		<td width=20>
            <input id="file_name" name="file_name" type="file">
    <td width=50>
    		<div class="input-row"><label for="mail">E-mail: </label>
    		<td width=0>
    		<input id="mail" name="mail" type="text" /></div>
    	    <input name="upload" value="Upload" type="submit">
    </div><!-- end form -->

  2. #2
    Join Date
    Apr 2006
    The file size will be set in the .php script which you did not include.

    Additionally, it would be this script that generates an alert.
    Last edited by jackoconnell; 10-02-2012 at 03:03 PM.

  3. #3
    Join Date
    Feb 2009
    Oh ok sorry, I thought it would be in the same file, because the validation if there is a file being uploaded is in the javascript code that I posted.
    but here is my php page:

    PHP Code:

    include_once( 'class/class.upload.php' );
    $handle = new upload($filname);
    $handle->allowed = array('image/*');
    $handle->image_border          5;   // defining border width
    $handle->image_border_color    '#000'// defining border color
    $handle->image_watermark 'badge.png';  // watermark image src
    $handle->image_watermark_position 'BR'// watermark image position again "B" for bottom and so on.
    $handle->image_resize         true;   // making resize function to true
    $handle->image_x              160;    // making width to 160px
    $handle->image_y              160;    // making hight to 160px
                if (
    $handle->processed) {
    //$message = '<div class="center"><p>Border color #F58462 added successfully</p><img src="uploads/'.$handle->file_dst_name.'" alt="" /></div>';

                     //*** Uniqid Session ***//
    $strSid md5(uniqid(time()));
    $to $_POST['mail'];
    $from 'my@gmail.com'
    $subject 'the subject';
    $headers 'From: '.$from"\r\n" 'Reply-To: '.$from"\r\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
    $headers .= "This is a multi-part message in MIME format.\n";
    $headers .= "--".$strSid."\n";
    $headers .= "Content-type: text/html; charset=utf-8\n";
    $headers .= "Content-Transfer-Encoding: 7bit\n\n";
    $_FILES["file_name"]["name"] != "") {
    $strFilesName $_FILES["file_name"]["name"];
    $strContent chunk_split(base64_encode(file_get_contents('uploads/'.$strFilesName)));
    $headers .= "--".$strSid."\n";
    $headers .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";
    $headers .= "Content-Transfer-Encoding: base64\n";
    $headers .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
    $headers .= $strContent."\n\n";
    $flgSend = @mail($to$subject$message$headers); // @ = No Show Error //
    "<font color='black'>Mail send completed</font>";
    $tmpfile 'uploads/'.$strFilesName;
    // http://php.net/manual/en/function.file-exists.php
    if(file_exists($tmpfile)) {
    // http://php.net/manual/en/function.unlink.php
    "Cannot send mail.";

  4. #4
    Join Date
    Apr 2006

  5. #5
    Join Date
    Feb 2009
    Hmm ok, now I downloaded phuploader.php, and I've included it.
    And I've tried to work abit with it, but I can imagine getting a problem when I submit the form.

    Because in the program i've made before, I didn't keep the file saved in the folder and it would send the image uploaded as an attachment after it has made some changes to to image.

    But in phuploader.php, there is already an upload button that uploads the images to the upload folder.
    But how do I get the upload field to respond to my button and also send the file and do all the changes to the uploaded image.

    I hope you understand what I meen.


  6. #6
    Join Date
    Apr 2006
    Attachments. OK try one of these: http://www.html-form-guide.com/conta...ttachment.html

  7. #7
    Join Date
    Feb 2009
    Hey again,

    hmm i'm kind of lost with this one.
    Because the program that I have made, will let a user upload a picture and create a watermark on it and then send it to an email that the user writes.

    First it validates with javascript if there has even been a file uploaded otherwise a popup with come where it says that there was no file.

    Isn't there also a command so I can get the popup to write if the file exceeds 2mb?

    My Javascript that already works if there is no file in the field looks like this:
    function validateForm()
    var x=document.forms["profile"]["file_name"].value;
    if (x==null || x=="")
      alert("An image must be uploadet");
      return false;
    Is there also a way to check if the file exceeds the size as simple as this, so it will come in a javascript popup, like it does if there is no file?

    I hope you understand what I meen.

  8. #8
    Join Date
    Apr 2006
    What you have here is a conditional javascript that states that if there is no image file (= null or "" (empty)) give an alert.

    You could use a second instance of this script that says if the fill size ==2mg || greater than 2mb send another alert.

  9. #9
    Join Date
    Feb 2009
    ok thx, but how is it I make a second instance in an already created javascript function?

  10. #10
    Join Date
    Apr 2006
    You can have more than one javascript in your form.

    Copy the one you have, paste it into the head of your form, then edit.

  11. #11
    Join Date
    Feb 2009

    Do you know what is the command for file size.

    I mean, if (file_size == 2mb){
    alert("too large");
    return false;

    I assume this is not the correct way to write it right?
    What's the correct cammond for file_size?

    Thank you for your help
    I hope you understand my question.

  12. #12
    Join Date
    Apr 2006
    Sorry, I'm wrong, can't be easily done.

    The file size upload alowable will have to be set in the .php script.

    Sent this link before.

    The submit form is part of the php script.


    so you are on the server when submitting.


  13. #13
    Join Date
    Feb 2009
    Ok thank you for your help.
    But I just got it to work with an else sentence.

    So if the file is not uploadet to the uploads folder it will echo "the file is too large";

    But thank's for you help

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.12261 seconds
  • Memory Usage 3,007KB
  • Queries Executed 15 (?)
More Information
Template Usage (35):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (1)bbcode_html
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (13)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (13)postbit
  • (13)postbit_onlinestatus
  • (13)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates