ravinghatmaker Posted June 24, 2007 Share Posted June 24, 2007 Hi, I'm trying to upload a file to a remote server where the following variables also have to be passed: key = special md5 key based on the time name = name of file data = data of the file being uploaded. The problem is that the remote server doesn't seem to be accepting these variables when I send them. My code is made up of large part of code that was posted on php.net under fsockopen entry, but I've gone through it a million times and I still don't quite know what I'm doing wrong. I have a feeling there is something easy I am missing. Can anyone with any experience please take a look at this for me? On the form page before this, I just have a simple form with this code: <form enctype="multipart/form-data" action="uploadprocess.php" name="uploadvideo" method="POST"> Please choose a file: <input name="data" type="file" /><br /> <input name="submit" type="Submit" value="Upload" /> </form> and the uploadprocess.php file is: <? $api_key = '*******'; //This is my special key for md5 $keyvalue = md5(date("YmdHis") . $api_key); $keyvalue .= date("YmdHis") . "***"; //*** is my member ID number $file_name = $_FILES['data']['name']; $tmp_name = $_FILES['data']['tmp_name']; $content_type = $_FILES['data']['type']; srand((double)microtime()*1000000); $boundary = "---------------------".substr(md5(rand(0,32000)),0,10); // Build the header $header = "POST /api.php HTTP/1.0\r\n"; $header .= "Host: api.site.net\r\n"; $header .= "Content-type: multipart/form-data, boundary=$boundary\r\n"; // attach post vars $data .="--$boundary\r\n"; $data .= "Content-Disposition: form-data; name=\"key\"\r\n"; $data .= "\r\n".$keyvalue."\r\n"; $data .="--$boundary\r\n"; $data .="--$boundary\r\n"; $data .= "Content-Disposition: form-data; name=\"name\"\r\n"; $data .= "\r\n".$file_name."\r\n"; $data .="--$boundary\r\n"; // and attach the file $data .= "--$boundary\r\n"; $content_file .= join("", file($tmp_name)); $data .="Content-Disposition: form-data; name=\"data\"; filename=\"$file_name\"\r\n"; $data .= "Content-Type: $content_type\r\n\r\n"; $data .= "Content-Transfer-Encoding: binary\r\n\r\n"; $data .= "".$content_file."\r\n"; $data .="--$boundary--\r\n"; $header .= "Content-length: " . strlen($data) . "\r\n\r\n"; // Open the connection $fp = fsockopen("api.site.net", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { // then just fputs($fp, $header.$data); while (!feof($fp)) $http_dmy .= fgets($fp, 4096); fclose($fp); } ?> Quote Link to comment 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.