Uploadify如何创建新文件夹并将文件加载到该文件夹

时间:2012-06-25 04:57:50

标签: php jquery codeigniter uploadify

任何人都可以告诉如何将值从jquery传递到uploadify.php dynfolder:$("#storeloyaltyid").val(),//值是photo1无法在uploadify.php中获取

Jquery的

if($("#uploadphotoz").length>0){

        uploadphotoz();

    }

    function uploadphotoz(){

        $('#uploadphotoz').Uploadify({

            uploader:urljs+"/jslib/Uploadify.swf",
            cancelImage:urljs+"/image/cancel.png",
            buttonImage:urljs+"/image/browse_button.png",
            uploadUrl:urljs+"/jslib/uploadify.php",
            dynfolder:$("#storeloyaltyid").val(),//value is photo1
            height  : 21,
            width   :59,
            auto    : true,
            fileLimit:1,
            preview:'stuimgpreview',
            onComplete: function(event,status,imgs){   
                $("#memPhoto").val(imgs);
            },
            onRemove:function(event,imgs){
                $("#memPhoto").val(imgs);
            }
        });
    }

uploadify.php

<?php
if (!empty($_FILES)) {

    $filename = $_FILES['Filedata']['name'];
    $filetmpname = $_FILES['Filedata']['tmp_name'];
    $fileType = $_FILES["Filedata"]["type"];
    $fileSizeMB = ($_FILES["Filedata"]["size"] / 1024 / 1024);

    // Place file on server, into the images folder
    //move_uploaded_file($_FILES['Filedata']['tmp_name'], "../uploaded_photos/".$filename);
    $dynfolder=$_POST['dynfolder'];
mkdir($dynfolder, 0755, true);move_uploaded_file($_FILES['Filedata']['tmp_name'], "../uploaded_photos"."/".$dynfolder."/".$filename);.../uploaded_photos/photos1/1.jpg

}elseif($_POST['d']){
    $filename = $_POST['d'];
    $dynfolder=$_POST['dynfolder'];
mkdir($dynfolder, 0755, true);
$dFile=move_uploaded_file($_FILES['Filedata']['tmp_name'], "../uploaded_photos"."/".$dynfolder."/".$filename);//.../uploaded_photos/photos1/1.jpg
    unlink($dFile);

}
?>

我无法在uploaded_photos文件夹中创建名为photos1的新文件夹...路径应为.../uploaded_photos/photos1/1.jpgdynfolder:$("#storeloyaltyid").val(),//value is photo1,任何人都可以告诉代码有什么问题。

2 个答案:

答案 0 :(得分:1)

$('#uploadphotoz').Uploadify({
    ...
    'method': 'post',
    'formData': { 'dynfolder': $("#storeloyaltyid").val() },
    ...
});

答案 1 :(得分:0)

试试这个:您可能需要将/../更改为../。我现在无法测试这个。

此外,您应该检查目录是否存在以及文件上载是否成功。如果文件夹已经存在,我不知道PHP会做什么。

<?php
$basepath = dirname(__FILE__);

if (!empty($_FILES)) {
    $filename = $_FILES['Filedata']['name'];
    $filetmpname = $_FILES['Filedata']['tmp_name'];
    $fileType = $_FILES["Filedata"]["type"];
    $fileSizeMB = ($_FILES["Filedata"]["size"] / 1024 / 1024);
    $dynfolder = $_POST['dynfolder'];
    mkdir($basepath.'/../'.$_POST['dynfolder'], 0755, true);
    move_uploaded_file($_FILES['Filedata']['tmp_name'], $basepath.'/../'.$_POST['dynfolder'].'/'.$filename);
}
?>

编辑:

Javascript问题是你只是在一个名为dynfolder的对象中“组成”了一个变量。 Uploadify有一个名为formData的设置。在这里传递变量。

例如:

[...]
height : 21,
width : 59,
auto : true,
formData : jQuery.parseJSON('{"dynfolder":"'+$("#storeloyaltyid").val()+'"}'),
[...]