[Solved] How to upload image using angularjs php mysql

Introduction

This tutorial will provide a step-by-step guide on how to upload an image using AngularJS, PHP, and MySQL. We will cover the basics of setting up the environment, creating the necessary HTML and JavaScript code, and connecting to the database. We will also discuss how to store the image in the database and how to retrieve it for display. By the end of this tutorial, you will have a working image upload system.

Solution

//AngularJS

//Create a file input field in your HTML

//In your controller, add the following code
$scope.uploadImage = function(){
var fd = new FormData();
fd.append(‘image’, $scope.image);
$http.post(‘upload.php’, fd, {
transformRequest: angular.identity,
headers: {‘Content-Type’: undefined}
})
.success(function(response){
console.log(response);
})
.error(function(error){
console.log(error);
});
}

//PHP

//Create a file called upload.php
500000) {
echo “Sorry, your file is too large.”;
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != “jpg” && $imageFileType != “png” && $imageFileType != “jpeg”
&& $imageFileType != “gif” ) {
echo “Sorry, only JPG, JPEG, PNG & GIF files are allowed.”;
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo “Sorry, your file was not uploaded.”;
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES[“image”][“tmp_name”], $target_file)) {
echo “The file “. basename( $_FILES[“image”][“name”]). ” has been uploaded.”;
} else {
echo “Sorry, there was an error uploading your file.”;
}
}
?>

//MySQL

//Create a table in your database to store the image
CREATE TABLE images (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
image VARCHAR(255) NOT NULL
);

//Insert the image into the database
$sql = “INSERT INTO images (image) VALUES (‘$target_file’)”;
if ($conn->query($sql) === TRUE) {
echo “New record created successfully”;
} else {
echo “Error: ” . $sql . “
” . $conn->error;
}


Yes, You can upload any image or file using AngularJs follow the below steps.
Note here I am uploading the file to store in a folder and not to the database because it’s a bad idea to upload the file into the database instead of that store the file into folder and name of the file in the database.

Step 1: Create the simple view in HTML as below:

<form name="upload_file" id="upload_file" ng-submit="functionName();">    
    <input type="file" name="inputfieldname">
    <button type="submit">Upload</button>
</form>

Now in angular controller write the function as below:

$scope.functionName = function() {
 var formdata = new FormData();
 var filename = document.getElementById('upload_file').files[0];

 formdata.append('filename', filename);

    $http({
        method: 'post',
        url: 'action.php',//Put your php file url
        data: formdata,
        headers: {'Content-Type': undefined},
    }).then(function successCallback(result) {

        $scope.message = result.data;
        alert($scope.message);

    });
}

1

solved How to upload image using angularjs php mysql


Solved: How to Upload Image Using AngularJS, PHP, and MySQL

Uploading images using AngularJS, PHP, and MySQL can be a daunting task. Fortunately, there are a few simple steps that can help make the process easier. In this article, we’ll walk through the steps of uploading an image using AngularJS, PHP, and MySQL.

Step 1: Create the HTML Form

The first step is to create an HTML form that will allow the user to select an image file to upload. The form should include an input field of type “file” and a submit button. Here is an example of a basic HTML form:

<form action="upload.php" method="post" enctype="multipart/form-data">
  <input type="file" name="image">
  <input type="submit" value="Upload">
</form>

Step 2: Create the PHP Script

The next step is to create a PHP script that will handle the image upload. The script should check to make sure the file is an image, and then move the file to a permanent location on the server. Here is an example of a basic PHP script:

<?php
  // Check if the file is an image
  if (exif_imagetype($_FILES['image']['tmp_name']) != IMAGETYPE_JPEG) {
    echo "Error: File is not an image.";
    exit;
  }

  // Move the file to a permanent location
  move_uploaded_file($_FILES['image']['tmp_name'], "uploads/{$_FILES['image']['name']}");
?>

Step 3: Store the Image in the Database

The final step is to store the image in the database. This can be done using a simple SQL query. Here is an example of a basic SQL query:

INSERT INTO images (name, path) VALUES ('{$_FILES['image']['name']}', 'uploads/{$_FILES['image']['name']}');

And that’s it! With these three simple steps, you can easily upload an image using AngularJS, PHP, and MySQL.