?? download.csp
字號:
<% @b
@include <stdlib.h>
@include <time.h>
@include <sys/types.h>
@include <sys/stat.h>
@include <undef.h>
@ifdef WIN32
@ include <fcntl.h>
@ include <io.h>
@ include <windows.h>
@ define usleep Sleep
@else
@ include <unistd.h>
@endif
@include <ebdef.h>
#if 1
int sendFile(char * fileName, char * fancyName, BOOL forceDownload,
int speedLimit, char * contentType);
sendFile(G("file"), NULL, TRUE, 0, NULL);
page_exit(OK);
#else
char * getenv(const char*);
char fname[512] = "";
char buff[512] = "";
struct stat fst;
int rlen = 0;
FILE * fp;
int len;
/* get filename */
strcpy(fname, G("file"));
if ('\0' == fname[0])
{
ebSendError(400, "not find file name");
page_exit(ERROR);
}
stat(fname, &fst);
if (NULL == (fp=fopen(fname, "rb")))
{
ebSendError(400, "Open file '%s' error.", fname);
page_exit(ERROR);
}
/*
* output mime header: filename
*/
header("Content-Length: %d\n", fst.st_size);
header("Content-Disposition: attachment; filename=\"%s\"\n",
get_filename(fname));
header("Content-Type: application/octet-stream\n\n");
ebBufFlush(__ebfp);
/* write data to stdout */
while(!feof(fp) && (len=fread(buff, 1, sizeof(buff), fp)) > 0) {
ebWriteBlock(__ebfp, buff, len);
rlen += len;
if (rlen > (1<<18)) {
rlen = 0;
Sleep(1);
}
}
/*
* close file
*/
fclose(fp);
page_exit(OK);
#endif
%>
<% @g
static BOOL preg_match2(char * fmt, char * src, int *matchs)
{
char * pstart;
if (isblankstr(src))
return FALSE;
if (NULL == (pstart=strstr(src, "bytes=")))
return FALSE;
pstart += 6;
/* get first number */
matchs[1] = strtol(pstart, &pstart, 10);
if (NULL == pstart || '-' != *pstart)
return FALSE;
/* get second number */
pstart ++; /* jump over '-' */
if (isblankstr(pstart))
matchs[2] = -1;
else
matchs[2] = strtol(pstart, NULL, 10);
return TRUE;
}
/**
* 發(fā)送文件
*
* @author: legend(legendsky@hotmail.com)
* @link: http://www.ugia.cn/?p=109
* @description: send file to client
* @version: 1.0
*
* @param string $fileName 文件名稱或路徑
* @param string $fancyName 自定義的文件名,為空則使用filename
* @param boolean $forceDownload 是否強制下載
* @param integer $speedLimit 速度限制,單位為字節(jié),0為不限制,不支持windows服務(wù)器
* @param string $$contentType 文件類型,默認為application/octet-stream
*
* @return boolean
*/
int sendFile(char * fileName, char * fancyName, BOOL forceDownload,
int speedLimit, char * contentType)
{
struct stat fileStat;
time_t lastModified;
int fileSize;
int contentLength;
BOOL isPartial;
char etag[256] = "";
int matches[3];
int startPos = 0;
int endPos = 0;
int length;
int bufferSize;
int bytesSent;
FILE * fp;
char buffer[1024];
char timebuf[128];
int readBufferSize;
static int rlen = 0;
int len;
const char * rfc1123_fmt = "%a, %d %b %Y %H:%M:%S GMT";
if (stat(fileName, &fileStat) < 0)
{
header("HTTP/1.1 404 Not Found\n\n");
return FALSE;
}
lastModified = fileStat.st_mtime;
sprintf(etag, "%ul-%ul", (int)fileStat.st_mtime, (int)fileStat.st_size);
strftime(timebuf, sizeof(timebuf), rfc1123_fmt, gmtime(&lastModified));
header("Last-Modified: %s\r\n", timebuf);
header("ETag: \"%s\"\r\n", etag);
#if 0
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $lastModified)
{
header("HTTP/1.1 304 Not Modified");
return TRUE;
}
if (isset($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) < $lastModified)
{
header("HTTP/1.1 304 Not Modified");
return TRUE;
}
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag)
{
header("HTTP/1.1 304 Not Modified");
return TRUE;
}
#else
/* if (!ebLastModifyCheck(lastModified))
return TRUE; */
#endif
#define basename get_filename
if (isblankstr(fancyName))
{
fancyName = basename(fileName);
}
if (isblankstr(contentType))
{
contentType = "application/octet-stream";
}
fileSize = fileStat.st_size;
contentLength = fileSize;
isPartial = FALSE;
if (!isblankstr(getenv("HTTP_RANGE")))
{
if (preg_match2("/^bytes=(d*)-(d*)$/", getenv("HTTP_RANGE"), matches))
{
startPos = matches[1];
endPos = matches[2];
if (-1 == startPos && -1 == endPos)
{
return FALSE;
}
if (-1 == startPos)
{
startPos = fileSize - endPos;
endPos = fileSize - 1;
}
else if (-1 == endPos)
{
endPos = fileSize - 1;
}
startPos = startPos < 0 ? 0 : startPos;
endPos = endPos > fileSize - 1 ? fileSize - 1 : endPos;
length = endPos - startPos + 1;
if (length < 0)
{
return FALSE;
}
contentLength = length;
isPartial = TRUE;
}
}
/* send headers */
if (isPartial)
{
header("Status: 206 Partial Content\n");
header("Content-Range: bytes %d-%d/%d\n", startPos, endPos, fileSize);
}
else
{
header("Status: 200 OK\n");
startPos = 0;
endPos = contentLength - 1;
}
header("Pragma: cache\n");
header("Cache-Control: public, must-revalidate, max-age=0\n");
header("Accept-Ranges: bytes\n");
header("Content-type: %s\n", contentType);
header("Content-Length: %d\n", contentLength);
if (forceDownload)
{
header("Content-Disposition: attachment; filename=\"%s\"\n", fancyName);
}
header("Content-Transfer-Encoding: binary\n\n");
bufferSize = sizeof(buffer);
#if 0
if (speedLimit != 0)
{
packetTime = floor(bufferSize * 1000000 / speedLimit);
}
#endif
bytesSent = 0;
fp = fopen(fileName, "rb");
fseek(fp, startPos, SEEK_SET);
while (bytesSent < contentLength && !feof(fp))
{
#if 0
if (speedLimit != 0)
{
list(usec, sec) = explode(" ", microtime());
outputTimeStart = ((float)usec + (float)sec);
}
#endif
readBufferSize = contentLength - bytesSent;
readBufferSize = readBufferSize < bufferSize ? readBufferSize: bufferSize;
len = fread(buffer, 1, readBufferSize, fp);
if (len != readBufferSize)
break;
len = ebWriteBlock(NULL, buffer, readBufferSize);
if (len != readBufferSize)
break;
bytesSent += readBufferSize;
#if 1
rlen += readBufferSize;
if (rlen > (1<<18))
{
rlen = 0;
usleep(1);
}
#else
if (speedLimit != 0)
{
list(usec, sec) = explode(" ", microtime());
outputTimeEnd = ((float)usec + (float)sec);
useTime = ((float) outputTimeEnd - (float) outputTimeStart) * 1000000;
sleepTime = round(packetTime - useTime);
if (sleepTime > 0)
{
usleep(sleepTime);
}
}
#endif
}
fflush(fp);
fclose(fp);
return TRUE;
}
%>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -