www.webdeveloper.com
Results 1 to 13 of 13

Thread: validating file upload.

  1. #1
    Join Date
    Feb 2009
    Posts
    47

    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:

    Code:
    <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;
      }
    }
    
    </script>
    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>
    		<td width=20>
            <input id="file_name" name="file_name" type="file">
    		</td>
    </tr>
    <tr>
    <td width=50>
    		<div class="input-row"><label for="mail">E-mail: </label>
    </td>
    		<td width=0>
    		<input id="mail" name="mail" type="text" /></div>
    </td>
    </tr>
    <tr>
    <td>
    	    <input name="upload" value="Upload" type="submit">
    		</td>
    </tr>
    </table>
    		
    		
        </form>
    </div><!-- end form -->

  2. #2
    Join Date
    Apr 2006
    Posts
    66
    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.
    Jack
    Last edited by jackoconnell; 10-02-2012 at 03:03 PM.

  3. #3
    Join Date
    Feb 2009
    Posts
    47
    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:

    <?php
        
    include_once( 'class/class.upload.php' );
        if(isset(
    $_POST['upload'])){
            
    $filname=$_FILES['file_name'];
            
    $handle = new upload($filname);
            
    $handle->allowed = array('image/*');
            if(
    $handle->uploaded){
                
    $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
                
    $handle->process('uploads/');
                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";
        if(
    $_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 //
       
    if($flgSend){
    echo 
    "<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
    unlink($tmpfile);
    }
    }
    else{
    echo 
    "Cannot send mail.";
    }
                }
            }
        }
    ?>

  4. #4
    Join Date
    Apr 2006
    Posts
    66

  5. #5
    Join Date
    Feb 2009
    Posts
    47
    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.

    Thanks

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

  7. #7
    Join Date
    Feb 2009
    Posts
    47
    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:
    Code:
    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
    Posts
    66
    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
    Posts
    47
    ok thx, but how is it I make a second instance in an already created javascript function?

  10. #10
    Join Date
    Apr 2006
    Posts
    66
    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
    Posts
    47
    thx,

    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
    Posts
    66
    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.

    http://www.html-form-guide.com/conta...ttachment.html

    so you are on the server when submitting.

    Jack

  13. #13
    Join Date
    Feb 2009
    Posts
    47
    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



Recent Articles