PHP GuruPHP, MySQL, JavaScript, AJAX, MVC, Codeigniter
  • Technology
    • PHP
    • PHP Frameworks
    • MySql
    • Javascript
    • AJAX
    • React Js
    • Node JS
    • ChatGPT
  • JSON Viewer
  • XML Viewer

Upload Files using PHP CURL

Date October 19, 2013 Author By PravinS Category PHP

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>&nbsp;</td>
      <td align="center">&nbsp;</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 !!!

Tags: file upload using curl, php curl file upload, php file upload using curl

18 thoughts on “Upload Files using PHP CURL”

  • Randhir Singh says:
    July 22, 2014 at 6:29 am

    You I have done really nice work.I would like to appreciate this.

    Reply
  • vijaykanth says:
    October 8, 2014 at 3:20 pm

    Nice Work!!!

    Reply
  • phponwebsites says:
    October 30, 2014 at 7:21 am

    Thanks. It explain very easily to understand

    Reply
  • Claudio says:
    November 7, 2014 at 3:33 pm

    thanks for your code, works great!

    Reply
  • thennarasu says:
    November 27, 2014 at 7:05 am

    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.

    Reply
    • PravinS says:
      November 27, 2014 at 11:19 am

      data is not sent to upload.PHP, so you are getting blank values and getting this error

      Reply
      • Leora says:
        August 10, 2015 at 9:00 pm

        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.

        Reply
        • PravinS says:
          August 11, 2015 at 4:53 am

          yes, name will be same,
          also does destination folder have proper writable permissions?

          Reply
          • Leora says:
            August 11, 2015 at 6:54 am

            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.

          • PravinS says:
            August 12, 2015 at 4:56 am

            please email me your code at pravin@php-guru.in

  • arjun says:
    June 22, 2015 at 5:26 pm

    nice work

    Reply
  • V Code (codewhenfree) says:
    January 2, 2018 at 8:05 am

    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.

    Reply
    • PravinS says:
      January 4, 2018 at 5:48 am

      The index should be “file”, my mistake it has written as “photo”, updated the post as well. Thanks

      Reply
  • Raj says:
    March 4, 2019 at 11:00 am

    Not working with in PHP 5.5

    Reply
    • PravinS says:
      March 5, 2019 at 6:08 am

      Are you getting any error?

      Reply
  • Pratik Acharya says:
    July 18, 2019 at 6:47 am

    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 ?

    Reply
    • PravinS says:
      July 18, 2019 at 11:31 am

      Yes, its developed in old version of PHP, you can try modifying in latest PHP version

      Reply
  • umar khan says:
    July 15, 2020 at 12:26 pm

    Not working for me, there is no error

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Comparison between CodeIgniter and Laravel
  • SOAP Client In CodeIgniter using NuSOAP PHP Toolkit
  • Pagination using PHP and MySQL – 1
  • Pagination using PHP and MySQL – 2
  • Upload Files using PHP CURL
  • Search Value in Multidimensional Array Using PHP
  • SOAP Server In CodeIgniter using NuSOAP PHP Toolkit
  • Simple JavaScript/CSS Lightbox
  • Import CSV data to MySQL Using PHP
  • Export MySQL data to CSV file Using PHP

Pages

  • ChatGPT
  • Node JS
  • PHP
  • React Js

Tag Cloud

A-Z alphabets in php AJAX alphabets using php array search in php Asynchronous JavaScript and XML CI NUSOAP library Client Side codeigniter codeigniter nusoap library coparison chart Database file upload using curl html to pdf import csv to mysql Javascript javascript css lightbox laravel lightbox MySQL nusoap client in codeigniter nusoap in codeigniter nusoap integration with codeigniter Open Source pagination in php and mysql pagination in php mysql pasination using php pdf pdf generation in codeigniter PHP php and mysql pagination php curl file upload php file upload using curl php mysql pagination php pagination php pagination function RDBMS Scripting Language search in multidimensional array Server Side language soap client in codeigniter soap in codeigniter soap server in codeigniter soap server using nusoap in codeigniter SQL TCPDF with CodeIgniter

Categories

  • AJAX
  • Javascript
  • MySql
  • PHP
    • PHP Frameworks

Recent Posts

  • Comparison between CodeIgniter and Laravel
  • SOAP Client In CodeIgniter using NuSOAP PHP Toolkit
  • Pagination using PHP and MySQL – 1
  • Pagination using PHP and MySQL – 2
  • Upload Files using PHP CURL
  • Search Value in Multidimensional Array Using PHP
  • SOAP Server In CodeIgniter using NuSOAP PHP Toolkit
  • Simple JavaScript/CSS Lightbox
  • Import CSV data to MySQL Using PHP
  • Export MySQL data to CSV file Using PHP

Tag Cloud

A-Z alphabets in php AJAX alphabets using php array search in php Asynchronous JavaScript and XML CI NUSOAP library Client Side codeigniter codeigniter nusoap library coparison chart Database file upload using curl html to pdf import csv to mysql Javascript javascript css lightbox laravel lightbox MySQL nusoap client in codeigniter nusoap in codeigniter nusoap integration with codeigniter Open Source pagination in php and mysql pagination in php mysql pasination using php pdf pdf generation in codeigniter PHP php and mysql pagination php curl file upload php file upload using curl php mysql pagination php pagination php pagination function RDBMS Scripting Language search in multidimensional array Server Side language soap client in codeigniter soap in codeigniter soap server in codeigniter soap server using nusoap in codeigniter SQL TCPDF with CodeIgniter

Advertisements

Categories

  • AJAX
  • Javascript
  • MySql
  • PHP
    • PHP Frameworks
  • Theme created by PWT. Powered by WordPress.org

Copyright © 2014 PHP Guru