我开始使用 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:我很擅长编码。