CRTP - это метод реализации полиморфизма времени компиляции. Вот очень простой пример. В приведенном ниже примере ProcessFoo()
работает с интерфейсом класса Base
, а Base::Foo
вызывает метод foo()
производного объекта, который вы намерены делать с виртуальными методами.
http://coliru.stacked-crooked.com/a/2d27f1e09d567d0e
template
struct Base {
void foo() {
(static_cast(this))->foo();
}
};
struct Derived : public Base {
void foo() {
cout << "derived foo" << endl;
}
};
struct AnotherDerived : public Base {
void foo() {
cout << "AnotherDerived foo" << endl;
}
};
template
void ProcessFoo(Base* b) {
b->foo();
}
int main()
{
Derived d1;
AnotherDerived d2;
ProcessFoo(&d1);
ProcessFoo(&d2);
return 0;
}
Выход:
derived foo
AnotherDerived foo
Должно быть возможно: вот форма, размещенная через браузер (нерелевантные поля опущены):
POST http://host.example.com/somewhere HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------------7da16b2e4026c
Content-Length: 105732
-----------------------------7da16b2e4026c
Content-Disposition: form-data; name="NewFile"; filename="test.jpg"
Content-Type: image/jpeg
(...raw JPEG data here...)
-----------------------------7da16b2e4026c
Content-Disposition: form-data; name="otherformfield"
content of otherformfield is this text
-----------------------------7da16b2e4026c--
Итак, если мы сами построим тело POST и установим дополнительный заголовок или два, мы должны уметь имитировать это:
// form field separator
$delimiter = '-------------' . uniqid();
// file upload fields: name => array(type=>'mime/type',content=>'raw data')
$fileFields = array(
'file1' => array(
'type' => 'text/plain',
'content' => '...your raw file content goes here...'
), /* ... */
);
// all other fields (not file upload): name => value
$postFields = array(
'otherformfield' => 'content of otherformfield is this text',
/* ... */
);
$data = '';
// populate normal fields first (simpler)
foreach ($postFields as $name => $content) {
$data .= "--" . $delimiter . "\r\n";
$data .= 'Content-Disposition: form-data; name="' . $name . '"';
// note: double endline
$data .= "\r\n\r\n";
}
// populate file fields
foreach ($fileFields as $name => $file) {
$data .= "--" . $delimiter . "\r\n";
// "filename" attribute is not essential; server-side scripts may use it
$data .= 'Content-Disposition: form-data; name="' . $name . '";' .
' filename="' . $name . '"' . "\r\n";
// this is, again, informative only; good practice to include though
$data .= 'Content-Type: ' . $file['type'] . "\r\n";
// this endline must be here to indicate end of headers
$data .= "\r\n";
// the file itself (note: there's no encoding of any kind)
$data .= $file['content'] . "\r\n";
}
// last delimiter
$data .= "--" . $delimiter . "--\r\n";
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_HTTPHEADER , array(
'Content-Type: multipart/form-data; boundary=' . $delimiter,
'Content-Length: ' . strlen($data)));
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
curl_exec($handle);
Таким образом, мы делаем все тяжелое поднятие себя и доверяем cURL, чтобы не калечить его.
php имеет доступ к временному местоположению «php: // memory», что фактически делает то, что вы пытаетесь сделать довольно легко.
$fh = fopen('php://memory','rw');
fwrite( $fh, $content);
rewind($fh);
$options = array(
CURLOPT_RETURNTRANSFER => true
,CURLOPT_SSL_VERIFYPEER => false
,CURLOPT_SSL_VERIFYHOST => 1
,CURLOPT_HTTPHEADER => array(
'Content-type: multipart/form-data'
)
,CURLOPT_INFILE => $fh
,CURLOPT_INFILESIZE => strlen($content)
);
PUT
загрузить INFILE
несколько файлов?
– DanFromGermany
10 July 2013 в 14:31
$postFields
в конце первогоforeach
вам не хватает$data .= $content . "\r\n";
, иначе содержимое поля не будет отправлено. – Cesar 18 July 2013 в 18:27