1

我在这段代码上尝试了很多次,我知道 html 是正确的,但是 PHP 很棘手

<html>
<body>
<?php
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
$uploaddir = '/var/www/Megan/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
move_uploaded_file($_FILES["file"]["tmp_name"],
$uploadfile);
echo "Stored in: " . "$uploaddir" . $_FILES["file"]["name"];
?>
<h1>File upload successful!</h1>
<form method="get" action="/megan">
<input type="submit" value="Continue"/>
</form>
</body>
</html>

帮我解决这个问题

4

3 回答 3

0

嘿,试试这个简单的代码,我认为这会对您有所帮助,并根据您的要求添加您的代码。

<form enctype="multipart/form-data" method="post">
 <input type="file" name="file" />
 <input type="submit" name="submit" value="upload" />
 </form>
<?php 
      $name=$_FILES['file']['name'];
      $temp=$_FILES['file']['tmp_name'];
      $dir="var/www/Megan/";
      move_uploaded_file($temp,$dir.$name);
    ?>
于 2013-11-01T05:20:45.453 回答
0
  1. 首先,您对 HTML 正确的假设是错误的。文件上传表单需要method="POST"

  2. 您的表单元素之一需要是文件选择器:

    <input type="file" name="file">
    
  3. 您的开始表单标签需要一个额外的参数:

    <form method="post" enctype="multipart/form-data">
    
  4. 您的 PHP 应该仅在表单已发布的情况下运行,在这种情况下设置了变量 $_POST。所以把你的php代码放在一个

    if($_POST) {
        //php here
    }
    
于 2013-11-01T05:24:45.367 回答
0

可能是您的上传目录是只读的或写保护的

<?php
     echo "Upload: " . $_FILES["file"]["name"] . "<br>";
     echo "Type: " . $_FILES["file"]["type"] . "<br>";
     echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
     echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

     $uploaddir = '/var/www/Megan/';
     $uploadfile = $uploaddir . basename($_FILES['file']['name']);
     chmod($uploadfile, 0777);

     if($_FILES['file']['error']==0) {
         if(move_uploaded_file($_FILES["file"]["tmp_name"], $uploadfile)){
            echo "Stored in: " . "$uploaddir" . $_FILES["file"]["name"];
         } else {
            echo "error!!";
         }
     } else {
            echo "An error has occurred.<br/>Error Code: " . $_FILES["file"]["error"];
     }
?>
于 2013-11-01T05:30:12.890 回答