php - 上传文件夹

标签 php javascript codeigniter upload uploadify

我正在使用 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 的值本田 但这给出了一些不同的东西。

我上传了一个文件,这个脚本将它上传到了

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 是 Controller ,editpage 是函数,honda 是参数。[Codeigniter 框架]


SOLVED

我解决了这个问题,这是 uploadify 中的一个错误:uploadify 文件夹变量不是直截了当的,因此我们应该在它之前添加一个 斜杠

所以它将是 var folder = "/"+ "FolderName"; 问题是如果您只使用文件夹名称,您将无法返回服务器上的数据。

最佳答案

我解决了这个问题,这是 uploadify 中的一个错误:uploadify 文件夹变量不是直截了当的,所以我们应该在它之前添加一个斜杠。

所以它将是 var folder = "/"+ "FolderName";问题是如果您只使用文件夹名称,您将无法返回服务器上的数据。

关于php - 上传文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8399642/

相关文章:

php - 如何将数据从数据库从模型发送到 Controller codeigniter

PHP - Codeigniter : Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open for reading, 文件不存在

php - 测量服务器正常运行时间

php重置密码链接

javascript - babel 与express js :avoid relative path imports

javascript - 从对象数组中获取唯一元素数组

mysql - 使用 Codeigniter 将复选框值传递到 mysql 数据库

php - .htaccess——css 被停用

php - 单击按钮时将数据从 MySQL 数据库加载到 HTML 文本框

javascript - 在 rails 中使用 jquery 提交之前,将值从输入框复制到隐藏字段