如何在uploadify中传递自定义值

时间:2012-06-06 09:10:57

标签: php html uploadify

uploadify中的

如何将用户输入值传递给uploadify.php 这是我的代码:

           $(function() {

    $('#file_upload').uploadify({
    'buttonText' : 'BROWSE...',
    'method'   : 'post', 
    'height'   : 50, 
    'buttonCursor' : 'hand',
    'fileSizeLimit' : '100KB', 
    'formData' : {
    'title' :$('input:text[name:title]').val(),
    'about' :$('input:text[name:about]').val() },
    'auto'     : false,
    'swf'      : 'uploadify.swf',
    'checkExisting' : 'check-exists.php',
    'uploader' : 'uploadify.php',
    'onUploadSuccess' : function(file, data, response) {
    $("#message").css("display","block");
    $("#message").text("upload process completed successfully");
    setTimeout(function() {
    $('#message').fadeOut('fast');
}, 5000);
    }, 

    'onUploadError' : function(file, errorCode, errorMsg, errorString) {
       alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
    }  
});



    });
    </script>
    </head>
<?php include("includes/connection.php");?>
    <body>
        <div id="page">
            <div id="header">
                <h1>Photogallery</h1>
     <?php  include("includes/navigation.php"); ?>
            </div><!--header-->

            <div id="content">

                <div id="left">

<div id="message">
</div>
<?php

 ?>     

 <table>
    <tr>
        <td>
            <P>Title:</P>
        </td>
        <td>
            <input type="text" value="" name="title" id="idtitle" />
        </td>
    </tr>
    <tr>
        <td>
            <p>About:</p>
        </td>
        <td>
            <p><input type="text" value="" name="about"  />  </p>
        </td>
    </tr>
    <tr>
        <td>
            Select Photos you want to Upload
        </td>
        <td>
            <input type="file" name="file_upload" id="file_upload" />
        </td>
    </tr>
 </table>           


<a href="javascript:$('#file_upload').uploadify('cancel')">Cancel First File</a> | <a href="javascript:$('#file_upload').uploadify('cancel', '*')">Clear the Queue</a> | <a href="javascript:$('#file_upload').uploadify('upload', '*')">Upload the Files</a>



                </div><!--left-->

这是我的uplodify.php文件

   <?php
require_once("includes/connection.php");

$targetFolder = '/workbench/sudeepc/photogallery/uploads' ; 
     $title=$_POST['title'];
     // $title="testtitle";
     $about=$_POST['about'];    
if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
    $targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];

    // Validate the file type
    $fileTypes = array('jpg','jpeg','gif','png'); // File extensions
    $fileParts = pathinfo($_FILES['Filedata']['name']);



    if (in_array($fileParts['extension'],$fileTypes)) {
        move_uploaded_file($tempFile,$targetFile);

$query="INSERT INTO `photo` ( `id` , `path` , `uname` , `title` , `about` , `date` )
VALUES ('','${targetFile}','uname','${title}','${about}',now())";       

 /*
 $query="INSERT INTO `photo` ( `id` , `path` , `uname` )
 VALUES ( '','${targetFile}', '${title}');";
 */
 mysql_query($query,$connection);


    } else {
        echo 'Invalid file type.';
    }
}
?>

我想传递标题以及关于每次上传的内容,当用户选择多个图片时,用户可以输入标题和每个标题。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:5)

  1. 如何在uploadify中传递自定义值:
  2. 'formData': { 'userid': $("#uid").val(), 'taskid': $("#tid").val(), 'guid': $("#guid").val() },

    1. 有没有办法在上传队列中获取图片数量? - onDialogClose你有权访问这样的队列:
    2. 'onDialogClose': function(queueData) { alert(queueData.filesQueued + ' files were queued of ' + queueData.filesSelected + ' selected files. There are ' + queueData.queueLength + ' total files in the queue.'); },

答案 1 :(得分:3)

要传递自定义值,请使用formData

然后您可以在PHP $_POST['someKey']

上访问它
 ...
 'formData'         : {
     'title' : $('input:text[name:title]').val(),
     'about' : $('input:text[name:about]').val(),
      ...
  },
 ..

要检查上传的文件数,请添加此回调:

  ...
  onDialogClose : function() {
      console.log(this.queueData.filesQueued);
  },
  ...