动态创建的输入类型文件不移动上传的文件[关闭]

问题描述 投票:-1回答:1

我创建了一个动态上传表单,用户点击添加更多表单并创建新的输入类型字段

< script src = "//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" > < /script> <
script type = "text/javascript" >
  var counter = 0;
$(function() {
  $('p#add_field').click(function() {
    counter += 1;
    $('#container').append(
      '<strong>ಇತರೆ ದಾಖಲಾತಿಗಳು. ' + counter + '</strong><br />' +
      '<input id="field_' + counter + '" name="dynfields[]' + '" type="file" /><br />');

  });
}); <
/script>
<div id="container" class="control-group">
  <div class="controls">
    <p id="add_field"><a href="#"><span>+ Click To Add</span></a></p>
  </div>
</div>

我的PHP代码

    if (!empty($_POST['dynfields'])) {
foreach ( $_POST['dynfields'] as $key=>$value ) {
$values = mysql_real_escape_string($value);
$file1 = rand(1000,100000)."-".$_FILES['values']['name'];
 $file_loc = $_FILES['values']['tmp_name'];
 $file_size = $_FILES['values']['size'];
 $file_type = $_FILES['values']['type'];
 $folder="../uploads/Dodballapur_PDF/";

 move_uploaded_file($file_loc,$folder.$file1);
$query = mysql_query("INSERT INTO other_files_dod (id,file1,type,size,Upload_Date1) VALUES ('','$file1','$file_type','$file_size',NOW())" );

//}
}

我无法分析我在这里犯的错误,需要帮助。 1)无法将上传的文件移动到上述文件夹位置。 2)数据库也未插入

我传递了错误的文件名吗?

php html mysql
1个回答
0
投票

最终工作代码

< script src = "//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" > < /script>

  <
  script type = "text/javascript" >
  $(document).ready(function() {
    var maxField = 3; //Input fields increment limitation
    var addButton = $('.add_button'); //Add button selector
    var wrapper = $('.field_wrapper'); //Input field wrapper
    var fieldHTML = '<div><input type="file" name="dynfields[]" value=""/><a href="javascript:void(0);" class="remove_button" title="Remove field"><font color="red"><p id="remove_field"><span><strong>X Remove</strong></span></p></font></a></div>'; //New input field html 
    var x = 1; //Initial field counter is 1
    $(addButton).click(function() { //Once add button is clicked
      if (x < maxField) { //Check maximum number of input fields
        x++; //Increment field counter
        $(wrapper).append(fieldHTML); // Add field html
      }
    });
    $(wrapper).on('click', '.remove_button', function(e) { //Once remove button is clicked
      e.preventDefault();
      $(this).parent('div').remove(); //Remove field html
      x--; //Decrement field counter
    });
  }); <
/script>
<div id="container" class="field_wrapper">
  <div class="controls">
    <!-- <input type="text" name="field_name[]" value=""/> -->
    <a href="javascript:void(0);" class="add_button" title="Add field">
      <p id="add_field"><span><strong>+ ಇತರೆ ದಾಖಲಾತಿಗಳು</strong></span></p>
    </a>
  </div>
</div>

我的PHP脚本

`

if(isset($_FILES['dynfields'])){
        $errors= array();
        foreach($_FILES['dynfields']['tmp_name'] as $key => $tmp_name ){
            $file_name = $key.$_FILES['dynfields']['name'][$key];
            $file_size =$_FILES['dynfields']['size'][$key];
            $file_tmp =$_FILES['dynfields']['tmp_name'][$key];
            $file_type=$_FILES['dynfields']['type'][$key];  
            if($file_size > 10097152){
                $errors[]='File size must be less than 2 MB';
            }       
            $query="INSERT INTO other_files_dod (id,file1,type,size,Upload_Date1) VALUES ('','$file_name','$file_type','$file_size',NOW())";
            $desired_dir="../uploads/Dodballapur_PDF/";
            if(empty($errors)==true){
                if(is_dir($desired_dir)==false){
                    mkdir("$desired_dir", 0700);        // Create directory if it does not exist
                }
                if(is_dir("$desired_dir/".$file_name)==false){
                    move_uploaded_file($file_tmp,"../uploads/Dodballapur_PDF/".$file_name);
                }else{                                  //rename the file if another one exist
                    $new_dir="../uploads/Dodballapur_PDF/".$file_name.time();
                     rename($file_tmp,$new_dir) ;               
                }
                mysql_query($query);            
            }else{
                    print_r($errors);
            }
        }
        if(empty($error)){
            echo "Success";
        }
    }

`

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