我遇到了一些代码上的麻烦,我希望有人能给我一些启示:)
我在一个项目中使用网络摄像头,就像Wordpress网站中的一个小型CRM。我使用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;发布于 2018-03-20 18:35:58
这样啊,原来是这么回事!
问题在于我的"uploader.php“文件。现在看起来是这样:
require_once('../../../../../wp-load.php');
$name = date('YmdHis');
$upload_dir = wp_upload_dir();
$imagename = $upload_dir['basedir'] . '/members_photos/' . $name . '.jpg';
move_uploaded_file($_FILES['webcam']['tmp_name'], $imagename);
$home_url = home_url();
$member_pic_url = $home_url . '/wp-content/uploads/members_photos/' . $name . '.jpg';
$postid = $_GET['postid'];
update_field('foto', $member_pic_url, $postid);我还必须手动创建目录。因此,如果有人有更好的解决方案,我愿意:)
https://stackoverflow.com/questions/49386928
复制相似问题