The majority of websites require users to upload files (such as images, documents, etc.) to web pages. Whether you’re creating a platform for file sharing or just need to upload files to a server.
If you are on the same server and upload file then you can easily upload it either directly using PHP or AJAX but if you want to upload it to another server then you have to use cURL.
This tutorial will guide you through the process of using PHP and cURL to upload files, making the web development process faster and more efficient. With just a few simple steps, you can easily upload files to the server.
Contents
1. Create Api to upload file
Create api.php
file.
If $_FILES['file']['name']
is isset()
then upload the file to upload
folder.
Assign a value to the $status
and $msg
variables, depending on whether the upload was successfully uploaded or not. Read extra data num1
passed using $_POST
.
Finally, return the $response
array in JSON format for further processing.
<?php $status = 0; $msg = ""; if(isset($_FILES['file']['name'])){ // file name $filename = $_FILES['file']['name']; // Location $location = 'upload/'.$filename; // file extension $file_extension = pathinfo($location, PATHINFO_EXTENSION); $file_extension = strtolower($file_extension); // Valid extensions $valid_ext = array("pdf","doc","docx","jpg","png","jpeg"); $status = 0; if(in_array($file_extension,$valid_ext)){ // Upload file if(move_uploaded_file($_FILES['file']['tmp_name'],$location)){ $status = 1; $msg = "Upload successfully"; } }else{ $status = 0; $msg = "Invalid file extension"; } } $response = array( 'status' => $status, 'msg' => $msg, 'num1' => $_POST['num1'] ); echo json_encode($response); die;
2. File upload using cURL
In the example, I am handling <form > submit on the same file.
-
Create a form to upload file
First, we need to create an HTML form with an input field of type “file”. This will allow users to select the file they want to upload.
<form method="post" action="" enctype="multipart/form-data"> <input type="file" name="file"> <br> <input type="submit" name="submit" value="Upload file"> </form>
-
Write the PHP code to upload the file using cURL
If submit button is POST then upload the file using cURL.
Create CURLFile
object where pass $_FILES['file']['tmp_name']
,$_FILES['file']['type']
, and $_FILES['file']['name']
as parameters.
Store $cfile
in $postRequest['file']
Array. Here, also storing value in $postRequest['num1']
for example purpose.
Decode the cURL response to read value.
Full Code with HTML <form >
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>How to upload file using cURL in PHP</title> </head> <body> <?php if(isset($_POST['submit'])){ if(isset($_FILES['file']['name'])){ // Create a CURLFile object $cfile = curl_file_create($_FILES['file']['tmp_name'],$_FILES['file']['type'],$_FILES['file']['name']); $postRequest = array( 'file' => $cfile, 'num1' => 54 ); $cURL = curl_init('http://localhost/apis/api.php'); curl_setopt($cURL, CURLOPT_POSTFIELDS, $postRequest); curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true); if(curl_exec($cURL) === false){ echo 'Curl error: ' . curl_error($cURL); }else{ $curlResponse = curl_exec($cURL); $jsonArrayResponse = json_decode($curlResponse); echo "<pre>"; print_r($jsonArrayResponse); echo "</pre>"; } curl_close($cURL); } } ?> <form method="post" action="" enctype="multipart/form-data"> <input type="file" name="file"> <br> <input type="submit" name="submit" value="Upload file"> </form> </body> </html>
3. Output
4. Conclusion
A robust and adaptable method of transferring data between servers is to upload files using PHP and cURL.
You can easily upload files using cURL in your PHP applications by following the instructions provided in this article, you can also pass extra parameters while sending a file for upload.
If you found this tutorial helpful then don't forget to share.