【问题标题】:Image - Upload not responding, no access to $_FILES图片 - 上传无响应,无法访问 $_FILES
【发布时间】:2014-03-14 08:48:18
【问题描述】:

这是我的文件上传脚本,我收到以下错误

Notice: Undefined index: fupload in C:\Users\Tuskar\Desktop\Projekt\htdocs\Project IT-Space\Profile\edit_profile_parse.php on line 8

但根据不应该有错误,因为我确定了索引。似乎我无权访问 $_FILES 数组,因为在我收到此错误之前,我已经收到了其他类似的错误,或者程序完全通过了 if 并直接转到 else(未选择文件)

我知道脚本是原始的并且几乎不包含任何安全性,但我只是希望它在我添加其他功能(如最大文件大小或文件限制)之前先工作...... :(

这是我正在使用的代码。

Upload Picture
<form action="edit_profile_parse.php" method="get" enctype="multipart/form-data" >
<input type="hidden" name="MAX_FILE_SIZE" value="999999999"> </input>
<input type="file" name="fupload"> </input>
<input type="submit" name="submit" value="Upload"> </input>
</form>

这是处理表单的php

if (isset( $_GET['submit'] ))
{
if (isset($_FILES['fupload'] ))
{
echo "name: ".$_FILES['fupload']['name']." <br> ";
echo "size: ".$_FILES['fupload']['sizw']." <br> ";
echo "type: ".$_FILES['fupload']['type']." <br> ";

    if ($_FILES['fupload']['type'] == "image/gif")
    {
        $source = $_FILES['fupload']['tmp_name'];
        $target = "images/" .$_FILES['fupload']['name'];
        move_uploaded_file($source, $target) or die ("Error: " .mysql_error());
        $size = getImageSize($target);

        $imgstr = "<img src=\" '".$target."' \">";
        echo $imgstr;
    }
    else
    {
    echo "Problem uploading the file ... ";
    }
}   
else
{
echo "No file chosen !! ";
}
}
else
{
echo "Button not clicked ";
}

【问题讨论】:

    标签: php html mysql image file


    【解决方案1】:

    你应该使用表单方法来POST而不是get。

    <form action="edit_profile_parse.php" method="post" enctype="multipart/form-data" >
    

    【讨论】:

    • 非常感谢 :) 哇,因为只有一种该死的方法,所以在“调试”上投入了这么多时间:D
    【解决方案2】:

    确保您的 FORM 标记具有 method="POST"。 GET 请求不支持多部分/表单数据上传。

    【讨论】:

      【解决方案3】:

      我希望这可行: 形式:

      <form action="edit_profile_parse.php" method="post" enctype="multipart/form-data" >
          <input type="hidden" name="MAX_FILE_SIZE" value="999999999"> </input>
          <input type="file" name="fupload"> </input>
          <input type="submit" name="submit" value="Upload"> </input>
      </form>
      

      php 文件:

      <?php
      if($_POST) {
          $max_size = mysql_real_escape_string(strip_tags($_POST['MAX_FILE_SIZE']));
          $file = $_FILES['fupload']['name'];
      
          if(isset($max_size) && !empty($max_size) && !empty($file)) {
              $file_type = $_FILES['fupload']['type'];
              $tmp = $_FILES['fupload']['tmp_name'];
              $file_size = $_FILES['fupload']['size'];
      
              $allowed_type = array('image/png', 'image/jpg', 'image/jpeg', 'image/gif');
      
              if(in_array($file_type, $allowed_type)) {
                  if($file_size < $max_size) {
                      $path = 'images/'.$file;
      
                      move_uploaded_file($tmp, $path);
                      //if you want to store the file in a db use the $path in the query
                  } else {
                      echo 'File size: '.$file_size.' is too big';
                  }
              } else {
                  echo 'File type: '.$file_type.' is not allowed';
              }
          } else {
              echo 'There are empty fields';
          }
      }
      ?>
      

      【讨论】:

      • 这还包括一些额外的安全性
      【解决方案4】:

      上传图片

      <form action="edit_profile_parse.php" method="POST" enctype="multipart/form-data" >
      <input type="hidden" name="MAX_FILE_SIZE" value="999999999"> </input>
      <input type="file" name="fupload"> </input>
      <input type="submit" name="submit" value="Upload"> </input>
      </form>  
      

      PHP 文件

       <?php 
          if (isset( $_POST['submit'] ))
          {
          if (isset($_FILES['fupload'] ))
          {
          echo "name: ".$_FILES['fupload']['name']." <br> ";
          echo "size: ".$_FILES['fupload']['size']." <br> ";
          echo "type: ".$_FILES['fupload']['type']." <br> ";
      
              if ($_FILES['fupload']['type'] == "image/gif")
              {
                  $source = $_FILES['fupload']['tmp_name'];
                  $target = "images/" .$_FILES['fupload']['name'];
                  move_uploaded_file($source, $target) or die ("Error: " .mysql_error());
                  $size = getImageSize($target);
      
                  $imgstr = "<img src=\" '".$target."' \">";
                  echo $imgstr;
              }
              else
              {
              echo "Problem uploading the file ... ";
              }
          }   
          else
          {
          echo "No file chosen !! ";
          }
          }
          else
          {
          echo "Button not clicked ";
          }
          ?>
      

      【讨论】:

        猜你喜欢
        • 2013-02-16
        • 2020-08-21
        • 1970-01-01
        • 1970-01-01
        • 2019-05-31
        • 2020-11-27
        • 2023-03-16
        • 2020-03-21
        • 1970-01-01
        相关资源
        最近更新 更多