1

尝试在这里构建一个用于上传文本和图像的管理面板是 html 页面。Html 页面工作正常,它是损坏的 php 页面,html 仅供参考。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<title>Administartor Panel</title>
<head>
<link rel="stylesheet" type="text/css" href="admin.css" />
</head>
<body>
<h1>Heritage House Administartor Panel</h1>
<br/>
<h2>
<a href="#eone">Event One</a>&nbsp;
<a href="#etwo">Event Two</a>&nbsp;
<a href="#ethree">Event Three</a>&nbsp;
<a href="#efour">Event Four</a>&nbsp;
<a href="#efive">Event Five</a>&nbsp;
<a href="#esix">Event Six</a>&nbsp;
</h2>
<br/>
<table>
<tr>
<td id="eone">
<br/>
<p>Event One</P>
<p> Please name the picture file1 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="afile" id="afile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt1.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical One:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="etwo">
<p >Event Two</P>
<p> Please name the picture file2 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="bfile" id="bfile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt2.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Two:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="ethree" >
<p >Event Three</P>
<p> Please name the picture file3 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="cfile" id="cfile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt3.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Three:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="efour" >
<p >Event Four</P>
<p> Please name the picture file4 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="dfile" id="dfile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt4.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Four:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="efive" >
<p >Event Five</P>
<p> Please name the picture file5 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="efile" id="efile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt5.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Five:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="esix" >
<p >Event Six</P>
<p> Please name the picture file6 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="ffile" id="file6" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt6.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Six:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
</body>
</html>

试图重命名上传的文件基于它们来自 html 页面 afile、bfile 等的表单。我已经尝试了这个 php 文件多种不同的方式。当我消除大型 IF 语句并制作 6 个单独的文件以供上传时,我可以让它工作,但我希望将其制作为一个文件这就是我认为它无法将全局变量移动到普通变量的原因?

        <?php
    //This function separates the extension from the rest of the file name and returns it
    function findexts ($filename)
    {
    $filename = strtolower($filename) ;
    $exts = split("[/\\.]", $filename) ;
    $n = count($exts)-1;
    $exts = $exts[$n];
    return $exts;
    } 
    //This if statement assigns the new file name to a variable and displays a message.
    if (file_exists($_FILES["afile"]["tmp_name"]))
      {
      $new = "file1.";
      $type = $_FILES ["afile"] ["type"];
      $size = $_FILES ["afile"] ["size"];
      $error = $_FILES ["afile"] ["error"];
      $name = $_FILES ["afile"] ["name"];
      $tmpname = $_FILES["afile"]["tmp_name"];
      $names = "afile";
      echo "file1 Uploaded <br />";
      }
    elseif (file_exists($_FILES["bfile"]["tmp_name"]))
      {
      $new = "file2.";
      $type = $_FILES ["bfile"] ["type"];
      $size = $_FILES ["bfile"] ["size"];
      $error = $_FILES ["bfile"] ["error"];
      $name = $_FILES ["bfile"] ["name"];
      $tmpname = $_FILES["bfile"]["tmp_name"];
      $names = "bfile";
      echo "file2 Uploaded <br />";
      }
    elseif (file_exists($_FILES["cfile"]["tmp_name"]))
      {
      $new = "file3.";
      $type = $_FILES ["cfile"] ["type"];
      $size = $_FILES ["cfile"] ["size"];
      $error = $_FILES ["cfile"] ["error"];
      $name = $_FILES ["cfile"] ["name"];
      $tmpname = $_FILES["cfile"]["tmp_name"];
      $names = "cfile";
      echo "file3 Uploaded <br />";
      }
    elseif (file_exists($_FILES["dfile"]["tmp_name"]))
      {
      $new = "file4.";
      $type = $_FILES ["dfile"] ["type"];
      $size = $_FILES ["dfile"] ["size"];
      $error = $_FILES ["dfile"] ["error"];
      $name = $_FILES ["dfile"] ["name"];
      $tmpname = $_FILES["dfile"]["tmp_name"];
      $names = "dfile";
      echo "file4 Uploaded <br />";
      }
    elseif (file_exists($_FILES["efile"]["tmp_name"]))
      {
      $new = "file5.";
      $type = $_FILES ["efile"] ["type"];
      $size = $_FILES ["efile"] ["size"];
      $error = $_FILES ["efile"] ["error"];
      $name = $_FILES ["efile"] ["name"];
      $tmpname = $_FILES["efile"]["tmp_name"];
      $names = "efile";
      echo "file5 Uploaded <br />";
      }
    elseif (file_exists($_FILES["ffile"]["tmp_name"]))
      {
      $new = "file6.";
      $type = $_FILES ["ffile"] ["type"];
      $size = $_FILES ["ffile"] ["size"];
      $error = $_FILES ["ffile"] ["error"];
      $name = $_FILES ["ffile"] ["name"];
      $tmpname = $_FILES["ffile"]["tmp_name"];
      $names = "ffile";
      echo "file6 Uploaded <br />";
      }

    //This applies the function to our file
    $ext = findexts ($_FILES ['$names'] ['name']) ;
        //This assigns the subdirectory you want to save into.
        $targett = "forms/upload/";
        //This combines the directory, the new file name, and the extension
        $target = $targett . $new.$ext; 
        // makes sure image meets specs
    if ((($type == "image/gif")
    || ($type == "image/jpeg")
    || ($type == "image/pjpeg"))
    && ($size < 2000000))
      {
      if ( $error > 0)
        {
        echo "Return Code: " . $error . "<br />";
        }
      else
        {
        echo "Thank You! <br />";
        }
    //saves uploaded file
        if (file_exists("forms/upload/" . $name))
          {
          move_uploaded_file( $tmpname, $target);
          echo $name . " Old File Over Written. ";
          }
        else
          {
          move_uploaded_file( $tmpname, $target);
          echo "Stored in: " . "forms/upload/" . $name;
          }   
?> 

也许我的 elseif 语句被破坏了?

我只玩这个 php 的东西大约一个星期,所以如果我在这里偏离了标记,很抱歉浪费你的时间。

也许如果我尝试使用全局变量创建 6 个函数而 move_uploaded_file($_FILES["file"]["tmp_name"], $target);不是将它们放在 elseif 语句中它可以工作?

这是工作 php 页面的副本

<?php
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
} 
//This applies the function to our file
$ext = findexts ($_FILES['afile']['name']) ;
    //This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
    $new = "file1.";
    //This assigns the subdirectory you want to save into... make sure it exists!
    $targett = "forms/upload/";
    //This combines the directory, the random file name, and the extension
    $target = $targett . $new.$ext; 
