1、为什么不能自动感知file对象名而需要显式的传递呢?
2、如果file对象不以数组方式命名,则
foreach ($_FILES[$this->filename]['name'] as $key=>$fname)
将出错!!!
2、如果file对象不以数组方式命名,则
foreach ($_FILES[$this->filename]['name'] as $key=>$fname)
将出错!!!
解决方案 »
- 如何在linux 下配置 SMTP 服务
- 请高手详细解答!谢谢
- 求一正则
- MSSQL 语句中SELECT * FROM tb_user WHERE id in(".$_POST['bb'].")可以获得2个以上的传值。MYSQL 中要怎么写??
- 问一个简单的字符串问题。
- php5.1.2中我想在oci_fetch中再写一个数据库表的分析和执行可以吗?
- session不能工作(初学)
- 今天我在北京找了一个php工作,800,为公司做一商业程序……。
- SOAP,遇到VersionMismatch,求解决
- str_replace问题
- 打开一个页面未执行完之前改变URL又转成打开另一个页面或者关闭它,那第一次打开执行的内容还会后台运行着吗?
- php5中没有问题,4中有问题,怎么改才能解决
新增方法
uploadSingle,单文件上传修改后的版本。<style>
body{font:12px verdana;}
</style>
<pre>
<?php
// 在 4.1.0 以前的 PHP 中,需要用 $HTTP_POST_FILES 代替 $_FILES。
// 在 4.0.3 以前的 PHP 中,需要用 copy() 和 is_uploaded_file() 来代替 move_uploaded_file()。
/**
* Short description. fileupload
*
* Detail description fileupload
* @author
* @version 1.0
* @copyright [email protected]
*/
class fileupload
{
var $uploaddir ;
var $filename = 'userfile';
var $maxsize = 2048888;
var $extname = 'html,txt,htm';function setdir(){
$uploaddir = $this->uploaddir;
if (!is_dir($uploaddir)) {
$this->mkdirp($uploaddir);
}
}//end func setdirfunction uploadSingle(){
$fname = $_FILES[$this->filename]['name'];
if ($this->checkFileExt($fname)) {
$fsize = $_FILES[$this->filename]['size'];
if ($fsize <$this->maxsize) {
$uploadfile = $this->uploaddir . $this->filename;
$tmpfile = $_FILES[$this->filename]['tmp_name'];
echo "$uploadfile = $tmpfile ".$fsize."\n";
if (move_uploaded_file($tmpfile, $uploadfile)) {
print "$fname File is valid, and was successfully uploaded.\n";
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($_FILES);
}
}//size end
else {
$fsize /=1024*1024;
echo "$fname size is $fsize MB:the max upload size is 1MB";
}
}//fname}//end func uploadSingle/**
* Short description.
* @param none
*/
function uploadMulti ()
{
foreach ($_FILES[$this->filename]['name'] as $key=>$fname) {
if ($this->checkFileExt($fname)) {
$fsize = $_FILES[$this->filename]['size'][$key];
if ($fsize <$this->maxsize) {
$uploadfile = $this->uploaddir . $fname;
$tmpfile = $_FILES[$this->filename]['tmp_name'][$key];
echo "$uploadfile = $tmpfile ".$fsize."\n";
if (move_uploaded_file($tmpfile, $uploadfile)) {
print "$fname File is valid, and was successfully uploaded.\n";
//print_r($_FILES);
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($_FILES);
}
}//size end
else {
$fsize /=1024*1024;
echo "$fname size is $fsize MB:the max upload size is 1MB";
}
}//fname
}//for
} // end func uploadMultifunction checkFileExt($fname){
$path_parts = pathinfo($fname);
$ext = $path_parts["extension"];
$re = "/$ext/i";
preg_match($re,$this->extname, $matches);if ($matches[0]) //是文本文件扩展名为:.php,.xml,.css,.js.......由数组$matches定义
{
//echo $path_parts["dirname"] . "\n" .$path_parts["basename"]. "\n".$path_parts["extension"] . "\n";
return true;
}
return false;
}// end func/**
* mkdirp is used to instead of mkdir ,mkdirp can create deep multiple directory.
*/
function mkdirp($target) {
// If the path already exists && is a directory, all is well.
// If the path is not a directory, we've a problem.
if (file_exists($target)) {
if (!is_dir($target)) return false;
else return true;
} // Attempting to create the directory may clutter up our display.
if ( @mkdir($target) ) return true; // If the above failed, attempt to create the parent node, then try again.
if ( $this->mkdirp(dirname($target)) ) return $this->mkdirp($target); return false;
}//</function mkdirp>} // end classif ($_FILES['userfile']<>'') {
$mu = new fileupload;
$mu->filename = 'userf';
$mu->extname = 'html,htm,txt,zip,rar';
$mu->maxsize = 1*1024*1024;
$mu->uploaddir = 'd:/usr/www/uploads/';
$mu->uploadSingle();
}
?>
<FORM METHOD=POST enctype="multipart/form-data" ACTION="?">
<input type="file" name="userfile[]" />
<input type="file" name="userfile[]" />
<input type="file" name="userfile[]" />
s<input type="file" name="userf" />
<input type="submit" value="submit" />
</FORM>
editplus里编辑,演示+类代码共140行,4KB,功能上大家还有什么意见?<style>
body{font:12px verdana;}
</style>
<pre>
<?php
// 在 4.1.0 以前的 PHP 中,需要用 $HTTP_POST_FILES 代替 $_FILES。
// 在 4.0.3 以前的 PHP 中,需要用 copy() 和 is_uploaded_file() 来代替 move_uploaded_file()。
/**
* Short description. fileupload
* @author gu1dai
* @version 0.1
* @copyright [email protected]
*/
class fileupload
{
var $uploaddir ;
var $filename = 'userfile';
var $maxsize = 2048888;
var $extname = 'html,txt,htm';function setdir(){
$uploaddir = $this->uploaddir;
if (!is_dir($uploaddir)) {
$this->mkdirp($uploaddir);
}
}//end func setdirfunction upload(){
if (is_array($_FILES[$this->filename]['name'])) {
$this->uploadMulti();
echo 'multi upload';
}
else {
$this->uploadSingle();
echo 'single upload';
}
}function uploadSingle(){
$fname = $_FILES[$this->filename]['name'];
if ($this->checkFileExt($fname)) {
$fsize = $_FILES[$this->filename]['size'];
if ($fsize <$this->maxsize) {
$uploadfile = $this->uploaddir . $this->filename;
$tmpfile = $_FILES[$this->filename]['tmp_name'];
echo "$uploadfile = $tmpfile ".$fsize."\n";
if (move_uploaded_file($tmpfile, $uploadfile)) {
print "$fname File is valid, and was successfully uploaded.\n";
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($_FILES);
}
}//size end
else {
$fsize /=1024*1024;
echo "$fname size is $fsize MB:the max upload size is 1MB";
}
}//fname}//end func uploadSingle/**
* Short description. uploadMulti is used to upload lots of files
*/
function uploadMulti ()
{
foreach ($_FILES[$this->filename]['name'] as $key=>$fname) {
if ($this->checkFileExt($fname)) {
$fsize = $_FILES[$this->filename]['size'][$key];
if ($fsize <$this->maxsize) {
$uploadfile = $this->uploaddir . $fname;
$tmpfile = $_FILES[$this->filename]['tmp_name'][$key];
echo "$uploadfile = $tmpfile ".$fsize."\n";
if (move_uploaded_file($tmpfile, $uploadfile)) {
print "$fname File is valid, and was successfully uploaded.\n";
//print_r($_FILES);
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($_FILES);
}
}//size end
else {
$fsize /=1024*1024;
echo "$fname size is $fsize MB:the max upload size is 1MB";
}
}//fname
}//for
} // end func uploadMultifunction checkFileExt($fname){
$path_parts = pathinfo($fname);
$ext = $path_parts["extension"];
$re = "/$ext/i";
preg_match($re,$this->extname, $matches);if ($matches[0]) //是文本文件扩展名为:.php,.xml,.css,.js.......由数组$matches定义
{
return true;
}
return false;
}// end func/**
* mkdirp is used to instead of mkdir ,mkdirp can create deep multiple directory.
*/
function mkdirp($target) {
// If the path already exists && is a directory, all is well.
// If the path is not a directory, we've a problem.
if (file_exists($target)) {
if (!is_dir($target)) return false;
else return true;
} // Attempting to create the directory may clutter up our display.
if ( @mkdir($target) ) return true; // If the above failed, attempt to create the parent node, then try again.
if ( $this->mkdirp(dirname($target)) ) return $this->mkdirp($target); return false;
}//</function mkdirp>} // end classif ($_FILES['userfile']<>'') {
$mu = new fileupload;
$mu->filename = 'userf';
$mu->extname = 'html,htm,txt,zip,rar';
$mu->maxsize = 1*1024*1024;
$mu->uploaddir = 'd:/usr/www/uploads/';
$mu->upload();
}
?>
<FORM METHOD=POST enctype="multipart/form-data" ACTION="?">
<input type="file" name="userfile[]" />
<input type="file" name="userfile[]" />
<input type="file" name="userfile[]" />
s<input type="file" name="userf" />
<input type="submit" value="submit" />
</FORM>
function savefile($ar) {
//检查和保存上传文件,代码略
}
function upload() {
//检查是否有上传对象
if(! isset($_FILES))
return;
//自动感知file对象的名字,无须由外部提供
foreach($_FILES as $file=>$info) {
//如果是独立对象则直接处理
if(! is_array($info['name'])) {
$this->savefile($info);
continue;
}
//否则提取相应的元组进行处理
for($i=0;$i<count($info['name']);$i++) {
$this->savefile(array(
'name' => $info['name'][$i],
'type' => $info['type'][$i],
'tmp_name' => $info['tmp_name'][$i],
'error' => $info['error'][$i],
'size' => $info['size'][$i],
));
}
}
}
用script给表单的file项给予值应该就可以了
类似form.userfile1.value='d:/web/xxx.zip';
form.submit();