当前位置:Gxlcms > PHP教程 > 关于一次下传多个图片的插件有没有

关于一次下传多个图片的插件有没有

时间:2021-07-01 10:21:17 帮助过:18人阅读

关于一次上传多个图片的插件有没有?
像discuz发帖那样,一次上传多个图片,然后将所有上传的图片路径插入到编辑器中。

有没有?


------解决方案--------------------
找个JS批量上传插件,主要是美观而已,技术角度还是个表单。

从http协议实现角度,下面的php代码是一个活生生的协议细节:


function do_post_request($url, $postdata, $files = null) 
{
$data = "";
$boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10);

//Collect Postdata
foreach($postdata as $key => $val)
{
$data .= "--$boundary\n";
$data .= "Content-Disposition: form-data; name=\"".$key."\"\n\n".$val."\n";
}

$data .= "--$boundary\n";

//Collect Filedata
foreach($files as $key => $file)
{
$fileContents = file_get_contents($file['tmp_name']);

$data .= "Content-Disposition: form-data; name=\"{$key}\"; filename=\"{$file['name']}\"\n";
$data .= "Content-Type: image/jpeg\n";
$data .= "Content-Transfer-Encoding: binary\n\n";
$data .= $fileContents."\n";
$data .= "--$boundary--\n";
}

$params = array('http' => array(
'method' => 'POST',
'header' => 'Content-Type: multipart/form-data; boundary='.$boundary,
'content' => $data
));

$ctx = stream_context_create($params);
$fp = fopen($url, 'rb', false, $ctx);

if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}

$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}

//set data (in this example from post)

//sample data
$postdata = array(
'name' => $_POST['name'],
'age' => $_POST['age'],
'sex' => $_POST['sex']

人气教程排行