douwuying4709 2013-07-17 10:59
浏览 39

UploadFive中的动态文件夹与CodeIgniter

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.

This the view page:

<!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>

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

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.';

    }
}
?>

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 : I Suck at coding.

  • 写回答

1条回答 默认 最新

  • douzha8489 2014-01-16 05:43
    关注

    Try changing the following:

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

    to

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

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?