高分求高手来看看上传问题

haihao_110 2010-07-30 09:36:20
我想上传文件时在备份一个文件 备份的文件名为上传文件名+上传人姓名+上传时间 ,
都不是汉字啊这是我的代码高手们帮帮看看,为什么报错
<form enctype="multipart/form-data" action="" method="post">
<table width="400" border="0" cellspacing="1" cellpadding="1" align="center" bgcolor="#E6E6E6" >
<tr>
<td height="22" align="center" valign="middle" bgcolor="#F5F5F5" width="400"> 文件上传 </td>
</tr>
<tr align="center" valign="middle">
<td align="center" height="80" width="400" bgcolor="#FFFFFF">
选择文件:<input type="file" name="upload_file" style="width:300">
</td>
</tr>
<tr align="center" valign="middle">
<td bgcolor="#F5F5F5" height="24" width="400">
<div align="center">
<input type="submit" value="开始上传">
</div></td>
</tr>
</table>
</form>

//$uploadFileName = $_POST['test'];
$ext = '.txt';
if(strrpos(@$_FILES['upload_file']['name'],'.') !== false){
$ext = substr( @$_FILES['upload_file']['name'], strrpos(@$_FILES['upload_file']['name'], '.' ));
}
if(strtolower($ext) != '.txt'
&& strtolower($ext) != '.jpg'
&& strtolower($ext) != '.gif'
&& strtolower($ext) != '.png'
&& strtolower($ext) != '.doc'){
echo '上传的文件类型不符合要求';
exit;
}

$fileName = "E:\site_data/".'/'.date('Ymd').'/'.time().rand(100,999).$ext;
move_uploaded_file(@$_FILES['upload_file']['tmp_name'],$_SERVER['DOCUMENT_ROOT'].'/'.$fileName);//这行报错
//
//$uploadfile="E:\site_data/".$_FILES['upload_file']['name'];
//if(move_uploaded_file($_FILES['upload_file']['tmp_name'],$uploadfile)){
//echo "成功";}

'D:/APMServ5.2.6/www/htdocs/E:\site_data//20100730/1280453764180.txt' in D:\APMServ5.2.6\www\htdocs\shangchuan.php on line 36
怎么改啊!!!!
...全文
148 点赞 收藏 31
写回复
31 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Dleno 2010-07-30
$upload_dir=="./site_data/";
$upload_bak="./site_data/";
//你至少要对$username赋予值.
$username = $_POST['username'];//提交过来的名字
//或者$username = $_COOKIE['username'];//cookie里的名字
$file=$_FILES['upload'];

$ext = explode(".",$file['name']);
$ext = $ext[count($ext)-1];
$filename1 = substr($file['name'],0,(strlen($file['name'])-strlen($ext)-1));
$bakfilename = "{$username}_".date("Y_m_d_H_i_s").".{$ext}";
if(move_uploaded_file($file['tmp_name'],$upload_bak.$file['name'])){
copy($upload_bak.$file['name'], $upload_bak.$bakfilename);
}
回复
Alfredknox 2010-07-30
帮顶,接分。。。
回复
haihao_110 2010-07-30
上传成功了 但是备份文件的文件名没有上传人的名字 只有时间 大侠们帮我看看吧
$upload_dir=="./site_data/";
$upload_bak="./site_data/";

$file=$_FILES['upload'];

$ext = explode(".",$file['name']);
$ext = $ext[count($ext)-1];
$filename1 = substr($file['name'],0,(strlen($file['name'])-strlen($ext)-1));
$bakfilename = "{$username}_{$uid}_".date("Y_m_d_H_i_s").".{$ext}";
if(move_uploaded_file($file['tmp_name'],$upload_bak.$file['name'])){
copy($upload_bak.$file['name'], $upload_bak.$bakfilename);
}
回复
haihao_110 2010-07-30
[Quote=引用 6 楼 cunningboy 的回复:]
PHP code
<?php
$fileName = date('Ymd').'/'.time().rand(100,999).$ext;
$base_folder = "E:/site_data/";
move_uploaded_file(@$_FILES['upload_file']['tmp_name'],$base_folder.$fileName);
?>

……
[/Quote]
我之前也这样写过但是也是没有效果
$ext = '.txt';
if(strrpos(@$_FILES['upload_file']['name'],'.') !== false){
$ext = substr( @$_FILES['upload_file']['name'], strrpos(@$_FILES['upload_file']['name'], '.' ));
}
if(strtolower($ext) != '.txt'
&& strtolower($ext) != '.jpg'
&& strtolower($ext) != '.gif'
&& strtolower($ext) != '.htm'
&& strtolower($ext) != '.doc'){
echo '上传的文件类型不符合要求';
exit;
}
$fileName = date('Ymd').'/'.time().rand(100,999).$ext;
$base_folder = "E:/site_data/";
if(move_uploaded_file(@$_FILES['upload_file']['tmp_name'],$base_folder.$fileName)){
echo "成功";}
回复
r3r4r8 2010-07-30
*o*路过*o*有分给点分,无分积10分
回复
jianglin28290 2010-07-30
$fileName = "E:/site_data/".'/'.date('Ymd').'/'.time().rand(100,999).$ext;
人家已经写的很清楚了,\在php里是转义,就算你要用在路径上,至少也要加两个吧
回复
CunningBoy 2010-07-30
<?php
$fileName = date('Ymd').'/'.time().rand(100,999).$ext;
$base_folder = "E:/site_data/";
move_uploaded_file(@$_FILES['upload_file']['tmp_name'],$base_folder.$fileName);
?>

回复
haihao_110 2010-07-30
感谢楼上的提示但是我改过后 不报错 但是上传文件后在 E:\site_data/文件夹中没有我上传的文件
$fileName = "E:\site_data/".'/'.date('Ymd').'/'.time().rand(100,999).$ext;            
//move_uploaded_file(@$_FILES['upload_file']['tmp_name'],$fileName);
//$fileName = date('Ymd').'/'.time().rand(100,999).$ext;
move_uploaded_file(@$_FILES['upload_file']['tmp_name'],$fileName);
@copy($_SERVER['DOCUMENT_ROOT'].'/'.$fileName, "E:\site_data/".'/'.$fileName);

感谢大侠们赐教
回复
xuzuning 2010-07-30
你是越改越乱了!
$fileName = "E:/site_data/".date('Ymd').'/'.time().rand(100,999).$ext;
你需要保证相应的目录存在并可写
比如今天
E:/site_data/20100730
明天
E:/site_data/20100731


回复
haihao_110 2010-07-30
谢谢大家 但还是报错说failed to open stream: No such file or directory in D:\APMServ5.2.6\www\htdocs\shangchuan.php on line 38

//$uploadFileName = $_POST['test'];
$ext = '.txt';
if(strrpos(@$_FILES['upload_file']['name'],'.') !== false){
$ext = substr( @$_FILES['upload_file']['name'], strrpos(@$_FILES['upload_file']['name'], '.' ));
}
if(strtolower($ext) != '.txt'
&& strtolower($ext) != '.jpg'
&& strtolower($ext) != '.gif'
&& strtolower($ext) != '.htm'
&& strtolower($ext) != '.doc'){
echo '上传的文件类型不符合要求';
exit;
}

//$fileName = "E:\site_data/".'/'.date('Ymd').'/'.time().rand(100,999).$ext;
//move_uploaded_file(@$_FILES['upload_file']['tmp_name'],$fileName);
$fileName = date('Ymd').'/'.time().rand(100,999).$ext;
move_uploaded_file(@$_FILES['upload_file']['tmp_name'],$_SERVER['DOCUMENT_ROOT'].'/'.$fileName);//就是这行
@copy($_SERVER['DOCUMENT_ROOT'].'/'.$fileName, "E:\site_data/".'/'.$fileName);

我想让上传文件保存到E:\site_data/中
回复
程序猿之殇 2010-07-30
$_SERVER['DOCUMENT_ROOT'] = 'D:/APMServ5.2.6/www/htdocs
$fileName你又给"E:\site_data/".'/'.date('Ymd').'/'.time().rand(100,999).$ext;传值了

两个构造在一起,肯定是不存在的路径了.

$fileName = date('Ymd').'/'.time().rand(100,999).$ext;            
move_uploaded_file(@$_FILES['upload_file']['tmp_name'],$_SERVER['DOCUMENT_ROOT'].'/'.$fileName);
@copy($_SERVER['DOCUMENT_ROOT'].'/'.$fileName, "E:\site_data/".'/'.$fileName);
回复
xuzuning 2010-07-30
既然你有
$fileName = "E:/site_data/".'/'.date('Ymd').'/'.time().rand(100,999).$ext;
那么就不需要而外指明路径了
move_uploaded_file(@$_FILES['upload_file']['tmp_name'], $fileName);
回复
haihao_110 2010-07-30
这是最后调好的
$filename1 = substr($file['name'],0,(strlen($file['name'])-strlen($ext)-1));              
$bakfilename = $filename1."_{$uid}_{$username}_".date("Y_m_d_H_i_s").".{$ext}";
if(move_uploaded_file($file['tmp_name'],$upload_bak.$file['name'])){
copy($upload_bak.$file['name'], $upload_bak.$bakfilename);
echo "上传成功";
回复
kyzy_yy_pm 2010-07-30
说说怎么回事
回复
haihao_110 2010-07-30
你这样写还是报错 ,不过谢谢你,问题我已经解决了,结贴给分了
回复
kyzy_yy_pm 2010-07-30
copy($upload_dir.$file['name'], $upload_dir.$file['name'].'_'.$bakfilename);

失误,楼主改下
回复
haihao_110 2010-07-30
[Quote=引用 24 楼 kyzy_yy_pm 的回复:]
PHP code
$upload_dir="./bak_data/";
$upload_bak="./site_data/";

$file=$_FILES['upload'];
$username=$_POST['upname'];


$ext = explode(".",$file['name']);
$ext = $ext[count($ext)-1];
$file……
[/Quote]
你这样写又报错了Warning: copy(./site_data/PHP对数据库的操作.txt) [function.copy]: failed to open stream: No such file or directory in D:\APMServ5.2.6\www\htdocs\shangchuan.php on line 52
就是这句copy($upload_bak.$file['name'], $upload_bak.$file['name'].'_'.$bakfilename);
回复
kyzy_yy_pm 2010-07-30
按楼主的想法的话$upload_bak根本没用,迷惑人
回复
kyzy_yy_pm 2010-07-30
copy($upload_bak.$file['name'], $upload_dir.$file['name'].'_'.$bakfilename);
回复
kyzy_yy_pm 2010-07-30
$upload_dir="./bak_data/";
$upload_bak="./site_data/";

$file=$_FILES['upload'];
$username=$_POST['upname'];


$ext = explode(".",$file['name']);
$ext = $ext[count($ext)-1];
$filename1 = substr($file['name'],0,(strlen($file['name'])-strlen($ext)-1));
$bakfilename = "{$username}_{$uid}_".date("Y_m_d_H_i_s").".{$ext}";
if(move_uploaded_file($file['tmp_name'],$upload_dir.$file['name'])){
copy($upload_bak.$file['name'], $upload_bak.$file['name'].'_'.$bakfilename);
echo "上传成功";
}

回复
发动态
发帖子
基础编程
创建于2007-09-28

2.1w+

社区成员

从PHP安装配置,PHP入门,PHP基础到PHP应用
申请成为版主
社区公告
暂无公告