在添加/删除动态字段中上传时如何重命名图像

时间:2018-07-01 08:21:34

标签: javascript php

我正在创建一个项目,在其中创建了几个“动态添加/删除”字段。一切正常,没有任何问题。

我已经创建了添加/删除图像文件字段。当我上传图片时,它会完美上传。但我希望使用rand()函数对这些图像进行重命名。

<?php

if ( isset( $_POST['project_draft'] ) ) {

$member_details->member_image = array_map( 'sanitize_file_name', $_FILES['member_image']['name'] );
$member_details->member_name = array_map( 'sanitize_text_field', $_POST['member_name'] );
$member_details->member_role = array_map( 'sanitize_text_field', $_POST['member_role'] );
$member_details->member_email = array_map( 'sanitize_text_field', $_POST['member_email'] );
$member_details->member_facebook_id = array_map( 'sanitize_text_field', $_POST['member_facebook_id'] );

$member_details_encode = wp_json_encode( $member_details );

  global $wpdb;

  $members_result = $wpdb->insert( 'wpxa_project_members',

         array( 

              'project_id'     => $_SESSION['project_id'],
              'author_id'      => $post_author,
              'project_members' => $member_details_encode

              ),

         array( 

              '%d',
              '%d',
              '%s'

      )

    );

for($i=0; $i < count( $_FILES['member_image']['name'] ); $i++) {

$profile_image_folder = "profile-images/";
$profile_image_name = $_FILES['member_image']['name'][$i];
$profile_image_path = trim( $profile_image_folder . basename( $profile_image_name ) );
$profile_image_temp = $_FILES['member_image']['tmp_name'][$i];

move_uploaded_file( $profile_image_temp, $profile_image_path );

}

}

?>

<form method="POST" enctype="multipart/form-data">

    <div class="panel panel-default">

      <div class="panel-heading"><center><b>Team Members</b></center></div>

      <div class="panel-body">

    <div class="row">

      <div class="col-md-4">

      <div class="thumbnail">

        <img src="<?php echo esc_url( site_url('/img/blank-image.png') ); ?>" id="member_image0" alt="">

      </div>

      <div class="form-group">

        <label class="btn btn-success btn-block btn-file">Add Image<input type="file" name="member_image[]" onchange="preview_member(event, 0)" style="display: none;"></label>

      </div>

      </div>

      <div class="col-md-8">

      <div class="form-group">
        <label for="member_name">Member Name <b style="color:#FF0000;">*</b></label>
        <input type="text" class="form-control" name="member_name[]" placeholder="">
      </div>

      <div class="form-group">
        <label for="member_role">Role in Project <b style="color:#FF0000;">*</b></label>
        <input type="text" class="form-control" name="member_role[]" placeholder="">
      </div>

      <div class="form-group">
        <label for="member_email">Email address <b style="color:#FF0000;">*</b></label>
        <input type="email" class="form-control" name="member_email[]" placeholder="">
      </div>

      <div class="form-group">
        <label for="member_facebook_id">Facebook Username <b style="color:#FF0000;">*</b></label>
        <input type="text" class="form-control" name="member_facebook_id[]" placeholder="">
      </div>          

      </div>

    </div>

    <button type="button" class="btn btn-success btn-block" id="add-member-fields"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add Member</button>

    <br>

    <div id="member-fields">

    </div>

      </div>

    </div>

<input class="btn btn-warning btn-block" type="submit" name="project_draft" value="Draft" style="border-radius: 0px;">

