简单的PHP上传表单不起作用

时间:2010-05-26 07:01:18

标签: 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>

3 个答案:

答案 0 :(得分:2)

不检查PHP代码是否有错误,首先要注意的是:

isset($_POST['submit'])

始终返回false。 input type =“submit”必须具有name属性“submit”才能发送:

<input type="submit" value="submit" name="submit" />

答案 1 :(得分:1)

这是错误,在这样的提交按钮使用中:

<input type="submit" value="submit" name="submit" />

答案 2 :(得分:0)

好的

<?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>