用upload_file_move()函数吧。 copy函数不安全,另外你有什么出错信息吗? 看看php.ini下关于upload部分是否配置好了, ;;;;;;;;;;;;;;;; ; File Uploads ; ;;;;;;;;;;;;;;;;; Whether to allow HTTP file uploads. file_uploads = On //首先这个要为on才能上传; Temporary directory for HTTP uploaded files (will use system default if not ; specified). ;upload_tmp_dir =/tmp //这个目录在unix肯定存在的,应该没问题,如果改了就..; Maximum allowed size for uploaded files. upload_max_filesize = 32M //这个有没有限制?如果你写的1k,
;;;;;;;;;;;;;;;; ; File Uploads ; ;;;;;;;;;;;;;;;;; Whether to allow HTTP file uploads. file_uploads = On; Temporary directory for HTTP uploaded files (will use system default if not ; specified). ;upload_tmp_dir =; Maximum allowed size for uploaded files. upload_max_filesize = 2M我的设置是这样的。
copy函数不安全,另外你有什么出错信息吗?
看看php.ini下关于upload部分是否配置好了,
;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;; Whether to allow HTTP file uploads.
file_uploads = On //首先这个要为on才能上传; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
;upload_tmp_dir =/tmp //这个目录在unix肯定存在的,应该没问题,如果改了就..; Maximum allowed size for uploaded files.
upload_max_filesize = 32M //这个有没有限制?如果你写的1k,那...:)
$uploadPicture_name=basename($uploadPicture);
$uploadPicture_size=filesize($uploadPicture);
的时候,$uploadPicture_size = false 所以不能继续执行了。
所以说 没有什么错误提示。
/** BEGIN function
*
* 作者:偶然
* 功能:提示并刷新
* 时间:2003.9.5
* 变量:
* notes = 提示信息
* 返回:none
* 示例:
*
*/
function alert_refresh($notes)
{
echo "<Script language='javascript'>window.alert('$notes');</script>";
echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=".$_SERVER["HTTP_REFERER"]."\">";
exit;
}/** BEGIN function
*
* 作者:偶然
* 功能:上传多种文件
* 时间:2003.9.5
* 变量:
* file = 临时文件
* realname = 文件名
* filetype = 允许上传的文件类型
* dir = 上传的文件夹
*
* 返回:none
* 示例:
* $filetype = "jpg,png,jpeg,gif,rar,zip";//允许的文件类型
* $tempfile = $_FILES['file_name']['tmp_name'];//临时文件
* $filename = $_FILES['file_name']['name'];//文件名
* $uploaddir = "upload";//自行指定
* upload($tempfile,$filename,$filetype,$uploaddir);
*
*/
function upload($file,$realname,$type,$dir)
{
/* 判断是否有文件上传 */
if(!$file)
{
alert_refresh("没有文件上传!");
}
/* 取得允许的文件类型 */
$array = explode(',',$type); /* 清理多余空格 */
$realname = str_replace(' ','',$realname); /* 取得文件后缀 */
$postfix = substr(strrchr($realname,"."),1));
/* 检查文件类型是否允许 */
for($i=0; $i<count($array); $i++)
{
if($postfix == $array[$i])
{
$agree = '1';//设置上传标志位
}
}
/* 如果类型正确便进行下面的操作 */
if($agree==1)
{
/* 文件的保存路径,$dir由用户自己设置 */
$dest = $dir.'/'.$realname; /* 确保文件能准确上传 */
if(!is_uploaded_file($file))
{
alert_refresh("没有文件上传到指定位置!");
} /* 查看目标目录是否存在,若不存在就建立一个 */
if(!file_exists($dir))
{
$make = mkdir($dir,0755)
if(!$make)
{
alert_refresh("文件夹建立失败!");
}
}
else
{
/* 查看所上传的地方是否为一目录 */
if(!is_dir($dir))
{
alert_refresh("所上传的位置不是一个目录!");
} /* 检查上传文件是否存在 */
if(file_exists($dest))
{
alert_refresh("你所上传的文件已经存在!");
} /* 进行文件转移 */
$r = move_uploaded_file($file,$dest);
if($r == false)
{
alert_refresh("上传失败!");
}
else
{
alert_refresh("上传成功!");
}
}
}
else
{
alert_notes("文件类型不正确!");
}
}
曾经在本地调试时没有加上他也是成功的,但没有他在服务器上是决不可能成功。
1)它只适用于register_globals=on的情况下,而在Off的条件中是不能通过的。
2)另外希望你在调试时能把本地的php.ini中的出错显示打开,因为你的程序一到我这里就报错,可见并不是没有错,而是你忽视了错误的存在。
3)还有一点要指出的是,调试时时常echo或是print_r一下你的条件是否成立。
4)变量名太长了,降低了可读性,幸好我的耐性还比较好。
5)书写不规范,有机会给你一份编程规范。
以下我就不加上函数说明了,反正上面已讲得很清楚了。
function uploadfile($dir_for_image,$uploadPicture,$tomask)
{
global $uploadPicture,$tomask,$uploadPicture_name,$uploadPicture_size;
$uploadPicture_name = $uploadPicture['name'];
$uploadPicture_size = filesize($uploadPicture['tmp_name']); if (!$uploadPicture_size)
{
$uploadPicture_size = 1;
} //设置通配符mask; //计算有几个通配符;
$num_mask=count($tomask);
if ($uploadPicture_size&&$uploadPicture_name!="")
{
$file=$dir_for_image.$uploadPicture_name;
$length_uploadFile_name=strlen($uploadPicture_name);//当前上传文件名的长度;
$extension_uploadFile_name=substr($uploadPicture_name,$length_uploadFile_name-3,$length_uploadFile_name-1);
for ($i=1;$i<=$num_mask;$i++)
{
if ($extension_uploadFile_name==current($tomask))
{
echo "<table align=center border=1 bordercolor=pink cellspacing=0><tr class=text2 align=center><td>禁止上传此类文件!</td></tr>";
return 0;
}
next($tomask);
} //检查当前要上传的目录里是否有同名文件;
if(file_exists($file))
{
echo "<table align=center border=1 bordercolor=pink cellspacing=0><tr class=text2 align=center><td>同名文件存在,不能上传!</td></tr>" ;
return 0;
}
else
{
if (!copy($uploadPicture['tmp_name'],$file))
{
echo "文件拷贝出错!<br>";
return 0;
}
else
{
return 1;
}
}
}
else
{
echo "<table align=center border=1 bordercolor=pink cellspacing=0><tr class=text2 align=center><td>文件名不能为空,或者文件大小不能为零!</td></tr>";
return 0;
}
}
?>
<head>
<title>自助建站系统</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<table width="100%" align="center" cellpadding="5" cellspacing="0">
<form name="form2" method="post" action="" enctype='multipart/form-data'>
<tbody>
<tr>
<td height="30" align="right">文件:</td>
<td> <input name="upfile" type="file" id="dir_pic" size="35" maxlength="150" class="boxttt1"></td>
</tr>
<tr>
<td height="30"> </td>
<td><input name="upload" type="submit" id="upload" value="保存设置">
</td>
</tr>
</tbody>
</form>
</table>
<?
$dir_name = "upload/";
$tomask = array("exe","php","asp","jsp");
if(isset($_FILES['upfile']))
{
$uploadPicture = $_FILES['upfile'];
uploadfile($dir_name,$uploadPicture,$tomask);
}
ArrayuploadPicture = /tmp/phpCEZtLR
不知道这个地方是为什么而file= files/123.txt copy的时候就不成功了.服务器是UNIX不知道为什么 ?
copy函数不安全,另外你有什么出错信息吗?
看看php.ini下关于upload部分是否配置好了,
;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;; Whether to allow HTTP file uploads.
file_uploads = On //首先这个要为on才能上传; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
;upload_tmp_dir =/tmp //这个目录在unix肯定存在的,应该没问题,如果改了就..; Maximum allowed size for uploaded files.
upload_max_filesize = 32M //这个有没有限制?如果你写的1k,
;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;; Whether to allow HTTP file uploads.
file_uploads = On; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
;upload_tmp_dir =; Maximum allowed size for uploaded files.
upload_max_filesize = 2M我的设置是这样的。
echo "<table align=center border=1 bordercolor=pink cellspacing=0>";
echo "<tr><td class=text2>上传</td>"; echo "<td><input class=input type='hidden' name='MAX_FILE_SIZE' value=4096000>";//设置最大可以上传的文件大小 echo "<input class=form TYPE='file' name='uploadPicture' size=20></td></tr>"; //2M图片上传 echo "</table><table align=center border=0><tr><td>"; echo "<input class=input type=submit name='upit' value='上传'></td></tr></table>";
今天早上到服务器上进行了测试,测试地址(http://www.trustbbs.com/uptest.php,只保留到今天中午--2003.10.23),我的代码没有问题。如果我的代码在你的机器上不能通过,那么请检查你的php.ini的设置。
copy函数没有move_uploaded_file安全,最好别用。
uploadPicture = /tmp/phppvF787 file= /home/qlky1csossv5g8v682l3/wwwroot/files/win2000命令全集.txt
move_uploaded_file($uploadPIcture,$file)
这样也是没有成功, 反回值是false$uploadPIcture用 is_uploaded_file($uploadPIcture) true
file= files/win2000命令全集.txt
如果在我自己的计算机上
move_uploaded_file($uploadPIcture,$file)
这样就是成功的。
为什么刚才的就是失败的呢?
如果这些都没有问题的话,可能是和设置有关系了.可是关于php.ini的设置 我已经设置了。我也发了上去了。
<?
function uploadfile($dir_for_image,$uploadPicture,$tomask)
{
global $uploadPicture,$tomask,$uploadPicture_name,$uploadPicture_size;
print_r($uploadPicture);
$uploadPicture_name = $uploadPicture['name'];
$uploadPicture_size = filesize($uploadPicture['tmp_name']); if (!$uploadPicture_size)
{
$uploadPicture_size = 1;
} //设置通配符mask; //计算有几个通配符;
$num_mask=count($tomask);
if ($uploadPicture_size&&$uploadPicture_name!="")
{
$file=$dir_for_image.$uploadPicture_name;
$length_uploadFile_name=strlen($uploadPicture_name);//当前上传文件名的长度;
$extension_uploadFile_name=substr($uploadPicture_name,$length_uploadFile_name-3,$length_uploadFile_name-1);
for ($i=1;$i<=$num_mask;$i++)
{
if ($extension_uploadFile_name==current($tomask))
{
echo "<table align=center border=1 bordercolor=pink cellspacing=0><tr class=text2 align=center><td>禁止上传此类文件!</td></tr>";
return 0;
}
next($tomask);
} //检查当前要上传的目录里是否有同名文件;
if(file_exists($file))
{
echo "<table align=center border=1 bordercolor=pink cellspacing=0><tr class=text2 align=center><td>同名文件存在,不能上传!</td></tr>" ;
return 0;
}
else
{ echo "uploadPicture = ".$uploadPicture['tmp_name']." file= ".$file;
$resouce = '/tmp/'.$uploadPicture_name;
echo ' resouce = '.$resouce;
if(!is_uploaded_file($uploadPicture['tmp_name']))
echo " 文件不存在 ";
else
echo " 文件存在 ";
//if (!copy($uploadPicture['tmp_name'],$file))
if (!move_uploaded_file($uploadPicture['tmp_name'],$file))
{
echo "文件拷贝出错!<br>";
return 0;
}
else
{
echo "文件拷贝成功!<br>";
return 1;
}
}
}
else
{
echo "<table align=center border=1 bordercolor=pink cellspacing=0><tr class=text2 align=center><td>文件名不能为空,或者文件大小不能为零!</td></tr>";
return 0;
}
}
你能把上传到Linux下的变量,告诉我一下吗? 这个变量就是在tmp文件夹下的文件名称? 我想看下是不是也是这样的乱码,谢谢。就是uploadPicture = /tmp/phppvF787 是不是也是这样的?谢谢
move_uploaded_file($uploadPIcture,$file)
这样的书写是没有问题吧?
如果是没有问题,就是配置的问题了.这样的书写在linux下是没有问题的吧?
; File Uploads ;
;;;;;;;;;;;;;;;;; Whether to allow HTTP file uploads.
file_uploads = On; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
;upload_tmp_dir =; Maximum allowed size for uploaded files.
upload_max_filesize = 2M
这里的配置都与这个现象不符合。说明这里的配置都是正确的。
;;;;;;;;;;;;;;;;; Whether to allow HTTP file uploads.
file_uploads = On; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
;upload_tmp_dir =
**************前面的分号没去掉,第二,后面也没附值,你想让他的临时文件传到哪去?
****************
; Maximum allowed size for uploaded files.
upload_max_filesize = 2M
这里的配置都与这个现象不符合。说明这里的配置都是正确的。
if(!is_uploaded_file($uploadPicture['tmp_name']))
echo " 文件不存在 ";
echo "源文件是:".$uploadPicture['tmp_name']." 目标文件是:".$file;
////if (!copy($uploadPicture['tmp_name'],$file))
if (!move_uploaded_file($uploadPicture['tmp_name'],$file))
{
echo " 文件 move_uploaded_file源,目标, 没有成功!<br>";
return 0;
}
目标:upload/flash441.htm 上面的是我的没有成功,下面的是你的成功了
说明与函数没有关系了,与设置有关系,可是可能是与什么设置有关系呢?
因为在windows下是可以的。搞不懂!