</form>

    <script type='text/javascript'>

    var i = 0;

    function preview_member(event, inp) {
      var reader = new FileReader();
      console.log(inp);
      reader.onload = function() {
        var output = document.getElementById("member_image" + inp);
        output.src = reader.result;
      };

      reader.readAsDataURL(event.target.files[0]);
    }

    jQuery(document).ready(function($) {
      //fadeout selected item and remove
      $(document).on("click", "#remove-member-fields", function(event) {
        event.preventDefault();
        $(this)
          .parent()
          .fadeOut(300, function() {
            $(this).empty();
            return false;
          });
      });

      //add input
      $("#add-member-fields").click(function() {
        i++;

      var rows = `<div class="project-members-fields"><div class="row"><div class="col-md-4"><div class="thumbnail"><img src="<?php echo esc_url( site_url('/img/blank-image.png') ); ?>" id="member_image${i}" alt=""></div><div class="form-group"><label class="btn btn-success btn-block btn-file">Add Image<input type="file" name="member_image[]" onchange="preview_member(event, ${i})" style="display: none;"></label></div></div><div class="col-md-8"><div class="form-group"><label for="member_name">Member Name <b style="color:#FF0000;">*</b></label><input type="text" class="form-control" name="member_name[]" placeholder=""></div><div class="form-group"><label for="member_role">Role in Project <b style="color:#FF0000;">*</b></label><input type="text" class="form-control" name="member_role[]" placeholder=""></div><div class="form-group"><label for="member_email">Email address <b style="color:#FF0000;">*</b></label><input type="email" class="form-control" name="member_email[]" placeholder=""></div><div class="form-group"><label for="member_facebook_id">Facebook Username <b style="color:#FF0000;">*</b></label><input type="text" class="form-control" name="member_facebook_id[]" placeholder=""></div></div></div><button type="button" class="btn btn-danger btn-block" id="remove-member-fields"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Remove Member</button><br></div></div></div>`;

        $(rows)
          .fadeIn("slow")
          .appendTo("#member-fields");
        return false;
      });
    });

    </script>

3 个答案:

答案 0 :(得分:0)

我主要用这个:

/*Upload image*/
$target_dir = "profile-images/";
$temp = explode(".",$_FILES["member_image"]["name"][$i]);
$target_file = $target_dir .sha1(uniqid(mt_rand(), true)) . '.' .end($temp);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$upload = move_uploaded_file($_FILES["member_image"]["tmp_name"][$i], $target_file);
/*Upload image*/

$target_file = $target_dir .sha1(uniqid(mt_rand(), true)) . '.' .end($temp);为您完成工作。

答案 1 :(得分:0)

JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
    }
}); 
mnFile.add(mntmExit);

$profile_image_name = $_FILES['member_image']['name'][$i];

为确保此图像(文件名)不存在,您可以这样做:

$profile_image_name = rand(1000,9999) . $_FILES['member_image']['name'][$i];

编辑

do {
    $profile_image_name = rand(1000,9999) . $_FILES['member_image']['name'][$i];
    $profile_image_path = trim( $profile_image_folder . basename( $profile_image_name ) );
}while( is_file( $profile_image_path ) );

答案 2 :(得分:0)

<?php

if ( isset( $_POST['project_draft'] ) ) {

$member_image_names = array_map( 'sanitize_file_name', $_FILES['member_image']['name'] );

foreach ( $member_image_names as $member_image_name ) {

    $member_image_newname[] = get_current_user_id() . "_" . time() . "_" . rand() . "_" . $member_image_name;

}

$member_details->member_image = $member_image_newname;
$member_details->member_name = array_map( 'sanitize_text_field', $_POST['member_name'] );
$member_details->member_role = array_map( 'sanitize_text_field', $_POST['member_role'] );
$member_details->member_email = array_map( 'sanitize_text_field', $_POST['member_email'] );
$member_details->member_facebook_id = array_map( 'sanitize_text_field', $_POST['member_facebook_id'] );

$member_details_encode = wp_json_encode( $member_details );

  global $wpdb;

  $members_result = $wpdb->insert( 'wpxa_project_members',

         array( 

              'project_id'     => $_SESSION['project_id'],
              'author_id'      => $post_author,
              'project_members' => $member_details_encode

              ),

         array( 

              '%d',
              '%d',
              '%s'

      )

    );

if ( ! empty( $members_result ) ) {

$push = json_decode($member_details_encode, true);
$count_member = count( $push['member_image'] );

for ( $u = 0; $u < $count_member; $u++ ) {
 $coco = $push['member_image'][$u]; 

$profile_image_folder = "profile-images/";
$profile_image_path = trim( $profile_image_folder . basename( $coco ) );
$profile_image_temp = $_FILES['member_image']['tmp_name'][$u];

move_uploaded_file( $profile_image_temp, $profile_image_path ); 

}

}

}

?>