如何用php正确上传文件?

时间:2018-10-06 16:06:47

标签: php html5

所以我已经在这个学校项目上工作了大约一个星期,然后遇到一个实例,在这个实例中,我必须实施一个用户注册表单,该表单应该提交用户信息以及其路径的用户个人资料图片也要存储在数据库中。缺点是我似乎无法使上传脚本正常工作。我已经看了数十个教程,并从这个平台上阅读了类似问题的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>

在这里,我的PHP函数可以收到FILE['image']参数。

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

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

3 个答案:

答案 0 :(得分:0)

如我所见,文件的输入字段名称为image。因此,您应该将文件对象传递给调用函数$_FILES

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

查看您的输入文件字段

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

,然后在尝试将图像上传到的位置更改代码为:

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

希望它会对您有所帮助。

答案 1 :(得分: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);

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

答案 2 :(得分: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'];

所以纠正它!

为您加油!