我使用以下代码将图像上传到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';
}
重命名文件可以正常工作。但我选择相同的图像两次上传一切都很好,但如果我选择一个不同的图像上传,Wordpress会自动为该文件添加一个数字:
如何让Wordpress自动覆盖具有相同ID的文件,而无需在文件中添加额外的数字?
答案 0 :(得分:2)
使用此代码,unique_filename_callback
是回调函数,此功能由wordpress自动调用。所以我们可以覆盖同名的文件。
$upload_overrides = array( 'test_form' => false,'unique_filename_callback' => 'my_cust_filename' );
// You codes
function my_cust_filename($dir, $name, $ext){
return $name.$ext;
}
答案 1 :(得分:1)
可悲的是,我没有足够的声誉来发表评论,但接受的解决方案是错误的。
函数already has dot in it中的this.navController.push(SearchPage).then(() => {
const index = this.viewCtrl.index;
this.navController.remove(index, 1); //this will remove page3 and page2
});
参数,因此正确的代码为:
$ext
答案 2 :(得分:0)
这是您使用随机名称
上传pdf或图像文件的方法<?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'];
}
}
?>
这是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 :(得分:0)
我发现了hack:在wp_handle_upload之前重命名文件。
URL obj = new URL("https://3d.login");
URLConnection conn = obj.openConnection();
System.out.println(conn.getHeaderFields());
答案 4 :(得分:0)
如果其他人有同样的问题,请发布此内容。
由于某种原因,如果我将其传递给 wp_handle_upload
,则覆盖不起作用,如下所示:
$overrides['unique_filename_callback'] = 'my_unique_filename_callback';
wp_handle_upload($upload_file, $overrides)
但是,当我尝试使用 wp_handle_upload_overrides
过滤器修改它时,以下代码有效,如下所示:
function wp_handle_upload_overrides($overrides, $file) {
//$overrides['test_form'] = false;
$overrides['unique_filename_callback'] = 'my_unique_filename_callback';
return $overrides;
}
add_filter( 'wp_handle_upload_overrides', 'wp_handle_upload_overrides', 10, 2 );