Thread: File Upload with libcurl

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    File Upload with libcurl

    Using VC++6 on XP, I am trying to upload a small jpeg file to a script that looks like:

    PHP Code:
    <form method="post" enctype="multipart/form-data" action="upload.php">

            
    File: <input type="file" name="file" size="40"> <br>
            
    Name: <input type="text" name="name" size="40"> <br>
            <
    input type="submit" value="send" name="submit"> <br>

    </
    form
    The code I am made is this:

    Code:
    #include <curl.h>
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	CURL *curl;
    	CURLcode res;
    
    	HttpPost* post =	NULL;
    	HttpPost* last =	NULL;
    
    	curl = curl_easy_init();
    	if(curl)
    	{
    		curl_formadd(&post, &last, 
    			CURLFORM_COPYNAME, "file",
    			CURLFORM_FILE, "C:\\rect.jpg",
    			CURLFORM_END);
    
    		curl_formadd(&post, &last, 
    			CURLFORM_COPYNAME, "name",
    			CURLFORM_FILE, "rect.jpg",
    			CURLFORM_END);
    
    		curl_easy_setopt(curl, CURLOPT_URL, "http://zxcvbn.t35.com/test.php");
    		curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
    
    		res = curl_easy_perform(curl);
    		if(res)
    		{
    			printf("curl_easy_perform failed: %d\n", res);
    		}
    		curl_formfree(post);
    	}
    	else
    		printf("curl_easy_init failed\n");
    
    	return 0;
    }
    And I am getting the error:

    curl_easy_perform failed: 7

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Maybe?
    CURLE_COULDNT_CONNECT, /* 7 */

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Sorry I forgot to check error codes, and I had forgotton to give the program priveledges and was on a limited account. I have now also changed it from C:\\rect.jpg to rect.jpg and had to move the executable because I was getting a read error (26). Now, I'm getting back from my web script: No file specified. This is the script;

    PHP Code:
    <?php

    if($_FILES['file']['name'] != "")
    {
        
    copy ($_FILES['file']['tmp_name'], "/uploads" $_FILES['file']['name'])
        or die (
    "Could not copy file");
    }
    else

        die( 
    "No file specified" ); 
    }

    ?>

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Are you using ethereal to watch
    a) what your browser does successfully when it uploads
    b) what your program fails to do

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Problem was with my script working with the host mostly. Thanks for the help. Final code:

    Code:
    	curl = curl_easy_init();
    	if(curl)
    	{
    		curl_formadd(&post, &last, 
    			CURLFORM_COPYNAME, "file",
    			CURLFORM_FILE, "C:\\rect.jpg",
    			CURLFORM_END);
    
    		curl_formadd(&post, &last, 
    			CURLFORM_COPYNAME, "name",
    			CURLFORM_COPYCONTENTS, "rect",
    			CURLFORM_END);
    
    		curl_easy_setopt(curl, CURLOPT_URL, "http://blah.com/upload.php");
    		curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
    
    		res = curl_easy_perform(curl);
    		if(res)
    		{
    			return 0;
    		}
    		
    		curl_formfree(post);
    	}
    	else 
    	{
    		return 0;
    	}
    
    	curl_easy_cleanup(curl);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  3. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM