PHP文件没有将文件放入文件夹

时间:2022-10-08 17:22:01

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

帮我解决这个问题

3 个解决方案

#1


0  

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

#2


0  

  1. To start, your assumption that the HTML is correct is wrong. File upload forms need to be method="POST"

    首先,您认为HTML正确是错误的。文件上传表单需要是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. So put your php code inside a

    您的PHP只应在表单已POSTED时运行,在这种情况下设置变量$ _POST。所以把你的PHP代码放在一个

    if($_POST) {
        //php here
    }
    

#3


0  

may be your uploading directory is readonly or write protected

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

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

#1


0  

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

#2


0  

  1. To start, your assumption that the HTML is correct is wrong. File upload forms need to be method="POST"

    首先,您认为HTML正确是错误的。文件上传表单需要是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. So put your php code inside a

    您的PHP只应在表单已POSTED时运行,在这种情况下设置变量$ _POST。所以把你的PHP代码放在一个

    if($_POST) {
        //php here
    }
    

#3


0  

may be your uploading directory is readonly or write protected

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

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