【问题标题】:Uploading Image [closed]上传图片[关闭]
【发布时间】:2013-06-28 08:55:19
【问题描述】:

我正在尝试上传图片。

 <input name="files" type="file" id="files">
<input type="submit" name="submit" id="submit" value="Add" />

发布数据后,

<?php

if(isset($_POST['submit']))
{
$path="panel/images"."/".$_FILES["file"]["name"];

move_uploaded_file($_FILES["files"]["tmp_name"], $path);
}
?>

但它不会上传文件。

【问题讨论】:

标签: php upload


【解决方案1】:

确保您在form 标签中指定了enctype

<form action="test.php" enctype="multipart/form-data" method="POST">

【讨论】:

    【解决方案2】:

    改变这个:-

    $path="panel/images"."/".$_FILES["file"]["name"];
    

    $path="panel/images"."/". basename($_FILES["files"]["name"]);
    

    您在表单中使用了id="files",但您在 php 代码中使用了“文件”。

    【讨论】:

      【解决方案3】:

      我觉得应该是这样的

      $_FILES["files"]["name"] not $_FILES["file"]["name"]
      

      在你的 $path 中

      【讨论】:

        【解决方案4】:

        你有错误

        if(isset($_POST['submit']))
         {
           $path="panel/images"."/".$_FILES["files"]["name"];
          //This Line Your element name is files not file
        
            move_uploaded_file($_FILES["files"]["tmp_name"], $path);
          }
        

        【讨论】:

          【解决方案5】:

          据我了解,您的问题仅在于文件上传..所以我已经为您简要介绍了 php 和 html 文件..代码中有注释,我试图让事情变得简单。您可以通过 cmets 了解有关代码的想法。如有更多疑问,我们将不胜感激。

           //This is the php code for upload file..
           <?php
           //allowedExts variable is an array consisting of file types that can be supported.we are uploading image so image file extensions are used.
          $allowedExts = array("gif", "jpeg", "jpg", "png");
          //explode function breaks the string, here used to get the file extension, whenever 
          //a dot(.) would be found in the filename,say abc.jpg, the extension jpg would be 
          //retrived.
          $temp = explode(".", $_FILES["file"]["name"]);
          $extension = end($temp);
          if ((($_FILES["file"]["type"] == "image/gif")
          || ($_FILES["file"]["type"] == "image/jpeg")
          || ($_FILES["file"]["type"] == "image/jpg")
          || ($_FILES["file"]["type"] == "image/pjpeg")
          || ($_FILES["file"]["type"] == "image/x-png")
          || ($_FILES["file"]["type"] == "image/png"))
          && ($_FILES["file"]["size"] < 20000)
          && in_array($extension, $allowedExts))
           {
           //if any error in file, display it
           if ($_FILES["file"]["error"] > 0)
          {
          echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
          }
           //else upload the file
          else
          {
          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>";
           //file will be uploaded in the upload directory..but also need to check whether 
           //the directory consists a same filename already or not..if it does contains
           // a file say abc.jpg already..it would display the msg file already exist
          if (file_exists("upload/" . $_FILES["file"]["name"]))
            {
            echo $_FILES["file"]["name"] . " already exists. ";
            }
          //else it will upload it in upload directory, you can name the directory acc to you..
          //but conventionally upload is used as the directory name
          else
            {
            move_uploaded_file($_FILES["file"]["tmp_name"],
            "upload/" . $_FILES["file"]["name"]);
            echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
            }
          }
           }
          else
           {
            echo "Invalid file";
           }
           ?>
          //php file ends here
          //this is the html file..frame it according to wat u need
          <html>
          <body>
          //the most important thing is that you should keep the 
          //enctype(encryption type)=multipart/form-data. This is mandatory for media files
          <form action="upload_file.php" method="post"
          enctype="multipart/form-data">
          <label for="file">Filename:</label>
          <input type="file" name="file" id="file"><br>
          <input type="submit" name="submit" value="Submit">
          </form>
          
          </body>
          </html>
          

          【讨论】:

            猜你喜欢
            • 2012-04-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-12-18
            • 2021-10-25
            • 2015-02-21
            相关资源
            最近更新 更多