Jump to content

Uploading File via Curl Problem


bsmurfy

Recommended Posts

Hi All, I have been having issues for several days uploading a simple pdf file via Curl to one of our service providers servers.  I'm doing an is_uploaded_file check and getting a successful response, but the document is not actually being uploaded to the server. Can anyone help?? Please see code below:

 

$filename = $_FILES['activity_doc']['name']; // Name of the file (including file extension).
$upload_path = https://upload_url

//Curl session initialized
$session = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($session, CURLOPT_URL, $upload_path);
curl_setopt($session, CURLOPT_USERPWD, 'username:password');
curl_setopt($session, CURLOPT_POST, 1);


$post = array(
        "file_box"=>"@" . $filename,
    );


curl_setopt($session, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($session);

if (is_uploaded_file($_FILES['activity_doc']['tmp_name'])) {
   echo "File ". $_FILES['activity_doc']['name'] ." uploaded successfully.\n";
} else {
   echo "Error occurred, file uploaded unsuccessful";
}

$req = new SimpleXMLElement($response);
print_r($req);
curl_close($session);

Link to comment
Share on other sites

I made that change, but still having issues. Please see the current code below:

 

$filename = $_FILES['activity_doc']['name'];// Name of the file (including file extension).
$path = $_FILES['activity_doc']['tmp_name'];
$post = array(
        "file_box"=>"@/" . $path,
    );
$upload_path = 'https://uploadpath';

//Curl session initialized
$session = curl_init();
curl_setopt($session, CURLOPT_HEADER, 0);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_VERBOSE, 1);
curl_setopt($session, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($session, CURLOPT_URL, $upload_path);
curl_setopt($session, CURLOPT_USERPWD, 'username:password');
curl_setopt($session, CURLOPT_POST, true);
//curl_setopt($session, CURLOPT_HTTPHEADER, array(
                                                //"Content-Type: image/pdf",
                                                //));
curl_setopt($session, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($session);

print_r(curl_getinfo($session));




if (is_uploaded_file($_FILES['activity_doc']['tmp_name'])) {
   echo "File ". $_FILES['activity_doc']['name'] ." uploaded successfully.\n";
} else {
   echo "Error occurred, file uploaded unsuccessful";
}

$req = new SimpleXMLElement($response);
print_r($req);
curl_close($session);

Link to comment
Share on other sites

I see it. What are the issues?

 

For some reason you verify that the file was uploaded after you've used it.

 

I'm getting hints that you're simply copy and pasting code, and are not quite sure exactly what each chunk is doing.

Link to comment
Share on other sites

The issue is that the file simply isn't being uploaded. I'm not very experienced with CURL, but it is required for this small bit of my project and I've been looking all over for examples. I've successfully used it to post and receive data, but not for uploading actual content.

 

Uploading files via php/curl seems to be a pretty common function, and the examples are pretty consistent.  So I'm kinda stumped as to why this isn't working. I did copy a handful of lines of code as a foundation, but I do understand the basic work flow in the code. The is_uploaded_file code is there because I initially thought was checking for a successful upload to the remote, not local, server. Regardless I don't see why it would prevent the doc from actually uploading.

Link to comment
Share on other sites

Yes I understand. I'm just trying to rule out the different factors. The html portion for this is super simple, so its not likely there. I just want to see if there were any obvious errors, and/or common pitfalls that I may be missing. I've looked at several examples of the whole process from html form to php code, and I'm not seeing anything in mine that is inconsistent.

Link to comment
Share on other sites

Ok, I've made some progress. Figured out that curl didn't like the ssl cert from the server, and figured out the leading slash in my file path was causing problems. Right now I'm running into the failed creating formpost data error. I understand that this is because curl is unable to read the file from the given directory, but I'm not clear why. My current code is below:

 

$upload_directory=dirname(__FILE__).'/uploaded_files/';
$filename = $_FILES['activity_doc']['name'];// Name of the file (including file extension).
$path =  __DIR__ . '/'  . $filename;

$trimmed = ltrim($path, '/');

$post = array(
        "activity_doc"=>"@$trimmed",
    );
print_r($post);


if (is_uploaded_file($_FILES['activity_doc']['tmp_name'])) {
   echo "File ". $_FILES['activity_doc']['name'] ." uploaded successfully.\n";
} else {
   echo "Error occurred, file uploaded unsuccessful";
}


$upload_path = 'https://uploadpath

//Curl session initialized
$session = curl_init();
curl_setopt($session, CURLOPT_HEADER, 0);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_VERBOSE, 1);
curl_setopt($session, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($session, CURLOPT_URL, $upload_path);
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($session, CURLOPT_USERPWD, 'username:password');
curl_setopt($session, CURLOPT_POST, true);
//curl_setopt($session, CURLOPT_HTTPHEADER, array(
                                                //"Content-Type: image/pdf",
                                               // ));
curl_setopt($session, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($session);

print_r($response);
print_r(curl_error($session));
$req = new SimpleXMLElement($response);

curl_close($session);
}

 

The response from print_r($post) is [activity_doc] => @home/docs/public_html/test.pdf. I don't see test.pdf on the local server in that directory, so does this mean that the file isn't being uploaded to the local server, and therefore curl is unable to read it? Any help/suggestions are welcome! Thanks!

Link to comment
Share on other sites

It's good to see you've applied my advice! I feel much more inclined to give you the solution in code now :D

You'd be surprised how many time I read 'Well, where do I put this?'

 

The issue here is you aren't referencing the file you've uploaded. I'll explain more in the working code provided below.

Any issues beyond this are entirely communications-based, which we cannot help you with.

 

<?php 

// This is useless stuff here. It all references files/folders than dont exist, so I'll comment it out
/*
$upload_directory=dirname(__FILE__).'/uploaded_files/';
$filename = $_FILES['activity_doc']['name'];// Name of the file (including file extension).
$path =  __DIR__ . '/'  . $filename;

$trimmed = ltrim($path, '/');
*/

// Check if form was submitted, if not, display an upload form
if( $_SERVER['REQUEST_METHOD'] != 'POST' ) {
?>
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" enctype="multipart/form-data">
<input type="file" name="activity_doc">
<input type="submit">
</form>
<?php
// Otherwise, parse the upload
} else {

// Make sure file was uploaded
if (is_uploaded_file($_FILES['activity_doc']['tmp_name'])) {
	echo "File ". $_FILES['activity_doc']['name'] ." uploaded successfully.\n";
} else {
	die('Error occurred, file uploaded unsuccessful');
}

// Rename the file back to it's original name, and throw it into the temp folder
// This will overwrite any other files in the tmp directory with the same name
$new_file = sys_get_temp_dir() . $_FILES['activity_doc']['name'];
if( !move_uploaded_file( $_FILES['activity_doc']['tmp_name'], $new_file) )
	die( 'Could not move file to '.$new_file );

$post = array(
	"upfile"=>'@' . ltrim($new_file, '/')
);
print_r($post);


$upload_path = 'http://cgi-lib.berkeley.edu/ex/fup.cgi';

//Curl session initialized
$session = curl_init();
curl_setopt($session, CURLOPT_HEADER, 0);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_VERBOSE, 1);
curl_setopt($session, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($session, CURLOPT_URL, $upload_path);
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($session);

print_r($response);
print_r(curl_error($session));

curl_close($session);

// Now that everything is done, remove the temp file
unlink( $new_file );

}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.