php - swfupload 销毁 session ? php

标签 php session arrays swfupload destroy

嘿,我需要一点帮助:

我使用SWFupload上传图片! 在上传函数中,我进行了一个文件夹调用 $_SESSION['folder'] ,并且我上传的所有文件都在上传后的 1 个数组调用 $_SESSION['files'] 中完成我 print_r($_SESSION) 但数组是空的?为什么会这样?

这是我的 upload.php:

if($_FILES['image']['name']) {
    list($name,$error) = upload('image','jpeg,jpg,png');
    if($error) {$result = $error;}
    if($name) { // Upload Successful
        $result = watermark($name);
        print '<img src="uploads/'.$_SESSION['dir'].'/'.$result.'" />';

    } else { // Upload failed for some reason.
        print 'noname'.$result;
    }
}

function upload($file_id, $types="") {
    if(!$_FILES[$file_id]['name']) return array('','No file specified');
    $isimage = @getimagesize($_FILES[$file_id]['tmp_name']);
    if (!$isimage)return array('','Not jpg');

    $file_title = $_FILES[$file_id]['name'];
    //Get file extension
    $ext_arr = split("\.",basename($file_title));
    $ext = strtolower($ext_arr[count($ext_arr)-1]); //Get the last extension

    //Not really uniqe - but for all practical reasons, it is
    $uniqer = substr(md5(uniqid(rand(),1)),0,10);
    //$file_name = $uniqer . '_' . $file_title;//Get Unique Name
    //$file_name = $file_title;
    $file_name = $uniqer.".".$ext;

    $all_types = explode(",",strtolower($types));
    if($types) {
        if(in_array($ext,$all_types));
        else {
            $result = "'".$_FILES[$file_id]['name']."' is not a valid file."; //Show error if any.
            return array('',$result);
        }
    }

    if((!isset($_SESSION['dir'])) || (!file_exists('uploads/'.$_SESSION['dir']))){
        $dirname = date("YmdHis");  // 20010310143223
        $pathtodir = $_SERVER['DOCUMENT_ROOT']."/ifunk/uploads/";
        $newdir = $pathtodir.$dirname;
        if(!mkdir($newdir, 0777)){return array('','cannot create directory');}
        $_SESSION['dir'] = $dirname;
    }

    if(!isset($_SESSION['files'])){$_SESSION['files'] = array();}
    //Where the file must be uploaded to
    $folder = 'uploads/'.$_SESSION['dir'].'/';
    //if($folder) $folder .= '/';   //Add a '/' at the end of the folder
    $uploadfile = $folder.$file_name;

    $result = '';
    //Move the file from the stored location to the new location
    if (!move_uploaded_file($_FILES[$file_id]['tmp_name'], $uploadfile)) {
        $result = "Cannot upload the file '".$_FILES[$file_id]['name']."'"; //Show error if any.
        if(!file_exists($folder)) {
            $result .= " : Folder don't exist.";
        } elseif(!is_writable($folder)) {
            $result .= " : Folder not writable.";
        } elseif(!is_writable($uploadfile)) {
            $result .= " : File not writable.";
        }
        $file_name = '';

    } else {
        if(!$_FILES[$file_id]['size']) { //Check if the file is made
            @unlink($uploadfile);//Delete the Empty file
            $file_name = '';
            $result = "Empty file found - please use a valid file."; //Show the error message
        } else {
            //$_SESSION['files'] = array();
            $_SESSION['files'][] .= $file_name;
            chmod($uploadfile,0777);//Make it universally writable.
        }
    }

    return array($file_name,$result);
}

最佳答案

当您上传时,SWFUpload 不会将 session ID 传递给脚本,因此您必须自己执行此操作。只需将 get 或 post 参数中的 session ID 传递给上传脚本,然后在您的应用程序中在 session_start 之前执行此操作:

if(isset($_REQUEST['PHPSESSID'])) {
    session_id($_REQUEST['PHPSESSID']);
}

关于php - swfupload 销毁 session ? php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2522623/

相关文章:

php - 在yii框架中创建分页

php - 有人知道任何好的后端用户在线文件管理器吗?

c - 在数组中,&arr[2] 返回什么?

javascript - 更改对象数组中的数据类型

php - Laravel 5.2 身份验证功能不起作用

php - 在 Action Controller 之后重定向到上一页

linux - Php session 锁定等待超时

php - 使用发布数据设置 session

javascript - JS改变django中的session时出现问题

c - 尝试从数组中打印出网格,但实际上没有显示任何内容