gpt4 book ai didi

php - Plupload : Passing a session variable to upload. php 以更新数据库中的头像上传

转载 作者:行者123 更新时间:2023-11-29 11:08:59 26 4
gpt4 key购买 nike

初学者问题:要管理他的个人资料,注册成员(member)必须能够通过 Plupload 插件上传/修改头像。

仅在 upload.php 文件中,要更新数据库(“users”表),我需要 $_SESSION['auth'] 变量给出的用户 ID。

此变量存在于 profile.php 中:var_dump($_SESSION['auth']) 给出:

C:\wamp\www\graf\profile.php:24:
object(stdClass)[1]
public 'id' => string '22' (length=2)
public 'username' => string 'johndoe' (length=6)
public 'email' => string 'john@doe.com' (length=17)
public 'password' => string '$2y$10$nwS.6PlyuH7NQnRuPdRJnuLmPSJK/gzmcWomHYz5Uk3SOkDutnLQ.' (length=60)
public 'confirmation_token' => null
public 'confirmed_at' => string '2016-11-29 12:27:40' (length=19)
public 'reset_token' => null
public 'reset_at' => null
public 'remember_token' => null
public 'avatar' => string 'John-Doe.png' (length=12)

但是我无法将$_SESSION['auth']传输到upload.php。请告诉我如何做。

配置文件.php:

if(session_status() == PHP_SESSION_NONE){ session_start(); }
require_once('inc/functions.php');
require_once('inc/db.php');
reconnect_from_cookie();
?><!DOCTYPE html>
<html >
<head>
<meta charset="utf-8"/>
<title>Member Profile</title>
<link href="css-up/style.css" rel="stylesheet" type="text/css" />
<script src="js-up/jquery-1.11.3.js" type="text/javascript" ></script>
</head>
<body>
<div id="plupload">
<div id="droparea">
<p>Drag and drop your files here</p>
<span class="or">ou</span>
<a href="#" id="browse">Browse</a>
</div>
<div id="filelist">
<?php foreach(glob('uploads/*.*') as $v):?>
<div class="file">
<img src="<?php echo $v; ?>">
<?php echo basename($v); ?>
<div class="actions">
<a href="<?php echo basename($v) ?>" class="del">&times;</a>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<script src="js-up/plupload/plupload.full.min.js" type="text/javascript" ></script>
<script src="js-up/plupload/moxie.js" type="text/javascript" ></script>
<script src="js-up/main.js" type="text/javascript" ></script>
</body>
</html>

main.js:

//var sessId = '<?php echo $_SESSION['auth']['id'] ; ?>';    
var uploader = new plupload.Uploader({
runtimes : 'html5,flash',
container : 'plupload',
browse_button : 'browse',
drop_element : 'droparea',
url : 'upload.php',
flash_swf_url : 'js-up/plupload/Moxie.swf',
multi_selection : false,
multipart : true,
urlstream_upload : true,
multipart_params : {directory:'test'},
max_file_size : '1mb',
resize : {width:320,height:240,quality:90},
filters : [{ title: 'images', extensions:'JPEG,JPG,PNG,GIF'}]
});

uploader.bind('Init',function(up, params){
if(params.runtime!='html5'){
$('#droparea').css('border','none').find('p,span').remove();
}
});

uploader.bind('UploadProgress',function(up, file){
$('#'+file.id).find('.progress').css('width',file.percent+'%');
});

uploader.init();

uploader.bind('FilesAdded',function(up,files){
var filelist=$('#filelist');
for(var i in files){
var file= files[i];
filelist.prepend('<div id="'+file.id+'" class="file">'+file.name+' ('+plupload.formatSize(file.size)+')'+'<div class="progressbar"><div class="progress"></div></div></div>');
}
$('#droparea').removeClass('hover');
uploader.start();
uploader.refresh();
});

uploader.bind('Error',function(up,err){
alert(err.message);
$('#droparea').removeClass('hover');
uploader.refresh();
});
uploader.bind('FileUploaded',function(up, file, response){
data = $.parseJSON(response.response);
if(data.error){
alert(data.message);
$('#'+file.id).remove();
}else{
$('#'+file.id).replaceWith(data.html);
/*
$.ajax({
type: "POST",
cache: false,
url: "upload.php",
data: "sessId="+sessId
});
*/
}
});

jQuery(function($){
$('#droparea').bind({
dragover:function(e){
$(this).addClass('hover');
},
dragleave:function(e){
$(this).removeClass('hover');
}
});

$('.del').on('click',function(e){
e.preventDefault();
var elem =$(this);
if(confirm('Are you sure want to delete this image?')){
$.get('upload.php', {action:'delete',file:elem.attr('href')});
elem.parent().parent().slideUp();
}
return false;
});
});

上传.php:

if(session_status() == PHP_SESSION_NONE){ session_start();  }

if(isset($_GET['action']) && $_GET['action']== 'delete'){
unlink('uploads/'.$_GET['file']);
die();
}
require_once('inc/functions.php');
require_once('inc/db.php');
reconnect_from_cookie();

$extensions_allowed = array('.JPEG','.jpg', '.PNG','.GIF' );
$extension = strrchr($_FILES['file']['name'], '.');
if(!in_array($extension, $extensions_allowed)) {
alert('Only files in these formats are accepted: png, gif, jpg, jpeg ');
}else{
$file=$_FILES['file'];
$name=$file['name'];
if(filesize($file['tmp_name']) > 1000000){
die('{"error":true, "message": "The file is too large. Please upload a picture below 1 Mo."}');
}
if(file_exists('uploads/'.$name)){
die('{"error":true, "message": "Image already exists"}');
}

$name = strtr($name,'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ','~');
$name = preg_replace('/([^.a-zA-Z0-9]+)/i', '-', $name);
$u = mt_rand(0,1000000)."_".$name;
move_uploaded_file( $_FILES['file']['tmp_name'],'uploads/'.$u );

//This request works
$pdo->prepare("UPDATE users SET avatar=? WHERE id=22 ")->execute([$u]) ;
$pdo = null;
/* This doesn't work
$_SESSION['auth'] = $user;
$pdo->prepare('UPDATE users SET avatar=? WHERE id=? ')->execute([$u,$user->id]) ;
$pdo = null;
*/
$v='uploads/'.$u;
$html='<div class="file"><img src="'.$v.'" />'.basename($v).'<div class="actions"><a href="'.basename($v).'" class="del" >&times;</a></div></div>';
$html=str_replace('"','\\"',$html);
die('{"error":false, "html": "'.$html.'"}');
}

最佳答案

您必须使用session_start() 来恢复 session 。只需删除前面的 if 即可每次启动。 Documentation

替换

if(session_status() == PHP_SESSION_NONE){ session_start();  }

session_start();

关于php - Plupload : Passing a session variable to upload. php 以更新数据库中的头像上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40951681/

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