我正在WordPress网站上工作,该网站允许用户在注册后上传文件。我已完成上传和注册部分。以下是用户单击注册按钮后处理上传的代码:
function text_domain_woo_save_reg_form_fields($customer_id) {
//First name field
if (isset($_POST['billing_first_name'])) {
update_user_meta($customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']));
update_user_meta($customer_id, 'billing_first_name', sanitize_text_field($_POST['billing_first_name']));
}
//Last name field
if (isset($_POST['billing_last_name'])) {
update_user_meta($customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name']));
update_user_meta($customer_id, 'billing_last_name', sanitize_text_field($_POST['billing_last_name']));
}
// $target_path = "c:/";
// $target_path = $target_path.basename( $_FILES['image_verification']['name']);
// echo " <script>console.log('it hit!!'); </script>";
// echo " <script>console.log('image upload - success! " . $target_path . "' ); </script>";
// if(move_uploaded_file($_FILES['image_verification']['tmp_name'], $target_path)) {
// echo "File uploaded successfully!";
// echo " <script>console.log('image upload - success!'); </script>";
// echo " <script>console.log('image upload - success! " . $target_path . "' ); </script>";
// } else {
// echo "Sorry, file not uploaded, please try again!";
// echo " <script>console.log('image upload - fail!'); </script>";
// echo " <script>console.log('image upload - success! " . $target_path . "' ); </script>";
// }
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
$uploadedfile = $_FILES['image_verification'];
$movefile = wp_handle_upload($uploadedfile, array('test_form' => false));
if ($movefile && !isset($movefile['error'])) {
var_dump($movefile);
} else {
echo $movefile['error'];
}
}
wp_handle_upload将用户上传的文件移动到C:\ wamp64 \ www \ wordpress \ wp-content \ uploads \ 2018 \ 07 \ imgname.png目录中。
现在我的关注点是:
如果2个用户注册并且他们都上传了文件imgname.png,它将把第二个“ imgname.png”重命名为“ imgname-1.png”。完全可以,但是我也尝试将对这些图像的引用存储在数据库中,如何知道该图像是否被重命名?
你们对此有什么建议吗?
示例:UserA注册后上传了一个名为“ test.png”的文件,我想像这样在数据库中创建一条记录:
|UserA|localhost\wordpress\wp-content\uploads\2018\07\test.png.|
现在,UserB注册后将上传一个名为“ test.png”的文件,其文件将被重命名为“ test-1.png”。因此,我需要在数据库中为他创建一条记录,如下所示:
|UserA|localhost\wordpress\wp-content\uploads\2018\07\test-1.png.|
如果文件“ test 1.png”在上传后发生更改,如何获取文件的目录/图像名称?
有什么建议吗?
答案 0 :(得分:0)
要访问新文件名,可以使用php函数basename()并为其指定从wp_handle_upload()函数返回的路径,并将其与上载的文件名进行比较。
$movefile = wp_handle_upload($uploadedfile, array('test_form' => false));
$new_filename = basename( $movefile['file'] );
$filename_was_changed = new_filename !== $_FILES['image_verification']['name'];