SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to send file attribute to php using javascript or jquery

    i'm struggling from few days that how to send file attribute using javascript or jquery on searching i realize and heard that javascript or jquery does not send file attribute is there any other way to send file attribute to php file? any kind of help will be appreciated Thanks.

  2. #2
    Non-Member
    Join Date
    Feb 2012
    Posts
    892
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    You can get the size of a file, for example.
    IE9- uses an ActiveX which is an unsafe procedure. This means the user has to allow the operation.
    IE10+ and newer browsers can access HTML5 new properties, such as files and size: f.files[0].size.

    HTML Code:
    <!doctype html>
    <html>
    <body>
    <form action="uploadFile" method="post" enctype="multipart/form-data" onsubmit="return getAttr(this.file)">
    <input type="file" name="file">
    <input type="submit" value="File Size">
    </form>
    <script>
    // http://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx
    function getInternetExplorerVersion()
    // Returns the version of Internet Explorer or a -1
    // (indicating the use of another browser).
    {
      var rv = -1; // Return value assumes failure.
      if (navigator.appName == 'Microsoft Internet Explorer')
      {
        var ua = navigator.userAgent;
        var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
          rv = parseFloat( RegExp.$1 );
      }
      return rv;
    }
    
    function getAttr(f)
    {
       var ver = getInternetExplorerVersion();
       if(ver > 0 && ver <= 9.0)
       {
          if(f.value)
          {
             var oas=new ActiveXObject("Scripting.FileSystemObject");
             var e=oas.getFile(f.value);
             var size=e.size;
          }
       }
       else
       {
          if(f.files[0]!=undefined)
          {
             size = f.files[0].size;
          }
       }
       alert(size);
       return false;
    }
    </script>
    </body>
    </html>

  3. #3
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can u tell me how i send/pass the file attribute to php file here is my code below

    index.php
    Code:
    <html>
    <head>
    <title>
    Member Area
    </title>
     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
    $(document).ready(function() {
        $("#register").click( function() {
            $.post( $("#form").attr("action"), $("#form :input").serializeArray(), function(info) { $("#results").empty(); $("#results").html(info)} );
        });
        $("#form").submit(function(){
            return false;
        });
    
    });
    </script>
    </head>
    
    <body>
    <form name="form" id="form" enctype="multipart/form-data" action="php_file.php" method="POST">
    <br /><input type="file" name="file" id="file"/>
    <button name="submit" id="register">Register</button>
    </form>
    
    <div id="results"></div>
    
    </body>
    </html>
    php_file.php
    Code:
    <?php
    error_reporting( E_ALL ^ E_NOTICE);
    session_start();
        include_once("connect.php");
            $file = $_FILES['file']['name'];
            $tmp_file = $_FILES['file']['tmp_name'];
            if($file)
            {
                $location = "avatars/$file";
                move_uploaded_file($tmp_file,$location);
                $query = "UPDATE users SET image_location ='".$location."' WHERE username ='".$_SESSION['username']."'";
                $result = mysql_query($query) or die (mysql_error());
            }
            else
            {
                $errors = "<div id='errors'>You must choose image with jpg , jpeg , png , gif this field cannot empty.</div>";
            }
    if(isset($errors)) { echo $errors; }
    ?>

  4. #4
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i wan't to insert file name into my database and move uploaded file without refreshing page below is my code what i exactly need

    index.php
    Code:
    <html>
    <head>
    <title>
    Member Area
    </title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
    $(document).ready(function() {
        $("#register").click( function() {
            $.post( $("#form").attr("action"), $("#form :input").serializeArray(), function(info) { $("#results").empty(); $("#results").html(info)} );
        });
        $("#form").submit(function(){
            return false;
        });
    
    });
    </script>
    </head>
    
    <body>
    <form name="form" id="form" enctype="multipart/form-data" action="php_file.php" method="POST">
    <br /><input type="file" name="file" id="file"/>
    <button name="submit" id="register">Register</button>
    </form>
    
    <div id="results"></div>
    
    </body>
    </html>
    php_file.php
    Code:
    <?php
    error_reporting( E_ALL ^ E_NOTICE);
    session_start();
    $_SESSION['username'] = "user1";
        include_once("connect.php");
            $file = $_FILES['file']['name'];
            $tmp_file = $_FILES['file']['tmp_name'];
            if($file)
            {
                $location = "avatars/$file";
                move_uploaded_file($tmp_file,$location);
                $query = "UPDATE users SET image_location ='".$location."' WHERE username ='".$_SESSION['username']."'";
                $result = mysql_query($query) or die (mysql_error());
            }
            else
            {
                $errors = "<div id='errors'>You must choose image with jpg , jpeg , png , gif this field cannot empty.</div>";
            }
    if(isset($errors)) { echo $errors; }
    ?>


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
  •