gpt4 book ai didi

php - Valums 文件 uploader : Limit uploads by users credit

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:48:18 24 4
gpt4 key购买 nike

我正在使用 Valums 出色的文件 uploader - https://github.com/valums/file-uploader

我想添加的一件事是基于用户帐户余额的限制。

第一张图片始终免费,因此即使您的余额为 0,您也可以上传一张图片。

额外的图片需要 0.50 的值(value)或积分。如果他们没有足够的信用,它将显示警报并且不会上传文件。

余额可以从 php session 变量 $_SESSION['user']['credit']

这是目前的代码

function createUploader(){ 
var running = 0;
var uploader = new qq.FileUploader({
multiple: true,
element: $('#file-uploader')[0],
action: 'classes/upload.item.php',
allowedExtensions: ['jpg', 'png', 'gif'],
params: {item: '<?php echo $item_id ?>'},
onSubmit: function(id, fileName){
running++;
$('.button').replaceWith('<a class="button large grey full-width">Please wait...</a>');
},
onComplete: function(id, fileName, responseJSON){
running--;
$(".thumbnails").append('<li class="span2"> <a class="thumbnail"><img src="<?php echo $path; ?>'+fileName+'" /></a> </li>');
if(running==0){
$('.button').replaceWith('<a class="button large green full-width" href="confirm/<?php echo $item_id; ?>">Continue to next step</a>');
}

},
onCancel: function(id, fileName){
running--;
},
debug: true
});
}

抱歉,问题是,您能否就我上述内容的实现提供任何建议?

干杯

编辑:我已使用以下建议尝试了以下操作(可能不正确)。

我尝试使用return false 停止文件上传的尝试不成功,上传立即开始,似乎被警报暂停,警报关闭后上传完成。

文档说

onSubmit(String id, String fileName) - called when the file is submitted to the uploader portion of the code. Note that this does not mean the file upload will begin at this point. Return false to prevent submission to the uploader.

<?php
// count uploaded files
$path = 'uploads/' . $_SESSION['user']['username'] . '/' . $item_id . '/thumbs/s_';
$files = glob($path . '*.*');


// has free upload been used yet? incase of page refresh etc
if (count($files) >= 1) {
$used_free = 'true';
} else {
$used_free = 'false';
}
?>

<script>
function createUploader(){
var credit = <?php echo $_SESSION['user']['credit'] ?>;
var used_free = <?php echo $used_free ?>;
var running = 0;


var uploader = new qq.FileUploader({
multiple: true,
element: $('#file-uploader')[0],
action: 'classes/upload.item.php',
allowedExtensions: ['jpg', 'png', 'gif'],
params: {item: '<?php echo $item_id ?>'},
onSubmit: function(id, fileName){
console.log(used_free);
if (!used_free) {
used_free = 'true';
running++;
$('.button').replaceWith('<a class="button large grey full-width">Please wait...</a>');
} else {
$.get('ajax/getCredit.php', function (data) {
if (data.credit >= 0.5) {
running++;
$('.button').replaceWith('<a class="button large grey full-width">Please wait...</a>');
} else {
alert('you do not have enough credits');
return false;
}
}, "json");
}
},
onComplete: function(id, fileName, responseJSON){
running--;
$(".thumbnails").append('<li class="span2"> <a class="thumbnail"><img src="<?php echo $path; ?>'+fileName+'" /></a> </li>');
if(running==0){
$('.button').replaceWith('<a class="button large green full-width" href="confirm/<?php echo $item_id; ?>">Continue to next step</a>');
}
},
onCancel: function(id, fileName){
running--;
},
debug: true
});
}
</script>

简化了它,这行得通

function createUploader(){ 
var credit = <?php echo $_SESSION['user']['credit'] ?>;
var used_free = <?php echo $used_free ?>;
var running = 0;


var uploader = new qq.FileUploader({
multiple: true,
element: $('#file-uploader')[0],
action: 'classes/upload.item.php',
allowedExtensions: ['jpg', 'png', 'gif'],
params: {item: '<?php echo $item_id ?>'},
onSubmit: function(id, fileName){
if (!used_free) {
used_free = 'true';
running++;
$('.button').replaceWith('<a class="button large grey full-width">Please wait...</a>');
} else {
if (credit >= 0.5) {
running++;
credit = credit - 0.5;
$('.button').replaceWith('<a class="button large grey full-width">Please wait...</a>');
} else {
alert('you do not have enough credits');
return false;
}
}
},
onComplete: function(id, fileName, responseJSON){
running--;
$(".thumbnails").append('<li class="span2"> <a class="thumbnail"><img src="<?php echo $path; ?>'+fileName+'" /></a> </li>');
if(running==0){
$('.button').replaceWith('<a class="button large green full-width" href="confirm/<?php echo $item_id; ?>">Continue to next step</a>');
}
},
onCancel: function(id, fileName){
running--;
},
debug: true
});
}

然后我在扣除信用的确认页面上计算上传的文件

最佳答案

您可以为您的 javascript 打印一个 bool 值以用于检查是否允许上传:

var allowUpload = <?php echo $_SESSION['user']['credit'] >= 0.5 ? 'true' : 'false' ?>;

当他们尝试上传文件时,您仍然需要验证他们在服务器端是否有足够的信用。

AJAX/JSON 方法

您可以使用 AJAX 获取可用的积分,让我们使用 PHP 生成一些 JSON。 getCredit.php:

<?php
session_start();
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo '{"credit":' . $_SESSION['user']['credit'] . '}';

然后您可以从您的 JavaScript 中即时获取积分。

$.get('getCredit.php', function (data) {
if (data.credit >= 0.5) {
// call uploader code here
}
}, "json");

关于php - Valums 文件 uploader : Limit uploads by users credit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12355544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com