Dynamic Folders in Uploadfive with CodeIgniter

时间秒杀一切 提交于 2020-01-06 12:55:13

问题


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:


Try changing the following:

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

to

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


来源:https://stackoverflow.com/questions/17697996/dynamic-folders-in-uploadfive-with-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!