【发布时间】:2012-01-14 00:56:22
【问题描述】:
我正在使用 Uploadify 上传文件并使用 Codeigniter 框架。
这是我的上传代码:
$("#change_profile_icon").uploadify({
'uploader' : '/project/style/scripts/crop/uploadify/uploadify.swf',
'script' : 'http://localhost/project/pages/profile_icon',
'cancelImg' : '/project/style/scripts/crop/uploadify/cancel.png',
'buttonText' :'Upload image',
'width' : '110',
'height' : '30',
'queueID' : 'fileQueue',
'auto' : true,
'scriptData' :{username :"<?php echo $this->session->userdata('username');?>",folder:"honda"},
'queueSizeLimit' : 1,
'multi' : false,
'fileDesc' : 'jpg',
'fileExt' : '*.jpg;*.png',
'sizeLimit' : '819200',//max size bytes - 800kb
'onComplete' : function(event,queueID,fileObj,response,data) {
alert("Completed");
var dataresponse = eval('(' + response + ')');
//$('#uploadifyUploader').remove();
var filenametmp = "http://localhost"+(dataresponse.file).substring(0,(dataresponse.file).lastIndexOf("?"));
var current_page = $('#page-list').val();
},
'onSelect' : function (){
var folder = $('#page-list option:selected').text(); //returns HONDA which is correct
$('#change_profile_icon').uploadifySettings('folder',folder);
} ,
'onError' : function(){
alert('error');
}
});
这是我的 PHP 部分 [Uploadify 中的脚本值]
function profile_icon()
{
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
// $fileTypes = str_replace('*.','',$_REQUEST['fileext']);
// $fileTypes = str_replace(';','|',$fileTypes);
// $typesArray = split('\|',$fileTypes);
// $fileParts = pathinfo($_FILES['Filedata']['name']);
// if (in_array($fileParts['extension'],$typesArray)) {
// Uncomment the following line if you want to make the directory if it doesn't exist
$targetPath = 'uploads/' .$_REQUEST['folder']. '/';
$targetFile = $targetPath.$_FILES['Filedata']['name'];
if (!file_exists($targetPath))
{
mkdir(str_replace('//','/',$targetPath), 0755, true);
}
move_uploaded_file($tempFile,$targetFile);
echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
// } else {
// echo 'Invalid file type.';
// }
}
问题:
$targetPath = 'uploads/' .$_REQUEST['folder']. '/';
$targetFile = $targetPath.$_FILES['Filedata']['name'];
if (!file_exists($targetPath))
{
mkdir(str_replace('//','/',$targetPath), 0755, true);
}
检查上面从PHP部分获取的代码。我认为$_REQUEST['folder']将给出在Uploadify脚本上指定的文件夹名称。folder的值是Honda
但这给出了一些不同的东西。
我上传了一个文件,这个脚本把它上传到了
C:\wamp\www\project\uploads\project\home\editpage\honda\honda
在 wamp 服务器上 [我在本地主机中]
但它是怎么来的?应该是
C:\wamp\www\project\uploads\honda
检查以下...
$targetPath = 'uploads/' .$_REQUEST['folder']. '/';
$targetFile = $targetPath.$_FILES['Filedata']['name'];
targetPath 现在应该是 uploads/honda/
targetFile 现在应该是 uploads/honda/fileName.ext
我不知道我做错了什么以及它在哪里......
请帮帮我。
谢谢。
编辑:当前页面的 URL 结构:http://localhost/Project/home/editpage/honda/
其中home 是控制器,editpage 是函数,honda 是参数。[Codeigniter 框架]
SOLVED
我解决了这个问题,这是uploadify中的一个错误:uploadify文件夹变量不是直截了当的,所以我们应该在此之前添加一个slash。
所以应该是var folder = "/"+ "FolderName";
问题是如果您只使用文件夹名称,您将无法返回服务器上的数据。
【问题讨论】:
标签: php javascript codeigniter upload uploadify