简单的PHP上传表单不起作用
问题描述:
在处理写入目录时,我似乎遇到了很多问题。如果有人可以请看这个脚本,并告诉我为什么它不起作用,我会很感激。简单的PHP上传表单不起作用
从窗体上传文件后,我什么也没有得到。它没有输出任何错误,只是刷新了。
感谢,LEA
<?php include ('config.php');
if(isset($_POST['submit'])) {
$target = ''.$_SERVER['DOCUMENT_ROOT'].'/images/';
$target = $target . basename($_FILES['photo']['name']) ;
$url = basename($_FILES['photo']['name']) ;
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
$moved = "File has been moved to location $target";
$name = mysql_real_escape_string($_POST['photoname']);
mysql_query("INSERT INTO photos (photo_name, photo_image) VALUES ('$name', '$url')") or die(mysql_error());
$success = "Photo has been added!";
} else {
$moved = "File has not been moved to $target";
$success = "Failed to upload:(";
}
} ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Photo Upload</title>
<meta name="robots" content="index, follow" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="<?php echo $globalurl; ?>styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="holder">
<br /><br />
<b>Add a new photo</b>
<hr />
<br />
<b><?php echo "$success<br />"; ?>
<?php echo "$moved<br />"; ?></b>
<form enctype="multipart/form-data" method="post" action="<?php echo $PHP_SELF; ?>">
<table cellspacing="0" cellpadding="0" width="500px">
<tbody>
<tr>
<td valign="top">Photo Name:</td>
<td valign="top">
<input type="text" name="photoname" /><br />
<br />
</td>
</tr>
<tr>
<td valign="top">Photo:</td>
<td valign="top">
<input type="file" name="photo"><br />
<br />
</td>
</tr>
</tbody>
</table>
<input type="submit" value="submit" />
</form>
</div>
</body>
</html>
答
没有检查错误PHP代码,脱颖而出的第一件事情是这样的:
isset($_POST['submit'])
始终返回false。输入类型=“提交”必须拥有的name属性“提交”发送:
<input type="submit" value="submit" name="submit" />
答
这是错误的,在提交按钮使用这样的:
<input type="submit" value="submit" name="submit" />
答
好做
<?php
if(isset($_POST['submit'])) {
$target = 'images/';
echo $target = $target . basename($_FILES['photo']['name']) ;
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
$moved = "File has been moved to location $target";
$success = "Photo has been added!";
}
else {
$moved = "File has not been moved to $target";
$success = "Failed to upload:(";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Photo Upload</title>
<meta name="robots" content="index, follow" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="<?php echo $globalurl; ?>styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="holder">
<b>Add a new photo</b>
<?php echo "$success<br />"; ?>
<?php echo "$moved<br />"; ?>
<form enctype="multipart/form-data" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<table cellspacing="0" cellpadding="0" width="500px">
<tbody>
<tr>
<td valign="top">Photo Name:</td>
<td valign="top">
<input type="text" name="photoname" /><br />
<br />
</td>
</tr>
<tr>
<td valign="top">Photo:</td>
<td valign="top">
<input type="file" name="photo">
</td>
</tr>
</tbody>
</table>
<input type="submit" value="submit" id="submit" name="submit" />
</form>
</div>
</body>
</html>
好的,谢谢。现在我感到愚蠢..大声笑。干杯队友,利亚 – Lea 2010-05-26 07:15:49