upload image to database using PHP class PDO, jquery, -
am having bug in script; trying upload image mysql , using php pdo, classes , jquery. sure script seeing image name. firstly getting error message of undefined variable 'img' not declared, fixed using isset(function), still no image inserted, , empty alert message. sorry say, new here. here code.
form.php // html inputs <form action="" method="post" enctype="multipart/form-data"> <input type="file" name ="myimage" id="image"> <input type="submit" id="imagesend" > </form> image.js // image script $("#imagesend").click(function(){ var postimg = $("#image").attr("name");// image name var datastring = 'img='+postimg; $.ajax({ type:"post", url:"postimage.php", data:datastring, success: function(html){ alert(html); }, }); }); postimage.php // script post image //class script included here if(isset($_files['img'])){ $image = $_files['img']; $class = new scriptimage(); $class->sendimage($image); } class script.php // class script class scriptimage { public $dns = "mysql:host=localhost;dbname=inspirationaldb"; public $root ="root"; public $dbpass =""; public $conn; //connection string public function connection(){ $this->conn = new pdo($this->dns, $this->root, $this->dbpass); $this->conn->setattribute( pdo::attr_errmode, pdo::errmode_exception ); } public function sendimg($uploadname){ $this->connection(); $image_name = $uploadname['name']; $image_size = $uploadname['size']; $image_type = $uploadname['type']; $image_tmp = $uploadname['tmp_name']; if($image_size > 1048576){// 1 mb echo "large image"; return false; } $path ="post-images/"; $image_exe = $this->getimgextntion($image_type); $imagefolderpath = $path.$image_name.$image_exe; $imagedbpath = $imagefolderpath; if(move_uploaded_file($image_tmp, $imagefolderpath)){ $save=$this->conn->prepare("insert images(image)values(:upload)"); $done=$save->execute(array(":upload" =>$imagedbpath)); if($done){ echo "image saved"; } else{ echo "image not saved"; } } }
i , nothing happens, no feedback or image in database.
Comments
Post a Comment