【问题标题】:uploadify session problem上传会话问题
【发布时间】:2011-09-12 15:38:21
【问题描述】:

您好,我正在使用 Uploadify 上传照片。 当用户登录时,他的 ID 存储在 session 中,但是通过 Uploadify 上传任何内容后,他的 ID 从 session 中删除,所以看起来他没有登录。

我尝试将他的 ID会话 ID会话名称 作为 scriptData 传递,但它没有工作。当用户登录并尝试上传某物时,上传会话干净后(没有 用户 ID上传的照片名称 存储)。 当用户未登录时,会话包含上传的照片名称...

这是.js:

$(document).ready(function() {
      $('#fileUpload').uploadify({
        'uploader'      : '{/literal}{$PATH_TO_ROOT}{literal}scripts/js/uploadify/uploadify.swf',
        'script'        : '{/literal}{$URL_PREFIX}{$tc->get('uploadify')}{literal}',
        'scriptData'    : {'PHP_SESS_ID': '{/literal}{$sessionId}{literal}','PHP_SESS_NAME':'{/literal}{$sessionName}{literal}'{/literal}{if $user},'PHP_SESS_UZIV':'{$user->get('Id')}'{/if}{literal}},
        'cancelImg'     : '{/literal}{$PATH_TO_ROOT}{literal}scripts/js/uploadify/cancel.png',
        'fileDataName'  : 'Filedata',
        'fileExt'       : '*.jpg;*.gif;*.png',
        'fileDesc'      : 'Image Files',
        'sizeLimit'     : 5242880, //  5x1024x1024 bytes        
        'auto'          : false,

        'buttonText'    : 'VYBERTE FOTKU',
        'buttonImg'     : '{/literal}{$PATH_TO_ROOT}{literal}scripts/js/uploadify/uploadify-butt-{/literal}{$lang}{literal}.jpg',
        'rollover'      : true,
        'width'         : 300,
        'height'        : 45,
        'hideButton'    : false,
        'method'        : 'post',
        'multi'         : false,

        'onAllComplete' : function(event,data) {
            window.location.href = '{/literal}{$URL_PREFIX}{$tc->get('photo-uploaded')}{literal}';
        }
      });
});

这是后端脚本:

if (!empty($_FILES)) {

    $session_id = $_POST["PHP_SESS_ID"];
    $session_name = $_POST["PHP_SESS_NAME"];

    session_id($session_id);
    session_name($session_name);

    if (isset($_POST["PHP_SESS_UZIV"])) {
        $user_id = $_POST["PHP_SESS_UZIV"];
        $_SESSION['sess_us_id'] = $user_id;
    }

    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = dirname(dirname(__FILE__)) . '/img/';

    $newName = time() . '_' . StripAccent($_FILES['Filedata']['name'], '', false, false);

    $targetFile =  str_replace('//','/',$targetPath) . $newName;

    $savePic = savePic($targetPath, $newName, -590, -500, 100, $tempFile);
    $saveThumb = savePic($targetPath, getThumbName($newName), -185, -142, 100, $targetFile);

    $_SESSION['uploadedPhoto'] = $newName;  
}

感谢您的帮助

编辑:好吧,我发现这段代码在一个服务器上有效,但在另一台服务器上无效......

【问题讨论】:

    标签: php session uploadify


    【解决方案1】:

    这对我有用:

    在显示文件上传按钮的代码中:

    <?php 
    session_start();
    ?>
    
    ....
    
    $(function() {
        $('#file_upload').uploadify({
            'formData'     : {
                'PHPSESSID': '<?=session_id()?>'
            },
            'buttonText'    : 'Upload',
            'displayData': 'speed',
            'swf'      : 'uploadify.swf',
            'uploader' : 'uploadify.php',
            'onUploadSuccess' : function(file, data, response) {
                ....
            }
        });
    });
    

    在uploadify.php 上 这样做:

    session_id($_POST['PHPSESSID']);
    session_start();
    

    就是这样,所有会话数据都可以在uploadify.php 中访问。 对我有用。

    希望我能帮助到某人。

    【讨论】:

    • 有完全相同的问题,uploadify 破坏了用户会话,但这很好地解决了它。非常感谢。
    【解决方案2】:

    你到底是用什么来生成这个 Javascript 的?

        'scriptData'    : {'PHP_SESS_ID': '{/literal}{$sessionId}{literal}','PHP_SESS_NAME':'{/literal}{$sessionName}{literal}'{/literal}{if $user},'PHP_SESS_UZIV':'{$user->get('Id')}'{/if}{literal}},
    

    这是从 PHP 中回显的吗? $sessionID、$user、$PATH_TO_ROOT 等在哪里定义?

    在您的示例上传处理代码启动之前,是否在某处执行了 session_start()?没有它,当脚本退出时,您的所有会话操作都将消失。您必须为 PHP 调用 session_start() 来为您保留 $_SESSSION。

    确保 Uploadify 实际上获得了正确的会话名称/ID - 如果它弄错了,那么您的代码会将会话 ID/名称重置为实际会话之外的其他内容,您最终会得到您出现的症状 - 上传数据消失 - 因为它进入了某个 OTHER 会话。

    同样,您的脚本中似乎没有任何错误处理,并假设上传成功。至少你应该有:

    if ($_FILES['Filedata']['error'] === UPLOAD_ERR_OK) {
       ... process upload ...
    } else {
       die("Upload failed with error code #" . $_FILES['Filedata']['error']);
    }
    

    仅仅因为 _FILES 数组不为空并不意味着上传成功。 PHP 仍会尽可能多地填写有关上传的信息,包括错误代码,这意味着无论上传成功/失败,该数组中总会有数据。

    【讨论】:

    • Smarty 模板,我试过start_session(),但没有任何改变。
    【解决方案3】:

    您是否正在使用 Internet Explorer 进行测试?

    【讨论】:

    • 用FF4,最新的Opera,最新的Chrome
    【解决方案4】:

    http://www.uploadify.com/documentation/uploadify/using-sessions-with-uploadify/

    $('#file_upload).uploadify({
        // Your normal options here
        formData : { '<?php echo session_name();?>' : '<?php echo session_id();?>' }
    });
    

    在上传中:

    //uploadify.php
    $session_name = session_name();
    
    if (!isset($_POST[$session_name])) {
        exit;
    } else {
        session_id($_POST[$session_name]);
        session_start();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      • 2012-09-23
      • 1970-01-01
      • 2010-12-19
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多