0
初心者質問: プロフィールを管理するには、登録メンバーがPluploadプラグインを使用してアバターをアップロード/修正できる必要があります。Plupload:データベース内のアバターアップロードを更新するためにupload.phpにセッション変数を渡す
upload.phpファイルでのみデータベース( 'users'テーブル)を更新するには、$_SESSION['auth']
という変数のユーザーIDが必要です。
この変数はprofile.phpに存在する:
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 '[email protected]' (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)
しかし、私はupload.phpする$_SESSION['auth']
を送信することはできません。var_dump($_SESSION['auth'])
ができます。方法を教えてください。
profile.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">×</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;
});
});
upload.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" >×</a></div></div>';
$html=str_replace('"','\\"',$html);
die('{"error":false, "html": "'.$html.'"}');
}