Upload multiple files with Uploadify - need ID from input field

↘锁芯ラ 提交于 2019-12-25 01:06:06

问题


I have multiple input fields on one page. Each input field have a text input and a file upload field. The current workflow looks like the following:

  • Directly after the upload the name of the files is stored in the database. (uploadify.php)
  • I get an ID from the database which I need to correspond to the upload field. (uploadify.php - write in session?)

Now I need a way to keep track of which file upload corresponds to which text input. If I have the id of the file upload field, I can make a relation to the corresponding text input and together with the returned id from the database I could store the text to the corresponding upload.

I tried the way described here, but onSelect only has a file object which can't provide me the data which input field called Uploadify. I didn't found another function which provide me this in the actual version of Uploadify.

Now I'm thinking about using $_SESSION for this, but don't know how this can be done.

Client side:

$('.iconupload').uploadify({
    'swf'            : '<?php echo SUBFOLDER; ?>/swf/uploadify.swf',
    'uploader'       : '<?php echo SUBFOLDER; ?>/includes/uploadify.php',
    'formData'       : {'folder' : '<?php echo IMG_UPLOAD_ICONS; ?>', 'id' : '<?php echo $_SESSION['ID']; ?>', 'type' : 'icon'},
    'multi'          : false,
    'auto'           : true,
    'removeCompleted': false,
    'queueSizeLimit' : 1,
    'simUploadLimit' : 1,
    'fileTypeExts'       : '*.jpg; *.jpeg; *.png; *.gif',
    'fileTypeDesc'       : 'JPG Image Files (*.jpg); JPEG Image Files (*.jpeg); PNG Image Files (*.png), GIF (*.gif)',
    'onUploadError'  : function(file, errorCode, errorMsg, errorString) {
        alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
    },
    'onUploadSuccess': function(file, data, response) {
        //alert('The file ' + file.name + ' was successfully uploaded with a response of ' + response + ':' + data);
    },
    'onSelect'       : function(file) {
        //var elem_id = $(event.target).attr("id"); //here you get the id of the object.
        //alert (elem_id);
        console.debug(file);
        //$("#"+elem_id).uploadifySettings('formData',{'formID':elem_id})
    }
});

Server-side:

if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
    $targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];

        $res= mkdir(str_replace('//','/',$targetPath), 0750, true);
        $res2=move_uploaded_file($tempFile,$targetFile);
        $relative_url = str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
        echo $relative_url;


    if($_REQUEST['type'] == 'icon' && !empty($_REQUEST['id'])){
        $iconid = $db->addIcon($_REQUEST['id'], $relative_url);
        $icons = array();
        if(isset($_SESSION['icons'])){
            $icons = $_SESSION['icons'];
        }
        $icons[$_REQUEST['formID']]= $iconid;
        $_SESSION['icons'] = $icons;
    }
}

Perhaps one could use onUploadSuccess and the data object, which can be returned in uploadify.php and use some Javascript magic. But I still don't know from which input field Uploadify was called ...


回答1:


You can setup uploadify using jQuery .each(). This will allow you to set an id to be passed through the formData like this:

$('.iconupload').each(function() {
    var $iconUpload = $(this);

    $iconUpload.uploadify({
        'swf'            : '<?php echo SUBFOLDER; ?>/swf/uploadify.swf',
        'uploader'       : '<?php echo SUBFOLDER; ?>/includes/uploadify.php',
        'formData'       : {'folder' : '<?php echo IMG_UPLOAD_ICONS; ?>', 'id' : '<?php echo $_SESSION['ID']; ?>', 'type' : 'icon', 'uploadId': $iconUpload.attr("id")},
        'multi'          : false,
        'auto'           : true,
        'removeCompleted': false,
        'queueSizeLimit' : 1,
        'simUploadLimit' : 1,
        'fileTypeExts'       : '*.jpg; *.jpeg; *.png; *.gif',
        'fileTypeDesc'       : 'JPG Image Files (*.jpg); JPEG Image Files (*.jpeg); PNG Image Files (*.png), GIF (*.gif)',
        'onUploadError'  : function(file, errorCode, errorMsg, errorString) {
             alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
        },
        'onUploadSuccess': function(file, data, response) {
            //alert('The file ' + file.name + ' was successfully uploaded with a response of ' + response + ':' + data);
        }
    }
});

If you do not want to save the data within uploadify.php, you can save it in onUploadSuccess like this:

'onUploadSuccess': function(file, data, response) {
    saveIconData($(iconUpload.attr("id"), file.name);       
}

function saveIconData(id, fileName) {
    // Save via ajax
}

To save data via ajax, you can use jQuery's $.ajax() method. You would setup the ajax call something like this:

$.ajax({
    url: "urlToPhpFile.php",
    data: { iconID: value, iconFileName: value },
    success: function(result) {
        // do something on success of save
    }
});


来源:https://stackoverflow.com/questions/12512509/upload-multiple-files-with-uploadify-need-id-from-input-field

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