bsmurfy Posted November 7, 2011 Share Posted November 7, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/250644-uploading-file-via-curl-problem/ Share on other sites More sharing options...
n3r0x Posted November 7, 2011 Share Posted November 7, 2011 is_uploaded_file checks it the specified file is uploaded on the local server. To check for success you would need to enterremote server and check if the file exists there.. Quote Link to comment https://forums.phpfreaks.com/topic/250644-uploading-file-via-curl-problem/#findComment-1285982 Share on other sites More sharing options...
bsmurfy Posted November 7, 2011 Author Share Posted November 7, 2011 Thanks, yea it seems like its not making that final step. Any ideas on what would be stopping that? Quote Link to comment https://forums.phpfreaks.com/topic/250644-uploading-file-via-curl-problem/#findComment-1285986 Share on other sites More sharing options...
xyph Posted November 7, 2011 Share Posted November 7, 2011 You have to provide an absolute path to the file. You can't just pass the file name into the cURL request. Quote Link to comment https://forums.phpfreaks.com/topic/250644-uploading-file-via-curl-problem/#findComment-1285987 Share on other sites More sharing options...
bsmurfy Posted November 7, 2011 Author Share Posted November 7, 2011 Thanks, I was wondering if that might be an issue. I'm noticing that the example I followed references the full path. I'll try using $_FILES[name][tpm_name]. Quote Link to comment https://forums.phpfreaks.com/topic/250644-uploading-file-via-curl-problem/#findComment-1285992 Share on other sites More sharing options...
bsmurfy Posted November 7, 2011 Author Share Posted November 7, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/250644-uploading-file-via-curl-problem/#findComment-1286032 Share on other sites More sharing options...
xyph Posted November 7, 2011 Share Posted November 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/250644-uploading-file-via-curl-problem/#findComment-1286043 Share on other sites More sharing options...
bsmurfy Posted November 7, 2011 Author Share Posted November 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/250644-uploading-file-via-curl-problem/#findComment-1286051 Share on other sites More sharing options...
xyph Posted November 7, 2011 Share Posted November 7, 2011 It shouldn't, but it helps to verify the local file has been uploaded before using it in the cURL. It's very hard to debug your issue because there is so much that could go wrong that isn't PHP related. Quote Link to comment https://forums.phpfreaks.com/topic/250644-uploading-file-via-curl-problem/#findComment-1286063 Share on other sites More sharing options...
bsmurfy Posted November 7, 2011 Author Share Posted November 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/250644-uploading-file-via-curl-problem/#findComment-1286067 Share on other sites More sharing options...
bsmurfy Posted November 8, 2011 Author Share Posted November 8, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/250644-uploading-file-via-curl-problem/#findComment-1286384 Share on other sites More sharing options...
xyph Posted November 8, 2011 Share Posted November 8, 2011 It's good to see you've applied my advice! I feel much more inclined to give you the solution in code now 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 ); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/250644-uploading-file-via-curl-problem/#findComment-1286401 Share on other sites More sharing options...
bsmurfy Posted November 9, 2011 Author Share Posted November 9, 2011 Thanks a lot! This is helping me a lot. One quick thing, still running into the problem, but I'm suspecting its because the $post value is incorrect. Right now it is @tmptest.pdf. Shouldn't it be the fill path? Quote Link to comment https://forums.phpfreaks.com/topic/250644-uploading-file-via-curl-problem/#findComment-1286448 Share on other sites More sharing options...
xyph Posted November 9, 2011 Share Posted November 9, 2011 Echo out sys_get_temp_dir to verify it returns a proper temporary path. If it doesn't return a trailing slash, you may need to add it. Quote Link to comment https://forums.phpfreaks.com/topic/250644-uploading-file-via-curl-problem/#findComment-1286455 Share on other sites More sharing options...
bsmurfy Posted November 9, 2011 Author Share Posted November 9, 2011 Thanks, I tried that, with $post = tmp/test.pdf. Still not getting around the issue. Quote Link to comment https://forums.phpfreaks.com/topic/250644-uploading-file-via-curl-problem/#findComment-1286463 Share on other sites More sharing options...
xyph Posted November 9, 2011 Share Posted November 9, 2011 Have you verified that tmp/test.pdf actually exists before running the cURL? You realize this will fail if the file you're trying to send them isn't really there. Quote Link to comment https://forums.phpfreaks.com/topic/250644-uploading-file-via-curl-problem/#findComment-1286477 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.