简体   繁体   English

PHP文件未将文件放入文件夹

[英]PHP file not putting file into folder

I have tried numerous attempts at this code, i know the html is correct, but the PHP is tricky 我已经尝试过多次尝试此代码,我知道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>

help me fix this 帮我解决这个问题

Hey try this simple code, I think this will help you, and add your code as per your requirement. 嘿,尝试这个简单的代码,我认为这将对您有所帮助,并根据您的要求添加代码。

<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);
    ?>
  1. To start, your assumption that the HTML is correct is wrong. 首先,您认为HTML正确的假设是错误的。 File upload forms need to be method="POST" 文件上传表单必须为method =“ POST”

  2. One of your form elements needs to be a file picker: 您的表单元素之一必须是文件选择器:

     <input type="file" name="file"> 
  3. Your opening form tag needs an extra parameter: 您的开始表单标签需要一个额外的参数:

     <form method="post" enctype="multipart/form-data"> 
  4. Your PHP should only run if the form has been POSTED, in which case the variable $_POST is set. 仅当表单已过POST时,PHP才应运行,在这种情况下,将设置变量$ _POST。 So put your php code inside a 因此,将您的php代码放入

     if($_POST) { //php here } 

may be your uploading directory is readonly or write protected 可能是您的上传目录为只读或受写保护

<?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"];
     }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM