我在代码方面遇到了一些麻烦,我希望有人能带来一些启示:)
我在一个类似于 Wordpress 网站内的小型 CRM 的项目上使用 webcamjs。我使用 ACF 创建了一个表单,我们从网站前端填写表单,该表单仅对登录用户可见。
在成员页面中,我需要能够从网络摄像头拍照,到目前为止,我的网络摄像头正在工作并生成 base64 图像。
我现在需要做的是能够将此图像上传到 WordPress 中的上传目录,并使用 ACF 插件将图像的链接附加到“照片”字段,以便所有信息都在一起。但这部分不起作用。
我想知道我是否从 JS 代码中正确调用了 PHP 文件,以及来自 JS 的信息是否会进入 PHP 文件。
非常感谢任何帮助!
<script language="JavaScript">
Webcam.set({
width: 640,
height: 480,
image_format: 'jpeg',
jpeg_quality: 90
});
Webcam.attach( '#members_camera' );
function take_snapshot() {
// take snapshot and acquire image data
Webcam.snap( function(data_uri) {
// snap complete, image data is in 'data_uri'
Webcam.on( 'uploadProgress', function(progress) {
// Upload in progress
// 'progress' will be between 0.0 and 1.0
} );
Webcam.on( 'uploadComplete', function(code, text) {
location.reload();
// Upload complete!
// 'code' will be the HTTP response code from the server, e.g. 200
// 'text' will be the raw response content
} );
var postid = '<?php echo $postid ?>';
var url = '<?php echo get_stylesheet_directory_uri() . '/inc/webcam/uploader.php?postid='; ?>' + postid;
Webcam.upload( data_uri, url, function(code, text) {
// Upload complete!
// 'code' will be the HTTP response code from the server, e.g. 200
// 'text' will be the raw response content
} );
} );
}
</script>
这是“uploader.php”文件
require_once("../../../../../wp-load.php");
$name = date('YmdHis');
$imagename = $_SERVER['DOCUMENT_ROOT']."/wp-content/uploads/members_photos/".$name.".jpg";
move_uploaded_file($_FILES['webcam']['tmp_name'], $imagename);
$postid = $_GET['postid'];
update_field('foto', $imagename, $postid);
echo $postid;
echo $imagename;