3

I am trying to develop a PHP based application to upload a file to the web server

the jquery code goes like this

<form action="upload.php" method="post" enctype="multipart/form-data" name="upload">
            <input name="uploaded_file" type="file" 
                   style="border-radius:0px; width:900px; font-family:'Lucida Sans Unicode', 'Lucida Grande', sans-serif" required/>
            <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
            <input type="hidden" name="id" value=<?php echo $getid; ?> />

            <br/>
            <br/>
            <input name="Upload" type="submit" value="Upload" class="btn btn-success btn-large" style="border-radius:0px;" id="uploadfile"/>

        </form>

           <div class="progress" id="progress"> 
               <div class="bar"></div >  
               <div class="percent">0%</div >  
           </div>  
           <div id="status"></div>
       <script src="jquery.js"></script>  
       <script src="jquery.form.js"></script> 
       <script>
            $(document).ready(function(){
                $('#progress').hide();

                $('#uploadfile').click(function(){
                    $('#progress').show();
                  });
            });
        </script> 
        <script>
            (function() {

            var bar = $('.bar');
            var percent = $('.percent');
            var status = $('#status');

            $('form').ajaxForm({
                beforeSend: function() {
                    status.empty();
                    var percentVal = '0%';
                    bar.width(percentVal)
                    percent.html(percentVal);
                },
                uploadProgress: function(event, position, total, percentComplete) {
                    var percentVal = percentComplete + '%';
                    bar.width(percentVal)
                    percent.html(percentVal);
                },
                success: function() {
                    var percentVal = '100%';
                    bar.width(percentVal)
                    percent.html(percentVal);
                },
                complete: function(xhr) {
                    status.html('Uploaded sucessfull');
                }
            }); 

            })();       
        </script> 

and the php code goes like this

<?php

    include('connect.php'); 
    include('antivirus.php');
    $virusstatus='FALSE';
    function clean($str) 
    {
        $str = @trim($str);
        if(get_magic_quotes_gpc()) 
        {
            $str = stripslashes($str);
        }
        return mysql_real_escape_string($str);
    }

    //Sanitize the POST values
    $filedesc= 'Enter Description';
    $fname=  mt_rand(1000,9999);
    $id=clean($_POST['id']);

    //upload random name/number
    $rd2 = mt_rand(1000,9999)."_File"; 

    //Check that we have a file
    if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 350Kb
  $filename = basename($_FILES['uploaded_file']['name']);

  $ext = substr($filename, strrpos($filename, '.') + 1);

  if (($ext != "exe") && ($_FILES["uploaded_file"]["type"] != "application/x-msdownload"))  {
    //Determine the path to which we want to save this file      
      //$newname = dirname(__FILE__).'/upload/'.$filename;
      $newname="uploads/".$rd2."_".$filename;      


        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) 
        {

            $vsig=scanfile($newname);
            $query=mysql_query("select * from virussig")or die(mysql_error());
            while($image=mysql_fetch_array($query))        
            {
                if($vsig==$image['vsig'])
                {
                    $virusstatus='TRUE';
                }
            }
            if($virusstatus=='TRUE')
            {
                die("This file contains some Virus signatures which is not allowed");
            }
            else
            {
                mysql_query("INSERT INTO up_files (id,fdesc,floc,fdatein,fname) VALUES ('$id','$filedesc','$newname',NOW(),'$fname')"); 
                header("location: iconview.php?id=".$id);           
            }
        }
        } 

    } 

?>

my problem is whenever error occurs in php during file upload,it gives file upload sucessful in jquery. I want the solution to somhow manage the errors got in php and display in jquery please help

Thankyou

8
  • WHY ALL CAPS in title? And what is your error? Also, mysql_* functions are deprecated, use MySQLi / PDO instead. Commented May 8, 2014 at 4:25
  • 1
    sidenote: what will be the content of antivirus.php contain? Are you calling Anti-virus software to scan upload file before storing it? Commented May 8, 2014 at 4:26
  • its a simple function that gets the file data and converts it into hash... Commented May 8, 2014 at 4:28
  • 1
    It seems there is error handler for ajaxForm plugin . try throwing some appropriate messages in else part of if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) whenever upload fails Commented May 8, 2014 at 4:28
  • code goes like this :<?php function scanfile($filename) { $file =$filename; return md5_file($file); } ?> Commented May 8, 2014 at 4:28

1 Answer 1

0

Have something like this in your php code

    if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname)))
    {
         $result = true;
         return json_encode($result);
    }
    else
    {
         $result = false;
         return json_encode($result);
    }

Then in your ajax success call back

    complete: function(xhr,data) {
                if(data === true)
                status.html('Uploaded sucessfull');
                else 
                status.html('Uploaded unsuccessful');
            }

Do check the syntax and json returned as well. Have written this logically.

You can also do it without json_encode. and appropriate return type in ajax call

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.