Upload Files using PHP CURL
Recently I was working on one REST API, in which I was sending API request using cURL and to send parameters I was using POST method.
In one of the API request I wanted to send or upload a file through cURL, so I started working and got the solution for the same.
Below is the simple code by which we can upload file using cURL in PHP.
Just copy-paste the below given code in 3 different file and give names as given at top of each code.
uploadform.php: Form with file field
<html> <head> <title>File Upload Using PHP and cURL - php-guru.in</title> <style type="text/css"> body { font-family:Verdana, Geneva, sans-serif; font-size:12px; } </style> </head> <body> <font style="color: red;"> < ?php echo ucfirst($errmsg);?> </font><br> <br> <table border="1" cellspacing="0" cellpadding="3" style="border-collapse:collapse;" bordercolor="#CCCCCC"> <form action="uploadpost.php" method="post" name="frmUpload" enctype="multipart/form-data"> <tr> <td>Upload</td> <td align="center">:</td> <td><input name="file" type="file" id="file"/></td> </tr> <tr> <td> </td> <td align="center"> </td> <td><input name="btnUpload" type="submit" value="Upload" /></td> </tr> </form> </table> </body> </html>
uploadpost.php:
$errmsg = ''; if (isset($_POST['btnUpload'])) { $url = "URL_PATH of upload.php"; // e.g. http://localhost/myuploader/upload.php // request URL $filename = $_FILES['file']['name']; $filedata = $_FILES['file']['tmp_name']; $filesize = $_FILES['file']['size']; if ($filedata != '') { $headers = array("Content-Type:multipart/form-data"); // cURL headers for file uploading $postfields = array("filedata" => "@$filedata", "filename" => $filename); $ch = curl_init(); $options = array( CURLOPT_URL => $url, CURLOPT_HEADER => true, CURLOPT_POST => 1, CURLOPT_HTTPHEADER => $headers, CURLOPT_POSTFIELDS => $postfields, CURLOPT_INFILESIZE => $filesize, CURLOPT_RETURNTRANSFER => true ); // cURL options curl_setopt_array($ch, $options); curl_exec($ch); if(!curl_errno($ch)) { $info = curl_getinfo($ch); if ($info['http_code'] == 200) $errmsg = "File uploaded successfully"; } else { $errmsg = curl_error($ch); } curl_close($ch); } else { $errmsg = "Please select the file"; } }
upload.php:File upload code
$uploadpath = "images/"; $filedata = $_FILES['filedata']['tmp_name']; $filename = $_POST['filename']; if ($filedata != '' && $filename != '') copy($filedata,$uploadpath.$filename);
May this works for you!!!
Cheers !!!
You I have done really nice work.I would like to appreciate this.
Nice Work!!!
Thanks. It explain very easily to understand
thanks for your code, works great!
i got error
Notice: Undefined index: filedata in D:\xampp\htdocs\spl\upload.php on line 3
Notice: Undefined index: filename in D:\xampp\htdocs\spl\upload.php on line 4.
data is not sent to upload.PHP, so you are getting blank values and getting this error
Hello,
I copied and pasted the above code,
I have a question,
Is a new file which is same as the uploaded file generated in the destination folder ?
Also, data is not being sent to upload.php
Why ?
I am working in an Ubuntu environment.
yes, name will be same,
also does destination folder have proper writable permissions?
I am trying to upload a .csv file via a rest api.
I got the following errors while running your code.
First,
I am getting error of undefined variable $errmsg when i am running uploadform.php
“Undefined variable: errmsg in /var/www/html/rest1/uploadform.php on line 15”
Second,
I am getting the error of undefined index ‘photo’ in uploadpost.php
“Undefined index: photo in /var/www/html/rest1/uploadpost.php on line 9”
Third,
I am getting error of undefined variables $filename, $filedata in upload.php
Notice: Undefined index: filedata in /var/www/html/rest1/upload.php on line 4
Notice: Undefined index: filename in /var/www/html/rest1/upload.php on line 5
Fourth,
No new file with the same name as uploaded file is being created.
I am working with ubuntu environment.
I have given the $uploadpath as “/var/www/html/rest/”
This path contains all the three files, upload.php, uploadpost.php, uploadform.php
All the above files and folders where given all permissions using sudo chmod 777
I have even introduced sessions, but it is not working.
Please help me, I really need to get this working for my project.
please email me your code at pravin@php-guru.in
nice work
Hi, I try your code and got an error:
Notice: Undefined index: photo in C:xampphtdocsfile_uploaduploadpost.php on line 9
May you help me to fix this? Thanks.
The index should be “file”, my mistake it has written as “photo”, updated the post as well. Thanks
Not working with in PHP 5.5
Are you getting any error?
With recent PHP version this code is not working, not getting any certain error but it stays at uploadpost.php with a blank / empty page , can this code be modified so it can work with updated recent php versions ?
Yes, its developed in old version of PHP, you can try modifying in latest PHP version
Not working for me, there is no error