会话变量未传递到上传.php


Session variable not getting passed to uploadify.php

我需要使用用户名的会话变量来指定uploadify(版本3.1)中的上传路径。在上传中.php使用以下方法可以正常工作:

$uploadDir = 'media/' . $_SESSION["user_name"] . '/';

但是在 uploadify 中,当我使用相同的代码时,文件只是上传到媒体文件夹(忽略用户文件夹)。我在 uploadify 中回显了 $_SESSION["user_name"].php并使用 onUploadSuccess 函数检索了它,尽管启动了会话,但用户名变量确实没有传递给 uploadify.php。

似乎很奇怪,这将适用于上传而不是上传。我对PHP不太精通,很高兴在这方面得到一些帮助。

我在下面包含完整的上传.php脚本。

谢谢

缺口

<?php
session_start();
/*
Uploadify
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
*/
// Define a destination
$targetPath = 'media/' . $_SESSION["user_name"] . '/';
if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetFile = $targetPath . $_FILES['Filedata']['name'];
    // Validate the file type
    $fileTypes = array('jpg','jpeg','gif','png'); // File extensions
    $fileParts = pathinfo($_FILES['Filedata']['name']);
    if (in_array($fileParts['extension'],$fileTypes)) {
        move_uploaded_file($tempFile,$targetFile);
        echo '1';
    } else {
        echo 'Invalid file type.';
    }
}
?>
我也偶然

发现了这个问题。

Uploadify flash文件是调用PHP脚本的文件,而不是浏览器,这意味着PHP会话不会从浏览器传播到flash并传播到服务器,您只需将会话ID传递$ _GET/$ _POST即可使问题消失。

在 HTML 文件中:

<input type="hidden" id="sessionid" value="<?php echo session_id(); ?>" />

在 javascript 文件中:

$('#image').uploadify({
    'uploader'  : '/uploadify-v2.1.4/uploadify.swf',
    'script'    : '/uploadify.php',
    'cancelImg' : '/uploadify-v2.1.4/cancel.png',
    'folder'    : '/tempfiles',
    'auto'      : true,
    'multi'     : false,
    'scriptData': { 'session': $('#sessionid').val() }
});

在 PHP 文件中:

if ( isset($_REQUEST['session']) && !empty($_REQUEST['session']) ) {
    session_id($_REQUEST['session']);
}
session_start();

当会话配置设置为 auto_start 时似乎存在问题(但我还没有完全测试过)。

这在Uploadify的文档中也提到了。

我会检查您的登录脚本,以确保您在对用户进行身份验证时确实将用户名存储在会话数组中。您调用session_start() ,这消除了这种可能性,因此,如果您调用$_SESSION["user_name"]并且没有获得值,则只能意味着该值一开始就不存在,或者您弄错了密钥。