Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
I write a php code for ajax image upload
when I don`t use class it works true,
but when I use it in class $_POST is true but $_File is empty:

client side(HTML Jquery):
<pre lang="HTML">
<div style="height:150px;width:97%;padding:10px 8px;">
               <form action="upload" method="POST" id="uploadform">
                  <input type="file" name="file"/>
                  <input type="submit" value="Upload"/>
                  <div id="loader" style="display:none;">
                   <center><img src="load.gif" /></center>
                  </div>
                  <div>
                   جزئیات :
                   <div id="onsuccessmsg" style="padding:15px;"></div>
                  </div>
               </form>
            </div>
                <script>
                $(document).ready(function(){
                 function onsuccess(response,status){
                  $("#loader").hide();
                  $("#onsuccessmsg").html("Status :<b>"+status+'</b><br><br>Response Data :<div id="msg" style="border:5px solid #CCC;padding:15px;">'+response+'</div>');
                 }
                  $("#uploadform").on('submit',function(){
                  $("#loader").show();
                  var options={
                   url     : $(this).attr("action"),
                   success : onsuccess
                  };
                  $(this).ajaxSubmit(options);
                 return false;
                 });
                });
            </script>


PHP code without class:

PHP
<?php
function getExtension($str) {$i=strrpos($str,".");if(!$i){return"";}$l=strlen($str)-$i;$ext=substr($str,$i+1,$l);return $ext;}
$formats = array("jpg", "png", "gif", "bmp", "jpeg", "PNG", "JPG", "JPEG", "GIF", "BMP");

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
 $name = $_FILES['file']['name'];
 $size = $_FILES['file']['size'];
 $tmp  = $_FILES['file']['tmp_name'];
 if(strlen($name)){
  $ext = getExtension($name);
  if(in_array($ext,$formats)){
   if($size<(1024*1024)){
    $imgn = time().".".$ext;
    if(move_uploaded_file($tmp, "folder address".$imgn)){
     echo "<br/>File Location : folder address".$imgn;
    }else{
     echo "Uploading Failed.";
    }
   }else{
    echo "Image File Size Max 1 MB";
   }
  }else{
   echo "Invalid Image file format.";
  }
 }else{
  echo "Please select an image.";
  exit;
 }
}
?>



PHP code with class:

PHP
<?php
class Upload
{
    function index()
    {
        #print_r($_POST);exit;
        $formats = array("jpg", "png", "gif", "bmp", "jpeg", "PNG", "jpg", "JPEG", "GIF", "BMP","pdf");

        if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
        {
            $name = $_FILES['file']['name'];
            $size = $_FILES['file']['size'];
            $tmp  = $_FILES['file']['tmp_name'];
            if(strlen($name)){
                $i=strrpos($name,".");
                if(!$i)
                {
                    $ext="";
                }
                $l=strlen($name)-$i;
                $ext=substr($name,$i+1,$l);
                if(in_array($ext,$formats)){
                    if($size<(1024*1024)){
                        $imgn = time().".".$ext;
                            if(move_uploaded_file($tmp, "folder address".$imgn)){
                                echo "<br/>File Location :folder address".$imgn;
                            }
                            else
                            {
                                echo "Uploading Failed.";
                            }
                    }
                    else
                    {
                        echo "Image File Size Max 1 MB";
                    }
                }
                else
                {
                    echo "Invalid Image file format.";
                }
            }
            else
            {
                echo "Please select an image.";
                exit;
            }
        }
    }
}
?>
Posted
Comments
pandu web dev 13-Aug-14 4:39am    
Which line you are getting error
Karim Pazoki 13-Aug-14 4:58am    
in PHP code without class everything is true
but in PHP code with class I comment "#print_r($_POST);exit;"
$_POST passed true but $_FILES is empty.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900