使用CodeIgniter在Uploadfive中创建动态文件夹 [英] Dynamic Folders in Uploadfive with CodeIgniter

查看:106
本文介绍了使用CodeIgniter在Uploadfive中创建动态文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I started using uploadifive for file uploads. I am trying to create a dynamic folder into which the uploaded files will be saved. The folder is being created. but i cant upload the files into that folder.

此视图页面:

<!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权限的文件夹.

In the code above i get the folder name from database and create a folder with 777 permissions.

这是uploadifive.php文件:

And this is the uploadifive.php file:

<?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. 我不确定该如何处理.

Here i get the file name from the database and use that as a dynamic folder. When ever i include the code to get the file name from the database, it throws an error 500. I'm not sure how to deal with this.

有什么想法吗? 感谢您的帮助.

Any ideas? Thanks for your help.

TL; DR:我很讨厌编码.

推荐答案

尝试更改以下内容:

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

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

这篇关于使用CodeIgniter在Uploadfive中创建动态文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