FTP upload file to distant server with CURL and PHP uploads a blank file

I’m trying to upload a file to a distant server, but looks like the source file does nothing. All i get is a blank file on the server.
My code is this:


    $c = curl_init();
    $file = "\PATHTOFILE\file.txt";
    $fp = fopen($file, "r");

    curl_setopt($c, CURLOPT_URL, "SERVERPATH/file.txt");
    curl_setopt($c, CURLOPT_USERPWD, "USER:PASSWORD");
    curl_setopt($c, CURLOPT_UPLOAD, 1);
    curl_setopt($c, CURLOPT_INFILE, $fp);
    curl_setopt($c, CURLOPT_INFILESIZE, filesize($file));

      echo "Success";


Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

After 2 days of banging my head against the keyboard.. finally i did it..
Here is how:


    if (isset($_POST['Submit'])) {
 if (!empty($_FILES['upload']['name'])) {
    $ch = curl_init();
    $localfile = $_FILES['upload']['tmp_name'];
    $fp = fopen($localfile, 'r');
    curl_setopt($ch, CURLOPT_URL, 'ftp://domain.com/'.$_FILES['upload']['name']);
    curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
    curl_setopt($ch, CURLOPT_UPLOAD, 1);
    curl_setopt($ch, CURLOPT_INFILE, $fp);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
    curl_exec ($ch);
    $error_no = curl_errno($ch);
    curl_close ($ch);
        if ($error_no == 0) {
            $error = 'File uploaded succesfully.';
        } else {
            $error = 'File upload error.';
 } else {
        $error = 'Please select a file.';
  echo $error;  

Here is the source, from where i found the solution

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply