PHP文件上传问题。文件根本没有上传

时间:2016-07-27 12:48:35

标签: php html file-upload

我试图从2小时开始调试代码,但在这里找不到问题。此代码不上传任何文件!它也没有显示任何错误。好像服务器根本没有收到文件!

这是html部分

<form action="?" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <label>Select file: (Max size: 2mb)</label>
  <input type="file" name="xls_file" id="xls_file" /> <input type='hidden' value='55' name='yo' />
  <input type="submit" name="submit" id="submit" value="Submit" />
</form>

这是PHP部分

    if(isset($_FILES['xls_file'])) {
    if ($_FILES['xls_file']['error'] !== UPLOAD_ERR_OK) {
   die("Upload failed with error code " . $_FILES['xls_file']['error']);
    }}

if(isset($_FILES['xls_file'])){
$file_name = $_FILES['xls_file']['name'];
$allowed =  array('xls','xlsx','csv');
$ext = pathinfo($file_name, PATHINFO_EXTENSION);

$file_size =$_FILES['xls_file']['size'];
$file_tmp =$_FILES['xls_file']['tmp_name'];
$file_type=$_FILES['xls_file']['type'];

//check for valid file
if(!in_array($ext,$allowed)) {
    echo "Invalid file type.";
    exit;
    }

//check for file size
if(($file_size/1000) > 2100){
    echo "File size greater than allowed limit. Kindly choose a small file. ";
    exit;
    }

move_uploaded_file($file_tmp,"./".str_replace(array('"',"'","\\","=","<",">","`",";"), '', $file_name));
$uploaded = true;
} 

这是完整的代码

    <?php
$G_POST = str_replace(array('"',"'","\\","=","<",">","`",";"), '', $_POST);
$G_GET = str_replace(array('"',"'","\\","=","<",">","`",";"), '', $_GET);


if($G_POST[filename])
        $uploaded = true;

    if(isset($_FILES['xls_file'])) {
    if ($_FILES['xls_file']['error'] !== UPLOAD_ERR_OK) {
   die("Upload failed with error code " . $_FILES['xls_file']['error']);
    }}

    if(isset($_FILES['xls_file'])){
    $file_name = $_FILES['xls_file']['name'];
    $allowed =  array('xls','xlsx','csv');
    $ext = pathinfo($file_name, PATHINFO_EXTENSION);

    $file_size =$_FILES['xls_file']['size'];
    $file_tmp =$_FILES['xls_file']['tmp_name'];
    $file_type=$_FILES['xls_file']['type'];

    //check for valid file
    if(!in_array($ext,$allowed)) {
        echo "Invalid file type.";
        exit;
        }

    //check for file size
    if(($file_size/1000) > 2100){
        echo "File size greater than allowed limit. Kindly choose a small file. ";
        exit;
        }

    move_uploaded_file($file_tmp,"./".str_replace(array('"',"'","\\","=","<",">","`",";"), '', $file_name));
    $uploaded = true;
    } 

?>
<!DOCTYPE html>
<html>
<head>
<title>Excel (xls,xlsx,csv) to VCF (vCard) Online Converter - The Web Vendor</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>

<div data-role="page">
  <div data-role="main" class="ui-content">

  <?php 
  //STEP 1, ON LOAD
  if(!$uploaded) {
  ?>
  <h2>Excel to VCF Converter</h2>
<p>This is a simple, 3-step tool to convert your contact details in excel / spreadsheet to vCard (3.0) format. Supported formats are: xls, xlsx and csv.</p>
<p>Must Watch video tutorial here: <a href="https://www.youtube.com/watch?v=sn7ROzt9YRA" target="_blank">https://www.youtube.com/watch?v=sn7ROzt9YRA</a></p>
<hr/>
<h3>Step 1:</h3>
    <form action="?" method="post" enctype="multipart/form-data" name="form1" id="form1">
      <label>Select file: (Max size: 2mb)</label>
      <input type="file" name="xls_file" id="xls_file" /> <input type='hidden' value='55' name='yo' />
      <input type="submit" name="submit" id="submit" value="Submit" />
    </form>
  <?php } 
  //STEP 2, AFTER FILE UPLOAD
    else if(!$G_POST[filename]) {
        ?>
        <form action="?" method="post" name="step2">
        <h3>Step 2:</h3>
            <label>Uploaded file:</label>
            <input type="text" name="filename" id="filename" readonly="readonly" value="<?php echo str_replace(array('"',"'","\\","=","<",">","`",";"), '', $file_name) ?>" />
            <label>Sheet no: </label>
            <input name="sheet" type="number" required="required" id="sheet" max="9999" min="1" value="1" />
            <label>First Column:</label>
            <select name="first_column" id="first_column" required >
        <option value="A">A</option>
        <option value="B">B</option>
        <option value="C">C</option>
        <option value="D">D</option>
        <option value="E">E</option>
        <option value="F">F</option>
        <option value="G">G</option>
        <option value="H">H</option>
        <option value="I">I</option>
        <option value="J">J</option>
        <option value="K">K</option>
        <option value="L">L</option>
        <option value="M">M</option>

      </select>
            <label>Last Column:</label>
            <select name="last_column" id="last_column" required >
        <option value="A">A</option>
        <option value="B">B</option>
        <option value="C">C</option>
        <option value="D">D</option>
        <option value="E">E</option>
        <option value="F">F</option>
        <option value="G">G</option>
        <option value="H">H</option>
        <option value="I">I</option>
        <option value="J">J</option>
        <option value="K">K</option>
        <option value="L">L</option>
        <option value="M">M</option>
        <option value="N">N</option>
      </select>
            <label>First Row:</label>
            <input type="number" name="first_row" id="first_row" min="1" max="9999" required="required" />
            <label>Last Row:</label>
            <input type="number" name="last_row" id="last_row" min="1" max="9999" required="required" />
            <input type="submit" value="Submit">

            </form>
            <?php
    }

  ?>

  </div>
</div>

</body>
</html>

我很有经验。但从未遇到任何此类问题。

1 个答案:

答案 0 :(得分:0)

正如@RahulPatel指出的那样,事情是jquery.mobile在处理表单数据时使用ajax。文件不能与ajax一起提交。所以我们需要告诉j.mobile关闭这个表单的ajax。因此,我们需要在表单定义中添加以下内容

data-ajax="false"

所以代码将是

<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1" data-ajax="false">

感谢https://forum.jquery.com/topic/file-upload-in-jquery-mobile