如何以正确的方式上传文件?

问题描述 投票:-2回答:3

所以我已经在这个学校项目上工作了大约一个星期,然后遇到了一个实例,我必须实现一个用户注册表单,它应该提交用户信息以及用户配置文件图像的路径是也存储在DB中。缺点是我似乎无法使上传脚本正常工作。我已经观看了数十个教程,并从这个平台上阅读了类似问题的15个解决方案,但仍无济于事。我还确保php.ini文件打开了file_upload设置。

这是我的HTML表格

<form style="padding: 1em;" class="ui form small inverted raised segment" action="signup.php" method="POST" enctype="multipart/form-data">
                   <h4 class="ui dividing header">Background Information</h4>
                    <div class="field">
                        <div class="three fields">
                            <div class="field">
                               <input type="text" name="fname" placeholder="Firstname" required> 
                            </div>
                            <div class="field">
                                <input type="text" name="mname" placeholder="Other name">
                            </div>
                            <div class="field">
                                <input type="text" name="lname" placeholder="Lastname" required>
                            </div>
                        </div>
                    </div>
                    <div class="field">
                        <div class="two fields">
                            <div class="field">
                              <select name="gender">
                                  <option value="">Select Gender</option>
                                  <option value="Male">Male</option>
                                  <option value="Female">Female</option>
                                  <option value="Other">Other</option>
                              </select>                              
                            </div>
                            <div class="field">
                                <input type="date" name="dob" required>
                            </div>
                        </div>
                    </div>
                    <div class="ui dividing header">Contact Information</div>
                    <div class="field">
                        <div class="two fields">
                          <div class="field">
                            <input type="tel" name="phone" placeholder="Mobile phone number" required>
                        </div>  
                        <div class="field">
                            <input type="email" name="email" placeholder="Email address" required/>
                        </div>
                        </div>
                    </div>
                    <div class="field">
                        <div class="three fields">
                            <div class="field">
                              <input type="text" name="area" placeholder="Area/Village" required>  
                            </div>
                            <div class="field">
                                <input type="text" name="trad_auth" placeholder="T/A or STA">
                            </div>
                            <div class="field">
                              <select id="district" name="kasungu">
                                  <option value="Kasungu">Kasungu</option>
                              </select>
                            </div>
                        </div>
                        <div class="ui dividing header">Work Details</div>
                        <div class="field">
                            <div class="two field">
                                <div class="field">
                                    <select id="department" name="department">
                                        <option value="">Select Department</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="ui dividing header">Acc Authentication Details</div>
                        <div class="field">
                          <input type="text" name="activation-code" placeholder="Enter admin authentication code (XXX-XXXX-XXXX)" required/>
                        </div>
                        <div class="field">
                          <div class="two fields">
                            <div class="field">
                              <input type="password" name="pass1" placeholder="Create Password" required/>
                            </div>
                            <div class="field">
                              <input type="password" name="pass2" placeholder="Confirm Password" required/>
                            </div>
                          </div>
                        </div>
                        <div class="ui dividing header">Upload Image</div>
                        <div class="field">
                          <div class="two fields">
                            <div class="field">
                               <input type="file" style="display: none;" id="pic" name="image"/>
                              <a type="button" id="upload" class="ui button fluid negative mini"><i class="icon camera"></i></a>
                            </div>
                          </div>

                        </div>
                         <div class="three fields">
                           <div class="field"></div>
                           <div class="field">
                              <button type="submit" name="submit" class="ui fluid mini positive button">
                                Signup &nbsp;<i class="icon user"></i>
                              </button>
                           </div>
                           <div class="field"></div>
                         </div>
                          <div>
                            <?php
                              if(!empty($errorMsg)){
                                echo $errorMsg;
                              }
                            ?>
                          </div>
                    </div>
                </form>

这是我接收FILE['image'] A PARAMETER的PHP功能。

    <?php
function uploadFile($file){
  $newName;
  if(isset($file)){    
    $file_name = $file['name'];
    $file_tmp_loc = $file['tmp_name'];
    $file_size = $file['size'];
    $file_error = $file['error'];

    $ext  = strtolower(end(explode('.', $file_name)));
    $allowed = array('jpg','jpeg','png','gif');

    if(in_array($allowed, $allowed)){
      if($file_error == 0){
        if($file_size > 3000000){
          $file_name_new = uniqid('',true).".".$ext;
          $destination = '../images/users/';
          $destination = $destination.$file_name_new;

          if(move_uploaded_file($file_tmp_loc, $destination)){
            $newName = $destination;
          }else{
            echo "<h1>Failed to move uploaded file</h1>";
          }
        }
      }else{
        echo "file upload failed. ".$file['error'];
      }
    }else{
      echo "Type not allowed!";    }
  }
}
   return $newName;  
?>

在我的其他PHP脚本中,我调用的函数如下:

<?php
$fileName = uploadFile($_FILE['image']);
?>

然后,将返回的新名称存储在MySQL DB中作为其引用

php html5
3个回答
0
投票

你收到文件时必须使用$ _FILES,第一个数组是$ _FILES ['你在输入文件中提到的文件名,如name ='abc''],第二个数组是关于文件名,大小,错误,位置。你也可以通过使用print_r($ _ FILES ['image'])来看到这个;

<input type="file" style="display: none;" id="pic" name="image"/>

        $file_name = $_FILES['image']['name'];
        $file_tmp_loc = $_FILES['image']['tmp_name'];
        $file_size = $_FILES['image']['size'];
        $file_error = $_FILES['image']['error'];

所以纠正它!

为你加油!


0
投票

这是运行代码。

$image_file = $_FILES['school_logo']['name'];
$tmp=$_FILES["school_logo"]["tmp_name"];
$ext = pathinfo($image_file ,PATHINFO_EXTENSION);
$folder="images/school_logo/";
$logos='logo'.'-'.uniqid(date('dmy')).rand().'.'.$ext;
$test=move_uploaded_file($tmp,$folder.$logos);

我希望这段代码对你有所帮助。


0
投票

正如我所看到的,文件的输入字段名称是image。所以你应该将一个文件对象传递给被调用的函数$_FILES

<?php
$fileName = uploadFile($_FILES);
?>

查看输入文件字段

<input type="file" style="display: none;" id="pic" name="image"/>

并更改您尝试上传图片的代码:

$file_name = $file['image']['name'];

希望它会对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.