如何使用uploadify传递Form值

时间:2011-07-23 09:11:39

标签: php javascript

    jQuery(document).ready(function() {
        jQuery("#file_upload").uploadify({
            'uploader'      : '/wp-content/plugins/amazon-s3-simple-upload-form/files/uploadify/uploadify.swf',
            'script'    : '/wp-content/plugins/amazon-s3-simple-upload-form/files/uploadify/uploadify.php',
            'folder'    : '/uploads',
            'auto'      : true,
            'buttonText'    : 'Browse',
            'cancelImg'     : '/wp-content/plugins/amazon-s3-simple-upload-form/files/uploadify/cancel.png',
            'fileExt'       : '*.*',
            'fileDataName'  : 'file_upload',
            'simUploadLimit': 2,
            'multi'         : true,
            'auto'          : true,
            'onError' : function(event, ID, fileObj, errorObj) {alert(errorObj.type+"::"+errorObj.info);
},
            'onComplete'    : function(event, ID, file, response, data) { console.log(file);}
        });
    });

那是我的JS档案......

<?php
/*
Uploadify v2.1.4
Release Date: November 8, 2010

Copyright (c) 2010 Ronnie Garcia, Travis Nickels

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
include_once($_SERVER['DOCUMENT_ROOT'].'/wp-config.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/wp-includes/wp-db.php');


error_reporting(E_ALL);
ini_set("display_errors", 1); 

if (!empty($_FILES)) {

    $tempFile = $_FILES['file_upload']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
    $targetFile =  str_replace('//','/',$targetPath) . $_FILES['file_upload']['name'];
    $fileTypes = array('jpg','jpeg','gif','png'); // File extensions

                global $current_user; 
                $current_user = wp_get_current_user();
                $userid = $current_user->ID;
                $username = $current_user->user_login;
                $email =  $current_user->user_email;
                $first_name = $current_user->user_firstname;
                $last_name = $current_user->user_lastname;
                $upload_time = date( 'Y-m-d H:i:s', time());
                $upload_url =  $targetFile;
                $s3userIP = $_SERVER["REMOTE_ADDR"];
                $description = $_POST['filedesc'];

if  (move_uploaded_file($tempFile,$targetFile))
{
    $insertuser = mysql_query("INSERT INTO wp_110504s3userDBinfo( upload_time, user_name, email_add, description, upload_url,s3_userIP) values  ('$upload_time','".$username."', '".$email."', '".$description."', '".$upload_url."', '".$s3userIP."')") or die(mysql_error());
                   }

    }
?>

那就是Uploadfiy.php 网址为Uploadify URL is

其实我正在使用worpress cms我希望这个到wordpress请检查什么问题

1 个答案:

答案 0 :(得分:0)

问题出在uploadify.php脚本中。您必须将输入文件名修改为Filedata,或者必须使用$_FILES['Filedata']

在uploadify.php $_FILES['file_upload']中进行修改
if (!empty($_FILES)) {
    $tempFile = $_FILES['file_upload']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
    $targetFile =  str_replace('//','/',$targetPath) . $_FILES['file_upload']['name'];
    move_uploaded_file($tempFile,$targetFile);
    echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);

}

(file_upload是您的<input type='file' id='file_upload' name='file_upload' />

编辑:

您必须将其添加到您的脚本中:

'onSelect'      : function(event,ID,fileObj) {

          $('#file_upload').uploadifySettings('scriptData',{filedesc:$('#filedesc').val()});

        }
像这样:

jQuery("#file_upload").uploadify({
        'uploader'      :       'uploadify.swf',
             'script'               :   'uploadify.php',
        'folder'                :   '/wp-content/uploads',
        'auto'              :   true,
        'buttonText'            :   'Browse',
        'cancelImg'     :   'cancel.png',
        'fileExt'       :   '*.*',
        'fileDataName'      :   'file_upload',
        'simUploadLimit'        :   2,
        'multi'         :   true,
        'auto'          :   true,
        'method'        :       'post',
        'onComplete'    :   function(event, ID, file, response, data) {},

        'onSelect'      : function(event,ID,fileObj) {

          $('#file_upload').uploadifySettings('scriptData',{filedesc:$('#filedesc').val()});

        }

    });

你的问题是你在init上设置了scriptData,那时你的描述是空的。所以我更新了onSelect事件的脚本数据值

你得到错误但你无法阅读它们,所以把它们放到txt文件中。 把这个放在你的uploadify.php后面这行

$insertuser = mysql_query("INSERT INTO wp_110504s3userDBinfo( upload_time, user_name, email_add, description, upload_url,s3_userIP) values  ('$upload_time','".$username."', '".$email."', '".$description."', '".$upload_url."', '".$s3userIP."')") or die(mysql_error());

    $flname= "testFile.txt";
    $flHandle= fopen($flname, 'w') or die("can't open file");
    $wr = fwrite($flHandle,mysql_errors());
    fclose($ourFileHandle);

如果你在服务器上运行它(不是在windows上的localhost),你必须设置权限

这个脚本。之后打开txt文件并读取错误。如果你想确保php正确接收参数,你可以用描述发送名称制作一个目录。

在uploadify中的

:mkdir($ _ POST ['filedesc']);