如何临时上传文件,然后提交表格


How to upload file temporarily and then submit form?

参见下面的编辑问题

我目前正在上传文件与以下代码。但是我想先上传文件,然后再提交表格。我的意思是在选择audiofile>>点击文件上传字段旁边的上传按钮后…>>消息=文件上传成功>>然后点击"提交"按钮上传所有数据和临时上传的文件在mysql和音频文件在相关文件夹....目前,这段代码我无法上传更大的文件。等了很长时间,我得到错误信息"网页不可用"我尝试了php.ini和.htaccess文件更新max_execution_time, upload_max_filesize等…另外,mod_security在服务器上被禁用…

PHP代码:
<?php
session_start();
// Database connection
include("../include/function.php");
include("commonfiles/validate.php");
// Create an object for an class
$bsq=new bsq();
//use class function to connect the database
$bsq->connect_db();
//
$fileName = $_FILES["audio"]["name"]; 
$fileNameNew = preg_replace('/'s+/', '_', $fileName);
$fileTmpLoc = $_FILES["audio"]["tmp_name"];
// Path and file name
$pathAndName = "uploadsaudio_admin/".$fileNameNew;
// Run the move_uploaded_file() function here
$moveResult = move_uploaded_file($fileTmpLoc, $pathAndName);
// Evaluate the value returned from the function if needed
$fileName1 = $_FILES["doc"]["name"]; 
$fileNameNew1 = preg_replace('/'s+/', '_', $fileName1);
$fileTmpLoc1 = $_FILES["doc"]["tmp_name"];
// Path and file name
$pathAndName1 = "uploadsaudiodoc_admin/".$fileNameNew1;
// Run the move_uploaded_file() function here
$moveResult1 = move_uploaded_file($fileTmpLoc1, $pathAndName1);
// Evaluate the value returned from the function if needed
$fileName2 = $_FILES["pdf"]["name"]; 
$fileNameNew2 = preg_replace('/'s+/', '_', $fileName2);
$fileTmpLoc2 = $_FILES["pdf"]["tmp_name"];
// Path and file name
$pathAndName2 = "uploadsaudioppt_admin/".$fileNameNew2;
// Run the move_uploaded_file() function here
$moveResult2 = move_uploaded_file($fileTmpLoc2, $pathAndName2);
// Evaluate the value returned from the function if needed
if($_POST['action']=="add"){
$all_columns[]="file_subject";
$all_columns[]="full_name";
$all_columns[]="upload_date";   
$all_columns[]="display_date";
$all_columns[]="message";   
    $all_columns[]="audio";
    $all_columns[]="doc";
    $all_columns[]="pdf";
    $display=date('Y-m-d', strtotime("+1 Day"));

//Get All values to insert in to table      
$all_values[]=addslashes($_POST["file_subject"]);
$all_values[]=addslashes($_POST["full_name"]);  
$all_values[]=addslashes($_POST["upload_date"]);
$all_values[]=$display; 
    $all_values[]=addslashes($_POST["message"]);
    $all_values[]=addslashes($pathAndName );
    $all_values[]=addslashes($pathAndName1 );
    $all_values[]=addslashes($pathAndName2 );

//=====================
$qry=$bsq->webdreaminsert("eo_uploadsaudio_by_admin",$all_columns,$all_values,'');
echo mysql_error();
header("location:upload_audiofile_for_downloading_list.php");
}   
?> 

HTML表单代码:

<form action="" method="post" enctype="multipart/form-data" name="addtwebinar1" id="addtwebinar1" onsubmit="javascript:return validateimage1();" >
 <input type="hidden" value="add" name="action" />
1) Audio File Subject* : <input name="file_subject" id="file_subject" type="text" value="" />
2) File Owner Name : <input name="full_name" id="full_name" type="text"  value="" />
3) Session Conducted On : <input type="text" autocomplete="off" name="upload_date" id="upload_date" placeholder="Click To Open calendar" readonly="readonly" />
4) Your Audio File For Upload : <label for="audio">Audio File To Upload: </label><br>
<input type="file" name="audio" id="audio" />
// Here I want temporary upload button with message "Upload successfully / Not Uploaded..." etc.....
5) Your Doc File For Upload : <label for="doc">Doc File To Upload: </label><br>
<input type="file" name="doc" id="doc" />
6) Your Question Answer File For Upload : <label for="pdf">Question Answer For Practice File To Upload: </label><br>
<input type="file" name="pdf" id="pdf" />
7) Message If Any : <textarea name="message" id="message" cols="" rows="3" placeholder="Message"></textarea>
<button>SUBMIT</button>
</form>

我想添加上传按钮旁边的字段4,其中音频文件将被临时上传,然后在填写其他字段后,表单将被提交....

或任何其他解决方案上载较大的文件(每个约40至50 MB大小)而不会出现任何错误,网速较低?


编辑问题:我使用ajax进行文件上传.....现在文件上传到文件夹,甚至26 MB,但mysql数据库没有更新.....

我用javascript编辑了HTML代码如下:

1) Audio File Subject* : <input name="file_subject" id="file_subject" type="text" value="" />
2) File Owner Name : <input name="full_name" id="full_name" type="text"  value="" />
3) Session Conducted On : <input type="text" autocomplete="off" name="upload_date" id="upload_date" placeholder="Click To Open calendar" readonly="readonly" />
4) Your Audio File For Upload : <label for="audio">Audio File To Upload: </label><br>
<input type="file" name="audio" id="audio" />
// Here I want temporary upload button with message "Upload successfully / Not Uploaded..." etc.....
5) Your Doc File For Upload : <label for="doc">Doc File To Upload: </label><br>
<input type="file" name="doc" id="doc" />
6) Your Question Answer File For Upload : <label for="pdf">Question Answer For Practice File To Upload: </label><br>
<input type="file" name="pdf" id="pdf" />
7) Message If Any : <textarea name="message" id="message" cols="" rows="3" placeholder="Message"></textarea>
<button>SUBMIT</button>
</form>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js">    </script>
    <script src="source/jquery.form.js"></script>
    <script>
        (function() {
        var bar = $('.bar');
        var percent = $('.percent');
        var status = $('#status');
        $('form').ajaxForm({
            beforeSend: function() {
                status.empty();
                var percentVal = '0%';
                bar.width(percentVal)
                percent.html(percentVal);
            },
            uploadProgress: function(event, position, total, percentComplete) {
                var percentVal = percentComplete + '%' + '' + 'Completed...';
                bar.width(percentVal)
                percent.html(percentVal);
            },
            success: function() {
                var percentVal = '100%';
                bar.width(percentVal)
                percent.html(percentVal);
            },
            complete: function(xhr) {
                status.html(xhr.responseText);
  window.location.href='upload_audiofile_for_downloading_list.php';
            }
        });
    })();
</script>

和代码 upload_audiofile_process.php :

<?php
session_start();
include("../include/function.php");
include("commonfiles/validate.php");
include("fckeditor.php");
//=================================================================================
// Create an object for an class
$bsq=new bsq();
//use class function to connect the database
$bsq->connect_db();
$fileName = $_FILES["image"]["name"]; 
$fileNameNew = preg_replace('/'s+/', '_', $fileName);
$fileTmpLoc = $_FILES["image"]["tmp_name"];
// Path and file name
$pathAndName = "uploadsaudio_admin/".$fileNameNew;

// Evaluate the value returned from the function if needed
$fileName1 = $_FILES["doc"]["name"]; 
$fileNameNew1 = preg_replace('/'s+/', '_', $fileName1);
$fileTmpLoc1 = $_FILES["doc"]["tmp_name"];
// Path and file name
$pathAndName1 = "uploadsaudiodoc_admin/".$fileNameNew1;
// Evaluate the value returned from the function if needed
$fileName2 = $_FILES["pdf"]["name"]; 
$fileNameNew2 = preg_replace('/'s+/', '_', $fileName2);
$fileTmpLoc2 = $_FILES["pdf"]["tmp_name"];
// Path and file name
$pathAndName2 = "uploadsaudioppt_admin/".$fileNameNew2;
// Evaluate the value returned from the function if needed
       if (isset($_FILES["image"])) {
       if ($_FILES["image"]["error"] > 0) {
    echo "Error: " . $_FILES["file"]["error"] . "<br>";
    } else {
        move_uploaded_file($fileTmpLoc, $pathAndName);
        move_uploaded_file($fileTmpLoc1, $pathAndName1);
        move_uploaded_file($fileTmpLoc2, $pathAndName2);
        $all_columns[]="file_subject";
        $all_columns[]="full_name";
        $all_columns[]="upload_date";   
        $all_columns[]="display_date";
        $all_columns[]="message";   
        $all_columns[]="image";
        $all_columns[]="doc";
        $all_columns[]="pdf";
        $display=date('Y-m-d', strtotime("+1 Day"));

    //Get All values to insert in to table      
        $all_values[]=addslashes($_POST["file_subject"]);
        $all_values[]=addslashes($_POST["full_name"]);  
        $all_values[]=addslashes($_POST["upload_date"]);
        $all_values[]=$display; 
        $all_values[]=addslashes($_POST["message"]);
        $all_values[]=addslashes($pathAndName );
        $all_values[]=addslashes($pathAndName1 );
        $all_values[]=addslashes($pathAndName2 );

    //=====================
        $qry=$bsq->webdreaminsert("eo_uploadsaudio_by_admin",$all_columns,$all_values,'');
echo mysql_error();
 }  
  }
 ?>

此代码适用于10-15 MB文件上传....但是如果文件大于20mb…不工作…

你可以用这种方法解决你的问题。

首先在上传按钮上调用ajax函数。

$.ajax({
       url: 'example.php',
       type: 'POST',
       data: fileData,
     success: function(data) {
         // set file name or file id in some field using jquery.
          $('#fileHiddenFieldId').val(data);
         // and then set css property 'display:block' for submit button.
         $('#submitButtonId').css('display','block');
     }
    });

这里url example.php是存放php文件上传代码的文件路径。'fileData'是你上传的文件数据。

当你得到成功的响应,然后设置文件id或文件名在一些隐藏的字段,然后显示提交按钮。