php - Upload Image and insert in Database MySQl withAjax -
i m user jquery mobile , php , ajax upload image , insert in database,but have probleme in insert name of image in database,the result in database :"c:fakepathlighthouse.jpg",i insert name of image after upload it. mu code in php
<?php // need add server side validation , better error handling here $data = array(); if(isset($_get['files'])) { $error = false; $files = array(); £fichier=basename($file['name']; $uploaddir = 'photo/'; foreach($_files $file) {if(move_uploaded_file($file['tmp_name'], $uploaddir. $fichier))) { $files[] = $uploaddir .$file['name']; } else { $error = true; } } $data = ($error) ? array('error' => 'there error uploading files') : array('files' => $files); } else { $data = array('success' => 'form submitted', 'formdata' => $_post); } echo json_encode($data); ?> my code script:
$(function() { // variable store files var files; // add events $('input[type=file]').on('change', prepareupload); $('form').on('submit', uploadfiles); // grab files , set them our variable function prepareupload(event) { files = event.target.files; } // catch form submit , upload files function uploadfiles(event) { event.stoppropagation(); // stop stuff happening event.preventdefault(); // totally stop stuff happening // start loading spinner here // create formdata object , add files var data = new formdata(); $.each(files, function(key, value) { data.append(key, value); }); $.ajax({ url: 'submit.php?files', type: 'post', data: data, cache: false, datatype: 'json', processdata: false, // don't process files contenttype: false, // set content type false jquery tell server query string request success: function(data, textstatus, jqxhr) { if(typeof data.error === 'undefined') { // success call function process form submitform(event, data); } else { // handle errors here console.log('errors: ' + data.error); } }, error: function(jqxhr, textstatus, errorthrown) { // handle errors here console.log('errors: ' + textstatus); // stop loading spinner } }); } function submitform(event, data) { // create jquery object form $form = $(event.target); // serialize form data var formdata = $form.serialize(); // should sterilise file names $.each(data.files, function(key, value) { formdata = formdata + '&filenames[]=' + value; console.log('nom de l image:' + '&filenames[]=' + value); }); $.ajax({ url: 'submit.php', type: 'post', data: formdata, cache: false, datatype: 'json', success: function(data, textstatus, jqxhr) { if(typeof data.error === 'undefined') { // success call function process form console.log('success: ' + data.success); } else { // handle errors here console.log('errors: ' + data.error); } }, error: function(jqxhr, textstatus, errorthrown) { // handle errors here console.log('errors: ' + textstatus); }, complete: function() { // stop loading spinner } }); } }); my initial page(index.html)
function getlocation(){ if (navigator.geolocation) { navigator.geolocation.getcurrentposition(successfunction, errorfunction); } //get latitude , longitude; function successfunction(position) { var latt = position.coords.latitude; var lngg = position.coords.longitude; var nom=$('input[id=nom]').val(); var photo= $('input[id=file_upload]').val(); var ville= $('input[id=ville]').val(); var pays= $('input[id=pays]').val(); long.value="longitude: " + lngg; lat.value="latitude: " + latt; //var adr="sousse"; codelatlng(latt, lngg); // var adresse=codelatlng(latt, lngg); var sendajax = $.ajax({ type: "post", url: 'api.php?rquest=insertmosque', data: 'lat='+latt+'&lng='+lngg+'&nom='+nom+'&pays='+pays+'&ville='+ville+'&photo='+photo, success: handleresponse }); function handleresponse(){ $('#answer').get(0).innerhtml = sendajax.responsetext; //console.log(data); } } function errorfunction(){ alert("geocoder failed"); } } code api.php private function insertmosque() { if ($this->get_request_method() != "post") { $this->response('', 406); } $nom = $_post['nom']; $lat =($_post['lat']); $lng = $_post['lng']; $adr = $_post['adr']; $ville = $_post['ville']; $pays = $_post['pays']; $photo=$_post['photo']; //$photo = addslashes($file['name']); $query = mysql_query("insert mosque values('', '$nom', '$lng','$lat',' $photo','$adr','$ville','$pays')", $this->db); if ($query) { $data['success'] = 'insertion avec success'; } else { $data['errors'] = 'failed'; } $this->response($this->json($data), 200); }
you use explode function in api.php parse filepath getting post:
$nom = $_post['nom']; $lat =($_post['lat']); $lng = $_post['lng']; $adr = $_post['adr']; $ville = $_post['ville']; $pays = $_post['pays']; //for example, $_post['photo'] = "c:/my/fake/directory/testfile.jpg" $photopath = explode("/", $_post['photo']); //print_r($photopath) results in array ( [0] => c: [1] => [2] => fake [3] => directory [4] => testfile.jpg ) $photo = end($photopath); //gets last result in array (your filename) $query = mysql_query("insert mosque values('', '$nom', '$lng','$lat',' $photo','$adr','$ville','$pays')", $this->db); please note may not efficient method of getting filename path, , delimiter ("/" in example above) might need change based on how getting path. if have questions feel free ask.
Comments
Post a Comment