header("Content-Type: application/json") And additionally, include the Authorization property in this header because users can easily access authentication. We've only allowed them to use the three headers above in this header. Header("Acess-Control-Allow-Headers: Acess-Control-Allow-Headers, Content-Type, Acess-Control-Allow-Methods, Authorization") – This header uses encryption because other people want to use their header to access the page Header("Acess-Control-Allow-Methods: POST") – It indicates that only POST requests are allowed. Header("Acess-Control-Allow-Origin: *") – This indicates that the request can be accepted from any place. Header("Acess-Control-Allow-Origin: *") – This indicates that the request is for JSON data or format. The following four headers have been added: $query = mysqli_query($conn,'INSERT into tbl_image (name) VALUES("'.$fileName.'")') Įcho json_encode(array("message" => "Image Uploaded Successfully", "status" => true)) $errorMSG = json_encode(array("message" => "Sorry, only JPG, JPEG, PNG & GIF files are allowed", "status" => false)) $errorMSG = json_encode(array("message" => "Sorry, file already exists check upload folder", "status" => false)) If($fileSize "Sorry, your file is too large, please upload 5 MB size", "status" => false)) check file not exist our upload folder path If(in_array($fileExt, $valid_extensions)) $valid_extensions = array('jpeg', 'jpg', 'png', 'gif') $fileExt = strtolower(pathinfo($fileName,PATHINFO_EXTENSION)) // get image extension $upload_path = 'upload/' // set upload folder path "please select image", "status" => false)) This code will upload a file and insert its information in the database, including individual file or image validation and a reasonable message viewed in postman tool JSON format if an error is found. Only with MySQLi extension, this file carries easy database setup code. ) ENGINE=InnoDB DEFAULT CHARSET=latin1 dbconfig.php I've used "file_upload_restapi_db" in this case.Īfter you've created your database, copy the following SQL code and paste it into the available header section of your SQL option to create a table. Create Database and Tableįirst, in phpMyAdmin, build a new database with any name you like. Note: Before that, build a new folder called upload in your server's root directory, where our files will be saved after being uploaded using the Postman tool via the Rest API. Project Set UpĪs you can see, I've built a PHP-Rest-API-file-Upload project root directory structure under the XAMPPserver's htdocs folder. Let's take a look at the PHP Rest API file upload code. it's covered file upload via Rest API with validation, such as checking whether the file exists or not and requiring only valid file extensions to upload. This tutorial answered all of your questions. When it comes to sending files to a server, however, it can be a time-consuming process. When dealing with web services, uploading files may not be one of the most frequent tasks. I've already written a PHP Rest API CRUD tutorial, but this one doesn't cover file upload to the server through Rest API.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |