UploadFive中的动态文件夹与CodeIgniter

时间:2013-07-17 10:59:14

标签: php codeigniter file-upload

我开始使用uploadifive进行文件上传。我正在尝试创建一个动态文件夹,上传的文件将保存到该文件夹​​中。正在创建该文件夹。但我无法将文件上传到该文件夹​​。

此视图页面:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>UploadiFive Test</title>
<script src="<?=base_url()?>application/views/Sample/jquery.min.js" type="text/javascript"></script>
<script src="<?=base_url()?>application/views/Sample/jquery.uploadifive.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="<?=base_url()?>application/views/Sample/uploadifive.css">
<style type="text/css">
body {
    font: 13px Arial, Helvetica, Sans-serif;
}
.uploadifive-button {
    float: left;
    margin-right: 10px;
}
#queue {
    border: 1px solid #E5E5E5;
    height: 177px;
    overflow: auto;
    margin-bottom: 10px;
    padding: 0 3px 3px;
    width: 300px;
}
</style>
</head>

<body>
    <h1>UploadiFive Demo</h1>
    <form>
        <div id="queue"></div>
        <input id="file_upload" name="file_upload" type="file" multiple="true">
        <a style="position: relative; top: 8px;" href="javascript:$('#file_upload').uploadifive('upload')">Upload Files</a>
    </form>
<?php 


                $query = $this->db->get('filename');

                $folder_name = underscore($query->row()->file_name);


               //$config['upload_path'] = "./uploads/$folder_name/";

                  if(!is_dir("uploads/$folder_name"))
                      {
                         mkdir("uploads/$folder_name",0777);
                       }

                      echo $folder_name; 
                       ?> 
    <script type="text/javascript">
        <?php $timestamp = time();?>
        $(function() {
            $('#file_upload').uploadifive({
                'auto'             : false,
                'checkScript'      : '<?=base_url()?>check-exists.php',
                'formData'         : {
                                       'timestamp' : '<?php echo $timestamp;?>',
                                       'token'     : '<?php echo md5('unique_salt' . $timestamp);?>'
                                     },
                'queueID'          : 'queue',
                'uploadScript'     : '<?=base_url()?>application/views/Sample/uploadifive.php',
                'onUploadComplete' : function(file, data) { console.log(data); }
            });
        });
    </script>
</body>
</html>

在上面的代码中,我从数据库中获取文件夹名称,并创建一个具有777权限的文件夹。

这是uploadifive.php文件:

<?php


// Set the uplaod directory

//Here i get the folder name


               $query = $this->db->get('filename');

                $folder_name = underscore($query->row()->file_name);



// Set the allowed file extensions
$fileTypes = array('jpg', 'jpeg', 'gif', 'png'); // Allowed file extensions

$verifyToken = md5('unique_salt' . $_POST['timestamp']);

if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
    $tempFile   = $_FILES['Filedata']['tmp_name'];


    $uploadDir  = "/Applications/MAMP/htdocs/0.9.1/uploads/$folder_name/";



    $targetFile = $uploadDir . $_FILES['Filedata']['name'];

    // Validate the filetype
    $fileParts = pathinfo($_FILES['Filedata']['name']);
    if (in_array(strtolower($fileParts['extension']), $fileTypes)) {

        // Save the file
        move_uploaded_file($tempFile, $targetFile);
        echo 1;

    } else {

        // The file type wasn't allowed
        echo 'Invalid file type.';

    }
}
?>

这里我从数据库中获取文件名,并将其用作动态文件夹。 当我包含从数据库中获取文件名的代码时,它会抛出错误500。 我不知道如何处理这件事。

有什么想法吗? 谢谢你的帮助。

TL; DR:我很擅长编码。

1 个答案:

答案 0 :(得分:0)

尝试更改以下内容:

$uploadDir  = "/Applications/MAMP/htdocs/0.9.1/uploads/$folder_name/";

$uploadDir = $_SERVER['DOCUMENT_ROOT'] . "/uploads/" . $folder_name . "/";