/** * 从网络传输文件,直接使用linux的wget命令 * @param string $inputfile 来源地址 * @param string $outputfile 输出文件路径 * @return int */ function wget($inputfile, $outputfile) { //在线获取文件类型 $onlinefiletype = online_filetype($inputfile); //wget续传抓取文件 $command = "wget -c '" . escapeshellcmd($inputfile) . "' -O '{$outputfile}' -T 120 -t 5"; exec($command, $result, $retval); /*if (defined('cmd_render') && !cmd_render){ $command .= ' 2>&1 >/dev/null'; echo $command,"\n"; }*/ if ($retval) { return implode("\n", $result); } if (!is_file($outputfile)) { return "{$outputfile} not exists"; } //获取抓取下来的文件类型 $localfiletype = filemime($outputfile); if ($onlinefiletype && $onlinefiletype != $localfiletype) { return "online {$onlinefiletype} not match local {$localfiletype}"; } return 0; }
} }); $app->get('/admin/media/{id}', function ($request, $response, $args) { $db = getConnection(); try { echo json_encode(getMedia($db, $args['id'])); } catch (Exception $e) { error_status($response, $e->getMessage()); } }); $app->get('/admin/files', function ($request, $response, $args) { try { $file = $request->getQueryParams()['file']; if (file_exists($file) && is_file($file)) { $dispositionResponse = $response->withHeader('Content-disposition', 'attachment; filename=' . basename($file)); $typeResponse = $dispositionResponse->withHeader('Content-type', filemime($file)); $typeResponse->getBody()->write(file_get_contents(getcwd() . '/' . $file)); return $typeResponse; } elseif (file_exists($file) && is_dir($file)) { $typeResponse = $response->withHeader('Content-type', 'text/x-dir'); $files = scandir(getcwd() . '/' . $file); array_shift($files); $typeResponse->getBody()->write($file . '/:' . implode("|", $files)); return $typeResponse; } else { error_status($response, 'Error: ' . $file . ' does not exist.'); } } catch (Exception $e) { error_status($response, $e->getMessage()); } });
/** * 通过CURL向指定的url路径上传文件 * @param string $url 目标url地址 * @param string $file 要上传的本地文件 * @param array|string $postFields 附带的post数据数组|字串 * @param string $fieldname 自定义文件表单名 * @return boolean */ function curl_upload($url, $file, $postFields = null, $fieldname = 'file') { if (!function_exists('curl_init')) { return false; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if (is_string($postFields)) { parse_str($postFields, $postFields); } else { $postFields = (array) $postFields; } if (class_exists('\\CURLFile')) { curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); $postFields[$fieldname] = new \CURLFile(realpath($file)); /*curl_setopt($ch, CURLOPT_POSTFIELDS, array( $fieldname => new \CURLFile(realpath($file)) ));*/ } else { if (defined('CURLOPT_SAFE_UPLOAD')) { curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); } $filename = basename($file); $type = filemime($file); $postFields[$fieldname] = '@' . realpath($file) . ";type=" . $type . ";filename=" . $filename; /*curl_setopt($ch, CURLOPT_POSTFIELDS, array( $fieldname => '@' . realpath($file) . ";type=" . $type . ";filename=" . $filename ));*/ } curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); $return_data = curl_exec($ch); curl_close($ch); return $return_data; }