当前位置: 编程技术>php
本页文章导读:
▪利用discuz实现PHP大文件上传应用实例代码
对于确实需要改善论坛附件上传条件的朋友可以尝试将上面提及的参数在php.ini进行设置,以适应大文件上传的需要。同时别忘记在论坛的后台相应做附件限制的地方进行设置。
论坛主要有2.........
▪php下载远程文件类(支持断点续传)
简易使用方法: 代码如下:$object = new httpdownload(); $object->set_byfile($file)%N#H#%;//服务器文件名,包括路径 $object->filename = $filename;//下载另存为的文件名 $object->download(); 3.源文件: 代码如下:.........
▪PHP和Java 集成开发详解分析 强强联合第1/4页
时间一天天过去,这两个亮点也变得越来越亮,很快,它们受到了编程者的喜欢,于是有人有疑问了:要是它们两者相遇,会发生什么事情?有没有可能将它们的强项结合在一起呢? 尝试.........
[1]利用discuz实现PHP大文件上传应用实例代码
来源: 互联网 发布时间: 2013-11-30
对于确实需要改善论坛附件上传条件的朋友可以尝试将上面提及的参数在php.ini进行设置,以适应大文件上传的需要。同时别忘记在论坛的后台相应做附件限制的地方进行设置。
论坛主要有2个地方可以对附件上传的大小进行限制,级别从高到低依次为:
- 帖子相关---附件类型尺寸
- 用户组---附件相关
同时,下面提供一个配置指导,来源一些成功通过http上传大附件的朋友的提供,当然,由于大家的服务器配置情况以及网络情况不同,并不一定适用你的情况,可能很多地方需要参照修改:
打开php.ini,
把上述参数修改后,在网络所允许的正常情况下,就可以上传大体积文件了
论坛文件上传常见错误类型(不断总结...)- Warning: Unable to open '\\php2' for reading: Invalid argument in e:\user\web\larksoft.net\upload\upfile.php on line 10
是php的upload_tmp_dir的原因,所指定的目录必须可读可写
- Parse error: parse error in c:\program files\apache group\apache\htdocs\mdweb\ftpfile\upload.php on line 14
Parse error一般都是语句的问题,比如象“;”,“'”,“)”等等的匹配问题 。
[2]php下载远程文件类(支持断点续传)
来源: 互联网 发布时间: 2013-11-30
简易使用方法:
$object = new httpdownload();
$object->set_byfile($file)%N#H#%;//服务器文件名,包括路径
$object->filename = $filename;//下载另存为的文件名
$object->download();
3.源文件:
<?
class httpdownload {
var $data = null;
var $data_len = 0;
var $data_mod = 0;
var $data_type = 0;
var $data_section = 0; //section download
var $sentSize=0;
var $handler = array('auth' => null);
var $use_resume = true;
var $use_autoexit = false;
var $use_auth = false;
var $filename = null;
var $mime = null;
var $bufsize = 2048;
var $seek_start = 0;
var $seek_end = -1;
var $totalsizeref = 0;
var $bandwidth = 0;
var $speed = 0;
function initialize() {
global $HTTP_SERVER_VARS;
if ($this->use_auth) //use authentication
{
if (!$this->_auth()) //no authentication
{
header('WWW-Authenticate: Basic realm="Please enter your username and password"');
header('HTTP/1.0 401 Unauthorized');
header('status: 401 Unauthorized');
if ($this->use_autoexit) exit();
return false;
}
}
if ($this->mime == null) $this->mime = "application/octet-stream"; //default mime
if (isset($_SERVER['HTTP_RANGE']) || isset($HTTP_SERVER_VARS['HTTP_RANGE']))
{
if (isset($HTTP_SERVER_VARS['HTTP_RANGE'])) $seek_range = substr($HTTP_SERVER_VARS['HTTP_RANGE'] , strlen('bytes='));
else $seek_range = substr($_SERVER['HTTP_RANGE'] , strlen('bytes='));
$range = explode('-',$seek_range);
if ($range[0] > 0)
{
$this->seek_start = intval($range[0]);
}
if ($range[1] > 0) $this->seek_end = intval($range[1]);
else $this->seek_end = -1;
if (!$this->use_resume)
{
$this->seek_start = 0;
//header("HTTP/1.0 404 Bad Request");
//header("Status: 400 Bad Request");
//exit;
//return false;
}
else
{
$this->data_section = 1;
}
}
else
{
$this->seek_start = 0;
$this->seek_end = -1;
}
$this->sentSize=0;
return true;
}
function header($size,$seek_start=null,$seek_end=null) {
header('Content-type: ' . $this->mime);
header('Content-Disposition: attachment; filename="' . $this->filename . '"');
header('Last-Modified: ' . date('D, d M Y H:i:s \G\M\T' , $this->data_mod));
if ($this->data_section && $this->use_resume)
{
header("HTTP/1.0 206 Partial Content");
header("Status: 206 Partial Content");
header('Accept-Ranges: bytes');
header("Content-Range: bytes $seek_start-$seek_end/$size");
header("Content-Length: " . ($seek_end - $seek_start + 1));
}
else
{
header("Content-Length: $size");
}
}
function download_ex($size)
{
if (!$this->initialize()) return false;
ignore_user_abort(true);
//Use seek end here
if ($this->seek_start > ($size - 1)) $this->seek_start = 0;
if ($this->seek_end <= 0) $this->seek_end = $size - 1;
$this->header($size,$seek,$this->seek_end);
$this->data_mod = time();
return true;
}
function download() {
if (!$this->initialize()) return false;
try
{
error_log("begin download\n", 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
$seek = $this->seek_start;
$speed = $this->speed;
$bufsize = $this->bufsize;
$packet = 1;
//do some clean up
@ob_end_clean();
$old_status = ignore_user_abort(true);
@set_time_limit(0);
$this->bandwidth = 0;
$size = $this->data_len;
if ($this->data_type == 0) //download from a file
{
$size = filesize($this->data);
if ($seek > ($size - 1)) $seek = 0;
if ($this->filename == null) $this->filename = basename($this->data);
$res = fopen($this->data,'rb');
if ($seek) fseek($res , $seek);
if ($this->seek_end < $seek) $this->seek_end = $size - 1;
$this->header($size,$seek,$this->seek_end); //always use the last seek
$size = $this->seek_end - $seek + 1;
while (!(connection_aborted() || connection_status() == 1) && $size > 0)
{
if ($size < $bufsize)
{
echo fread($res , $size);
$this->bandwidth += $size;
$this->sentSize+=$size;
}
else
{
echo fread($res , $bufsize);
$this->bandwidth += $bufsize;
$this->sentSize+=$bufsize;
}
$size -= $bufsize;
flush();
if ($speed > 0 && ($this->bandwidth > $speed*$packet*1024))
{
sleep(1);
$packet++;
}
}
fclose($res);
}
elseif ($this->data_type == 1) //download from a string
{
if ($seek > ($size - 1)) $seek = 0;
if ($this->seek_end < $seek) $this->seek_end = $this->data_len - 1;
$this->data = substr($this->data , $seek , $this->seek_end - $seek + 1);
if ($this->filename == null) $this->filename = time();
$size = strlen($this->data);
$this->header($this->data_len,$seek,$this->seek_end);
while (!connection_aborted() && $size > 0) {
if ($size < $bufsize)
{
$this->bandwidth += $size;
$this->sentSize+=$size;
}
else
{
$this->bandwidth += $bufsize;
$this->sentSize+=$bufsize;
}
echo substr($this->data , 0 , $bufsize);
$this->data = substr($this->data , $bufsize);
$size -= $bufsize;
flush();
if ($speed > 0 && ($this->bandwidth > $speed*$packet*1024))
{
sleep(1);
$packet++;
}
}
} else if ($this->data_type == 2) {
//just send a redirect header
header('location: ' . $this->data);
}
if($this->totalsizeref==$this->sentSize )error_log("end download\n", 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
else error_log("download is canceled\n", 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
if ($this->use_autoexit) exit();
//restore old status
ignore_user_abort($old_status);
set_time_limit(ini_get("max_execution_time"));
}
catch(Exception $e)
{
error_log("cancel download\n".$e, 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
}
return true;
}
function set_byfile($dir) {
if (is_readable($dir) && is_file($dir)) {
$this->data_len = 0;
$this->data = $dir;
$this->data_type = 0;
$this->data_mod = filemtime($dir);
$this->totalsizeref = filesize($dir);
return true;
} else return false;
}
function set_bydata($data) {
if ($data == '') return false;
$this->data = $data;
$this->data_len = strlen($data);
$this->data_type = 1;
$this->data_mod = time();
return true;
}
function set_byurl(/blog_article/$data/index.html) {
$this->data = $data;
$this->data_len = 0;
$this->data_type = 2;
return true;
}
function set_lastmodtime($time) {
$time = intval($time);
if ($time <= 0) $time = time();
$this->data_mod = $time;
}
function _auth() {
if (!isset($_SERVER['PHP_AUTH_USER'])) return false;
if (isset($this->handler['auth']) && function_exists($this->handler['auth']))
{
return $this->handler['auth']('auth' , $_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW']);
}
else return true; //you must use a handler
}
}
?>
代码如下:
$object = new httpdownload();
$object->set_byfile($file)%N#H#%;//服务器文件名,包括路径
$object->filename = $filename;//下载另存为的文件名
$object->download();
3.源文件:
代码如下:
<?
class httpdownload {
var $data = null;
var $data_len = 0;
var $data_mod = 0;
var $data_type = 0;
var $data_section = 0; //section download
var $sentSize=0;
var $handler = array('auth' => null);
var $use_resume = true;
var $use_autoexit = false;
var $use_auth = false;
var $filename = null;
var $mime = null;
var $bufsize = 2048;
var $seek_start = 0;
var $seek_end = -1;
var $totalsizeref = 0;
var $bandwidth = 0;
var $speed = 0;
function initialize() {
global $HTTP_SERVER_VARS;
if ($this->use_auth) //use authentication
{
if (!$this->_auth()) //no authentication
{
header('WWW-Authenticate: Basic realm="Please enter your username and password"');
header('HTTP/1.0 401 Unauthorized');
header('status: 401 Unauthorized');
if ($this->use_autoexit) exit();
return false;
}
}
if ($this->mime == null) $this->mime = "application/octet-stream"; //default mime
if (isset($_SERVER['HTTP_RANGE']) || isset($HTTP_SERVER_VARS['HTTP_RANGE']))
{
if (isset($HTTP_SERVER_VARS['HTTP_RANGE'])) $seek_range = substr($HTTP_SERVER_VARS['HTTP_RANGE'] , strlen('bytes='));
else $seek_range = substr($_SERVER['HTTP_RANGE'] , strlen('bytes='));
$range = explode('-',$seek_range);
if ($range[0] > 0)
{
$this->seek_start = intval($range[0]);
}
if ($range[1] > 0) $this->seek_end = intval($range[1]);
else $this->seek_end = -1;
if (!$this->use_resume)
{
$this->seek_start = 0;
//header("HTTP/1.0 404 Bad Request");
//header("Status: 400 Bad Request");
//exit;
//return false;
}
else
{
$this->data_section = 1;
}
}
else
{
$this->seek_start = 0;
$this->seek_end = -1;
}
$this->sentSize=0;
return true;
}
function header($size,$seek_start=null,$seek_end=null) {
header('Content-type: ' . $this->mime);
header('Content-Disposition: attachment; filename="' . $this->filename . '"');
header('Last-Modified: ' . date('D, d M Y H:i:s \G\M\T' , $this->data_mod));
if ($this->data_section && $this->use_resume)
{
header("HTTP/1.0 206 Partial Content");
header("Status: 206 Partial Content");
header('Accept-Ranges: bytes');
header("Content-Range: bytes $seek_start-$seek_end/$size");
header("Content-Length: " . ($seek_end - $seek_start + 1));
}
else
{
header("Content-Length: $size");
}
}
function download_ex($size)
{
if (!$this->initialize()) return false;
ignore_user_abort(true);
//Use seek end here
if ($this->seek_start > ($size - 1)) $this->seek_start = 0;
if ($this->seek_end <= 0) $this->seek_end = $size - 1;
$this->header($size,$seek,$this->seek_end);
$this->data_mod = time();
return true;
}
function download() {
if (!$this->initialize()) return false;
try
{
error_log("begin download\n", 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
$seek = $this->seek_start;
$speed = $this->speed;
$bufsize = $this->bufsize;
$packet = 1;
//do some clean up
@ob_end_clean();
$old_status = ignore_user_abort(true);
@set_time_limit(0);
$this->bandwidth = 0;
$size = $this->data_len;
if ($this->data_type == 0) //download from a file
{
$size = filesize($this->data);
if ($seek > ($size - 1)) $seek = 0;
if ($this->filename == null) $this->filename = basename($this->data);
$res = fopen($this->data,'rb');
if ($seek) fseek($res , $seek);
if ($this->seek_end < $seek) $this->seek_end = $size - 1;
$this->header($size,$seek,$this->seek_end); //always use the last seek
$size = $this->seek_end - $seek + 1;
while (!(connection_aborted() || connection_status() == 1) && $size > 0)
{
if ($size < $bufsize)
{
echo fread($res , $size);
$this->bandwidth += $size;
$this->sentSize+=$size;
}
else
{
echo fread($res , $bufsize);
$this->bandwidth += $bufsize;
$this->sentSize+=$bufsize;
}
$size -= $bufsize;
flush();
if ($speed > 0 && ($this->bandwidth > $speed*$packet*1024))
{
sleep(1);
$packet++;
}
}
fclose($res);
}
elseif ($this->data_type == 1) //download from a string
{
if ($seek > ($size - 1)) $seek = 0;
if ($this->seek_end < $seek) $this->seek_end = $this->data_len - 1;
$this->data = substr($this->data , $seek , $this->seek_end - $seek + 1);
if ($this->filename == null) $this->filename = time();
$size = strlen($this->data);
$this->header($this->data_len,$seek,$this->seek_end);
while (!connection_aborted() && $size > 0) {
if ($size < $bufsize)
{
$this->bandwidth += $size;
$this->sentSize+=$size;
}
else
{
$this->bandwidth += $bufsize;
$this->sentSize+=$bufsize;
}
echo substr($this->data , 0 , $bufsize);
$this->data = substr($this->data , $bufsize);
$size -= $bufsize;
flush();
if ($speed > 0 && ($this->bandwidth > $speed*$packet*1024))
{
sleep(1);
$packet++;
}
}
} else if ($this->data_type == 2) {
//just send a redirect header
header('location: ' . $this->data);
}
if($this->totalsizeref==$this->sentSize )error_log("end download\n", 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
else error_log("download is canceled\n", 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
if ($this->use_autoexit) exit();
//restore old status
ignore_user_abort($old_status);
set_time_limit(ini_get("max_execution_time"));
}
catch(Exception $e)
{
error_log("cancel download\n".$e, 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
}
return true;
}
function set_byfile($dir) {
if (is_readable($dir) && is_file($dir)) {
$this->data_len = 0;
$this->data = $dir;
$this->data_type = 0;
$this->data_mod = filemtime($dir);
$this->totalsizeref = filesize($dir);
return true;
} else return false;
}
function set_bydata($data) {
if ($data == '') return false;
$this->data = $data;
$this->data_len = strlen($data);
$this->data_type = 1;
$this->data_mod = time();
return true;
}
function set_byurl(/blog_article/$data/index.html) {
$this->data = $data;
$this->data_len = 0;
$this->data_type = 2;
return true;
}
function set_lastmodtime($time) {
$time = intval($time);
if ($time <= 0) $time = time();
$this->data_mod = $time;
}
function _auth() {
if (!isset($_SERVER['PHP_AUTH_USER'])) return false;
if (isset($this->handler['auth']) && function_exists($this->handler['auth']))
{
return $this->handler['auth']('auth' , $_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW']);
}
else return true; //you must use a handler
}
}
?>
[3]PHP和Java 集成开发详解分析 强强联合第1/4页
来源: 互联网 发布时间: 2013-11-30
时间一天天过去,这两个亮点也变得越来越亮,很快,它们受到了编程者的喜欢,于是有人有疑问了:要是它们两者相遇,会发生什么事情?有没有可能将它们的强项结合在一起呢?
尝试在PHP和Java之间搭建一座桥梁,利用这座桥梁在这两个实体之间建立起一个沟通渠道,在这座桥梁的帮助下,你可以在Java中开发类,然后在PHP中调用它们的方法,同样,在你的Java桌面或Web应用程序中也可以使用PHP脚本。
在这篇文章中,你将会学到如何:
◆安装和配置PHP/Java桥
◆在PHP脚本中使用Java类
◆在Java类中使用PHP脚本
◆在JSP页面中使用PHP脚本
那我们开始吧!
安装和配置PHP/Java桥
最新的PHP/Java桥zip包可在http://sourceforge.net/projects/php-java-bridge/下载到,安装过程依赖于选择哪个Java平台通过这座桥与PHP脚本交互。
◆对于J2SE,安装非常简单:
◆安装J2SE 1.6或更高版本
◆安装PHP 5.1.4或更高版本
◆解压php-java-bridge_5.2.2_j2ee.zip包
从命令提示符进入刚刚解压后的目录,输入:
?>java ?classpath JavaBridge.war TestInstallation
在这个文件夹下,你应该看到一个ext目录,它下面包括四个.jar文件,拷贝其中的JavaBridge.jar 和php-script.jar到你的J2SE安装目录下的ext文件夹(通常是{JAVA_HOME}/jre/lib/ext)。
对于J2EE,要执行下列安装步骤:
将JavaBridge.war文件拷贝到你的J2EE服务器或servlet引擎(Tomcat,Resin等)下的auto_deploy文件夹。
根据你的应用程序重命名该文件,然后重启J2EE服务器,等待自动部署进程创建与该.war文件相关的目录,在这个例子中,这个应用程序叫做appName.war。
从浏览器测试新的应用程序,输入:http://localhost:8080/appName,然后点击test.php。
如果你的J2EE服务器运行在不同的主机和端口好,参数要做相应的修改。
注意:如果你想在Apache或IIS上运行J2EE/PHP应用程序,将包括appName的目录拷贝到Apache/IIS的文档根目录下。
尝试在PHP和Java之间搭建一座桥梁,利用这座桥梁在这两个实体之间建立起一个沟通渠道,在这座桥梁的帮助下,你可以在Java中开发类,然后在PHP中调用它们的方法,同样,在你的Java桌面或Web应用程序中也可以使用PHP脚本。
在这篇文章中,你将会学到如何:
◆安装和配置PHP/Java桥
◆在PHP脚本中使用Java类
◆在Java类中使用PHP脚本
◆在JSP页面中使用PHP脚本
那我们开始吧!
安装和配置PHP/Java桥
最新的PHP/Java桥zip包可在http://sourceforge.net/projects/php-java-bridge/下载到,安装过程依赖于选择哪个Java平台通过这座桥与PHP脚本交互。
◆对于J2SE,安装非常简单:
◆安装J2SE 1.6或更高版本
◆安装PHP 5.1.4或更高版本
◆解压php-java-bridge_5.2.2_j2ee.zip包
从命令提示符进入刚刚解压后的目录,输入:
?>java ?classpath JavaBridge.war TestInstallation
在这个文件夹下,你应该看到一个ext目录,它下面包括四个.jar文件,拷贝其中的JavaBridge.jar 和php-script.jar到你的J2SE安装目录下的ext文件夹(通常是{JAVA_HOME}/jre/lib/ext)。
对于J2EE,要执行下列安装步骤:
将JavaBridge.war文件拷贝到你的J2EE服务器或servlet引擎(Tomcat,Resin等)下的auto_deploy文件夹。
根据你的应用程序重命名该文件,然后重启J2EE服务器,等待自动部署进程创建与该.war文件相关的目录,在这个例子中,这个应用程序叫做appName.war。
从浏览器测试新的应用程序,输入:http://localhost:8080/appName,然后点击test.php。
如果你的J2EE服务器运行在不同的主机和端口好,参数要做相应的修改。
注意:如果你想在Apache或IIS上运行J2EE/PHP应用程序,将包括appName的目录拷贝到Apache/IIS的文档根目录下。
最新技术文章: