Rename and overwrite uploaded files

限于喜欢 提交于 2019-12-23 05:49:22

问题


I use the following code to upload images to Wordpress

function custom_upload_name($filename)
{
  $info = pathinfo($filename);

 $item_id = $_POST['item_id'];    
  $filename  =  $item_id . '.jpg';
     return $filename;
 }

add_filter('sanitize_file_name', 'custom_upload_name', 10);

if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
 $item_id = $_POST['item_id'];
$uploadedfile = $_FILES['file'];
  $image_name = $item_id;
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile ) {
    //file is uploaded successfully. do next steps here.
      echo $location_home .'/images/' .$image_name . '.jpg';  
}

The renaming of the files works correctly. But I select the same image twice to upload everything is fine, but if I select a different image to upload, Wordpress automatically adds an number to the file:

  • Result on first upload: 10000.jpg
  • Result on second upload (same file, with same id): 100001.jpg
  • Result on third upload (same file, with same id): 1000012.jpg

How to let Wordpress automatically overwrite the files with the same id, without adding extra numbers to the file?


回答1:


Use this code, unique_filename_callback is call back function, this function automatically called by wordpress. So we can overwrite the files with same name.

$upload_overrides = array( 'test_form' => false,'unique_filename_callback' => 'my_cust_filename' );

// You codes

function my_cust_filename($dir, $name, $ext){
    return $name.$ext;
}



回答2:


This is how you would upload pdf or image file with random name

<?php
function custom_upload_name($filename)
{
$info = pathinfo($filename);
echo  'info:'. $info . "\n";

$ext=$info['extension'];
  $filename  =  rand(1,1000) .".".$ext;

echo $filename . "\n";
     return $filename;
 }



if ( ! function_exists( 'wp_handle_upload' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
}

if(isset($_FILES['fileToUpload']))
{
$uploadedfile = $_FILES['fileToUpload'];
echo $uploadedfile[name].' '. $uploadedfile[type].' '.$uploadedfile[tmp_name].' ';
$_FILES['fileToUpload']=null; //removing file

// *********************** Overriding parameter ***********************
$upload_overrides = array( 'test_form' => false, 'mimes' => array(// Image formats
    'jpg|jpeg|jpe'                 => 'image/jpeg',
    'gif'                          => 'image/gif',
    'png'                          => 'image/png',
    'bmp'                          => 'image/bmp',
    'tif|tiff'                     => 'image/tiff',
    'ico'                          => 'image/x-icon',
//pdf
        'pdf' => 'application/pdf'
) 
);
add_filter('sanitize_file_name', 'custom_upload_name', 10);
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );

if ( $movefile && ! isset( $movefile['error'] ) ) {
    echo "File is valid, and was successfully uploaded.\n";
    var_dump( $movefile );
} else {
    /**
     * Error generated by _wp_handle_upload()
     * @see _wp_handle_upload() in wp-admin/includes/file.php
     */
    echo $movefile['error'];
}
}
?>

This is the HTML

<form action="#" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload file" name="submit">
</form>



回答3:


I found hack : rename the file before wp_handle_upload.

$_FILES['myfile']['name'] = uniqid( 'file' ) . '.' . pathinfo( $_FILES['myfile']['name'] ) ['extension'];

$upload_overrides = [ 'test_form' => false ];

$movefile = wp_handle_upload( $_FILES['myfile']['name'], $upload_overrides );


来源:https://stackoverflow.com/questions/23382934/rename-and-overwrite-uploaded-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!