I am working on a WordPress website which allows users to upload files upon registration. I got the upload and registration portion working. Below is the code that handles the upload once the user clicks the register button:
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'];
}
}
The wp_handle_upload moves the file the user uploads into the C:\wamp64\www\wordpress\wp-content\uploads\2018\07\imgname.png directory.
now my concern here is:
if 2 users register and they both upload the file imgname.png, it will rename the second "imgname.png" to "imgname-1.png". This is perfectly fine but I am trying to also store a reference to these images in the database, how would I be able to know if the image got renamed or not?
Do you guys have any suggestions for this?
Example: UserA signing up uploads a file called "test.png", i want to create a record in the db like this:
|UserA|localhost\wordpress\wp-content\uploads\2018\07\test.png.|
Now UserB signing up uploads a file called "test.png", his file will be renamed "test-1.png". Therefore I would need to create a record in the DB for him as the following:
|UserA|localhost\wordpress\wp-content\uploads\2018\07\test-1.png.|
How would I get the directory/image name of the file if the change to "test 1.png" happened after it was uploaded?
Any suggestions?