【发布时间】:2018-07-31 05:19:17
【问题描述】:
我正在开发一个允许用户在注册时上传文件的 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”。这很好,但我也试图在数据库中存储对这些图像的引用,我怎么知道图像是否被重命名?
你们有什么建议吗?
示例:注册的用户A上传了一个名为“test.png”的文件,我想在数据库中创建一个这样的记录:
|UserA|localhost\wordpress\wp-content\uploads\2018\07\test.png.|
现在注册的用户B上传了一个名为“test.png”的文件,他的文件将被重命名为“test-1.png”。因此,我需要在数据库中为他创建一条记录,如下所示:
|UserA|localhost\wordpress\wp-content\uploads\2018\07\test-1.png.|
如果在上传后更改为“test 1.png”,我将如何获取文件的目录/图像名称?
有什么建议吗?
【问题讨论】:
-
您在拨打
var_dump($movefile);时看到新文件名了吗?如果您调用var_dump( $_FILES['image_verification'] );,您会看到原始文件名吗? -
@JasonB Var_Dump 不断将我重定向到一个空白页面,并打印出一个随机数组,所以我评论了该行。不过,这实际上是一个好主意,我会尝试一下,看看我得到了什么作为输出。如果它给了我新的文件名,它应该正是我需要的时候。感谢您的回复,我还是开发新手,所以我有很多愚蠢的问题哈哈
-
@JasonB 这是我从 var_dump($_FILES['image_verification']) 得到的输出; array(5) { ["name"]=> string(11) "testimg.png" ["type"]=> string(9) "image/png" ["tmp_name"]=> string(14) "/ tmp/phpzkiiSZ" ["error"]=> int(0) ["size"]=> int(1511) } 它不显示新文件的名称(test-1.png)
-
当您说
var_dump( $movefile )将您重定向到一个随机数组时,我假设它实际上是在转储wp_handle_upload函数的结果,该函数是一个关联数组,应该包含新文件名.您也可以发布该通话的结果吗?我希望您能够将$_FILES中的原始文件名与$movefile中的文件名进行比较,以查看在执行wp_handle_upload期间文件名是否已更改 -
@JasonB 是的,你是对的! var_dump($movefile) 确实最终打印了新文件名。这是输出数组(3) { ["file"]=> string(76) "/home/localhost/public_html/wordpress/wp-content/uploads/2018/07/testimg-2.png" ["url" ]=> string(69) "localhost.com/wordpress/wp-content/uploads/2018/07/…" ["type"]=> string(9) "image/png" } "testimg-2" 是新文件的名称。如何从输出中检索此文件名?