if ((($_FILES["afile"]["type"] == "image/gif")
|| ($_FILES["afile"]["type"] == "image/jpeg")
|| ($_FILES["afile"]["type"] == "image/pjpeg"))
&& ($_FILES["afile"]["size"] < 2000000))
  {
  if ($_FILES["afile"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["afile"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["afile"]["name"] . "<br />";
    echo "Type: " . $_FILES["afile"]["type"] . "<br />";
    echo "Size: " . ($_FILES["afile"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["afile"]["tmp_name"] . "<br />";

    if (file_exists("forms/upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["afile"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["afile"]["tmp_name"], $target);
      echo "Stored in: " . "forms/upload/" . $_FILES["afile"]["name"];
      }
    else
      {
      echo "Invalid file";
      }
4

4 回答 4

1

那是一大堆重复的代码!你真的需要 6 个单独的 HTML 表单吗?为什么没有一种形式,带有用于选择事件的下拉菜单?

<select name="event">
    <option value="afile">Event One</option>
    <option value="bfile">Event Two</option>
    ...etc
</select>

对于输入,使用:

<input type="file" name="userfile" id="file" />

然后,不要使用冗长的 if/else 子句,只需执行以下操作:

if ( isset($_POST['event']) && file_exists($_FILES['userfile']['tmp_name']) )
{
    // base this code on the value of $_POST['event']
}
于 2009-08-24T13:44:10.627 回答
0

也许你应该使用

isset($_FILES["afile"])

代替

file_exists($_FILES["afile"]["tmp_name"])

- 编辑

在标题中回答您的问题:如果设置了 $_FILES["afile"],则有可能。这样你的新变量中就会有一个数组。

于 2009-08-24T13:25:42.693 回答
0

非常接近实际工作。

//Uses $names as a key in $_FILES - no single quotes for variable expansion....
$ext = findexts($_FILES[$names]['name']);

我是否还建议使用某种循环而不是带有一堆粘贴代码的大 elseif。使用了两个改进:$_FILES 数组上的foreach和提取

foreach ($_FILES as $fileKey => $values)
{
  // Sets $type, $size, $name, $tmp_name, $error
  extract($values);
  //This applies the function to our file
  $ext = findexts($name) ;

  //This assigns the subdirectory you want to save into.
  $targett = "forms/upload/";
  //This combines the directory, the new file name, and the extension
  $target = $targett . $new.$ext; 
  // makes sure image meets specs
  if ((($type == "image/gif")
    || ($type == "image/jpeg")
    || ($type == "image/pjpeg"))
    && ($size < 2000000))
  {
    if ($error > 0)
    {
      echo "Return Code: " . $error . "<br />";
    }
    else
    {
      echo "Thank You! <br />";
    }
    //saves uploaded file
    if (file_exists("forms/upload/" . $name))
    {
      move_uploaded_file( $tmp_name, $target);
      echo $name . " Old File Over Written. ";
    }
    else
    {
      move_uploaded_file( $tmp_name, $target);
      echo "Stored in: " . "forms/upload/" . $name;
    }         
  }
}
于 2009-08-24T13:29:19.787 回答
0

我只是想感谢所有为我的问题留下答案的人,我对我的问题的短响应时间感到非常惊讶。在尝试并测试了您的答案之后,我仍然没有找到任何类型的真正解决方案来解决我的问题,直到我发现我忘记结束一个 if 语句,我才开始在答案方面取得进展。

 if ((($type == "image/gif")
    || ($type == "image/jpeg")
    || ($type == "image/pjpeg"))
    && ($size < 2000000))
      {

既然这个 if 语句有一个结尾,我有时间研究你所有的答案,我相信我很快就会让这个页面按照我的喜好工作。它现在正在工作,但它们仍有待改进。这是我的工作 html 和 php 页面。

HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!-- 
html admin page for HH 
-->
<title>Administartor Panel</title>
<head>
<link rel="stylesheet" type="text/css" href="admin.css" />
</head>
<body>
<h1>Heritage House Administartor Panel</h1>
<br/>
<table>
<tr>
<td id="eone">
<br/>
<p>Event Uploader</P>
<form action="formupload.php" method="post"
enctype="multipart/form-data">
<label for="selection">Please select the event you would like to edit.</label>
<select name="selection">
<option value="file1">Event1</option>
<option value="file2">Event2</option>
<option value="file3">Event3</option>
<option value="file4">Event4</option>
<option value="file5">Event5</option>
<option value="file6">Event6</option>
</select>
<br />
<p> Please upload your event picture file here.</p>
<p align="center">
<label for="file">Photo:</label>
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Upload" />
</p>
</form>
<form action="WriteTxt1.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for the event artical:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>

</form>
</td>
</tr>
</body>
</html>

PHP

    //This function separates the extension from the rest of the file name and returns it
    function findexts ($filename)
    {
    $filename = strtolower($filename) ;
    $exts = split("[/\\.]", $filename) ;
    $n = count($exts)-1;
    $exts = $exts[$n];
    return $exts;
    } 
    //This if statement assigns the new file name to a variable from the value of the options menu in the html form selection.
 $new = $_POST ["selection"] . "."; 
    //This applies the function to our file
    $ext = findexts ($_FILES ["file"] ["name"]) ;
        //This assigns the subdirectory you want to save into.
        $targett = "forms/upload/";
        //This combines the directory, the new file name, and the extension
        $target = $targett . $new.$ext; 
        // makes sure image meets specs
    if ((($_FILES ["file"] ["type"] == "image/gif")
    || ($_FILES ["file"] ["type"] == "image/jpeg")
    || ($_FILES ["file"] ["type"] == "image/pjpeg"))
    && ($_FILES ["file"] ["size"]< 2000000))
      {
      echo "file check pass<br />";
      }
      //makes an error if file does not meet standards 
      if ( $_FILES ["file"] ["error"] > 0)
        {
        echo "Return Code: " . $_FILES ["file"] ["error"] . "<br />";
        }
      else
        {
        echo "Thank You! <br />";
        }
    //saves uploaded file
        if (file_exists("forms/upload/" . $_FILES ["file"] ["name"]))
          {
          move_uploaded_file( $_FILES ["file"]["tmp_name"], $target);
          echo $_FILES ["file"] ["name"] . " Old File Over Written. ";
          }
        else
          {
          move_uploaded_file( $_FILES["file"]["tmp_name"], $target);
          echo "Stored in: " . "forms/upload/" . $_FILES ["file"] ["name"];
          }   
?>
<html>
<!-- 
html to return user to admin page 
-->
<title>
Upload Result
</title>
<head>
<p>
<br />
Back To Admin Page
</p>
</head>
<body>
<form action="">
<a href="adminarea.html">
<input type="button" value="Back" />
</a>
</form>
</body>
</html>

注意:我只使用 notepad2 和 firefox 来编辑和错误检查我的代码。我猜他们是调试php的更好方法吗?

于 2009-08-26T19:00:03.283 回答