简体   繁体   中英

insert image into Mysql database using ionic2

I have trying to upload image from my device camera and insert it into a folder on a server , how can i modify the code below in order to save the captured image into a database table .

 <?php header('Access-Control-Allow-Origin: *'); require_once("db_connect.php"); $target_path = "image/"; if(isset($_FILES['file'])) { $target_path = $target_path . basename( $_FILES['file']['name']); if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) { $image=basename( $_FILES["file"]["tmp_name"],".jpg"); // used to store the filename in a variable //storind the data in your database // $query= "INSERT INTO items VALUES ('{$image}')"; //mysqli_query($con,$query); echo "success"; } else { echo $target_path; echo "There was an error uploading the file, please try again!"; } } ?> 

Instead of saving the image to the database it may be better to only store the path of the image in your database. I did not test this code, but i think you can accomplish it like this:

// This is the folder name where the images are going to be uploaded.
$storeFolder = '/images';  

if (!empty($_FILES)) {
    $uploads_dir = __DIR__ . $storeFolder;

       $tmp_name = $_FILES["file"]["tmp_name"];
       $name = time() . $_FILES["file"]["tmp_name"];

       if (move_uploaded_file($tmp_name, "$uploads_dir/$name")) {
          //File is uploaded. Show some message if you want. 
       }else {
          //Handle error
       }  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM