下面是文件上传前台页面的部分代码 (8-18.php ) <form ENCTYPE="multipart/form-data" ACTION="8-19.php" METHOD="POST" onsubmit="return juge(this)">
<tr>
<td>选择图片:</td>
<td><input name="upfile" type="file" onchange=viewmypic(img1,this.form.upfile)></td>
以下是文件上传后台页面的部分代码 (8-19.php) <?
if($upfile=="")
{
echo"没有可选文件";
echo"<p>";
echo"点<a href=\"8-18.php\">这里</a>返回"; }调试后提示出错:
Notice: Undefined variable: upfile in E:\Program Files\PHPnow-1.5.6\htdocs\8-19.php on line 7最后结果是文件上传内容为空
<tr>
<td>选择图片:</td>
<td><input name="upfile" type="file" onchange=viewmypic(img1,this.form.upfile)></td>
以下是文件上传后台页面的部分代码 (8-19.php) <?
if($upfile=="")
{
echo"没有可选文件";
echo"<p>";
echo"点<a href=\"8-18.php\">这里</a>返回"; }调试后提示出错:
Notice: Undefined variable: upfile in E:\Program Files\PHPnow-1.5.6\htdocs\8-19.php on line 7最后结果是文件上传内容为空
用print_r( $_FILES['upfile']) 看看有没结果才是正确的
给你个例子 http://www.w3school.com.cn/php/php_file_upload.asp
$_FILES['upfile']['error'] 来确定.
用print_r( $_FILES['upfile']) 后输出下面结果,好像看着上传没错啊
Error: 0
Array ( [upfile] => Array ( [name] => 1-110321131112.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\Temp\php5774.tmp [error] => 0 [size] => 25626 ) )
具体怎么实现?我是新手,队php不是太熟悉,再解释详细点儿呗
$tmp_name = $_FILES['upfile']['tmp_name'];//上传后临时文件的位置
if(is_uploaded_file($tmp_name))//是提交后上传的,而不是用户自己放入的。
{
move_uploaded_file($tmp_name,$_SERVER['DOCUMENT_ROOT'].'./upload/.../filename');//临时文件移动到你要保存的目录。
}}
这样一个代码框架,里边的东西你可以自己补充。
我想先解决自己代码里upfile未定义这个问题,因为我还在学习阶段,所以解决出现的问题对我比较重要,能不能帮我看看我写的代码,分两个文件写的
8-18.php:
<body>
<script language="javascript">
//upfile是<input>中file类型的name值
function juge(theForm)
{
if(theForm.upfile.value=="")
{
alert("请先选择文件!");
theForm.upfile.focus();
return(false);
}
if(theForm.content.value=="")
{
alert("请输入图片说明!");
theForm.content.focus();
return(false);
}
if(theForm.content.value.length>50)
{
alert("说明内容太多,请重新输入!");
theForm.content.focus();
return(false);
}
}
function viewmypic(img1,upfile)
{
if(upfile.value)
{
img1.src=upfile.value;
img1.style=upfile.value;
img1.border=1;
}
}
</script>
<center>
<h3>相册管理系统图片上传页面</h3>
<p><p><p>
<a href="8-17.php">返回首页</a>
<table border="1">
<form ENCTYPE="multipart/form-data" ACTION="8-19.php" METHOD="POST" onsubmit="return juge(this)">
<tr>
<td>选择图片:</td>
<td><input name="upfile" type="file" onchange=viewmypic(img1,this.form.upfile)></td>
</tr>
<tr>
<td>输入说明:</td>
<td><input name="content" type="text">(*限30字)</td>
</tr>
<tr>
<td colspan="2"><center><input type=submit value="确认提交">
<input type=reset value="重新选择"></center></td>
</tr>
<tr>
<td colspan="2"><center><img src="" id="img1" style="display:none;"></center></td>
</tr>
</table>
</center>
</body>8-19.php
<body>
<?
if($upfile=="none")//没有选择相应文件
{
echo"没有可选文件";
echo"<p>";
echo"点<a href=\"8-18.php\">这里</a>返回";
}
else
{
$filepath="images/";
$filename=$filepath.$_FILES["upfile"]["name"];
if(copy($upfile,$filename))//如果文件被顺利复制
{
unlink($_FILES["upfile"]["tmp_name"]);
$dataname="data.dat";
$myfile=file($dataname);
if($myfile[0]=="")//记录文件为空的情况
{
$fp=fopen($dataname,"a+");
fwrite($fp,"1||".$_FILES[upfile][name]."||".$_POST["content"]."||".date("y年m月d日")."\n");
fclose(fp);
}
else //记录文件中已有相应内容
{
$temp=explode("||",$myfile[0]);
$temp[0]++;
$fp=fopen($dataname,"r");
$line_has=fread($fp,filesize("$dataname"));
fclose($fp);
$fp=fopen($dataname,"w");
fwrite($fp,$temp[0]."||".$_FILES["upfile"]["name"]."||".$_POST["content"]."||".date("y年m月d日")."\n");
fwrite($fp,"$line_has");
fclose($fp);
}
echo"指定文件已经成功上传!";
echo"<p><p>";
echo"点<a href=\"8-17.php\">这里</a>返回";
}
else
echo"文件上传失败!";
}
?>
</body>
$upfile = $_FILES['upfile'];
if($upfile=="none")//没有选择相应文件
{
echo"没有可选文件";
echo"<p>";
......
是啊,我学嵌入式,web是因为喜欢所以才打算学的,最近才接触php,所以请各位帮帮忙啦,毕竟是新手么
而对于文件上传。
虽然表单中使用了name = 'upfile',后台使用直接使用$upfile的话,实际上是得到的文件名字。
这两种情况都不会提示未定义$upfile 的问题。
请看测试:
<form action="" method = "post" enctype = "multipart/form_data">
<input type="text" name="name" value="ssss"/>
<input type="file" name="file"/>
<input type="submit" name='submit' value="提交"/>
</form>
<?php
ini_set('display_errors',1);
if(isset($_POST['submit'])){
print_r($_POST);
echo "<br/>".$name."<br/>";
print_r($file);
}
?>而更好的方案是不直接使用$upfile而使用超级全局变量。你可以在执行前加上判断if(isset($_POST['submit']))来判断表单是否提交。提交执行动作。
if (isset($_POST["upfile"])) $upfile=$_POST["upfile"];
else $upfile="";
if($upfile=="")
{
echo"没有可选文件";
echo"<p>";
echo"点<a href=\"8-18.php\">这里</a>返回";
}
else{}
我说了,更通用的方法是使用全局变量$_FILES处理。
if(isset($_POST['submit'])){if($_FILES['upfile']['error'] == 0){//大于0表示有错误发生,错误代码可查看文档。
$tmp_name = $_FILES['upfile']['tmp_name'];//上传后临时文件的位置
if(is_uploaded_file($tmp_name))//是提交后上传的,而不是用户自己放入的。
{
move_uploaded_file($tmp_name,$_SERVER['DOCUMENT_ROOT'].'./upload/... /filename');//临时文件移动到你要保存的目录。
}
}