Jump to content

Uploading a file to a remote server, help!!


ravinghatmaker

Recommended Posts

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);

}

?>

 

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.