代码之家  ›  专栏  ›  技术社区  ›  Interactive

csv文件不会通过php uploader上传[duplicate]

  •  -1
  • Interactive  · 技术社区  · 7 年前

    我在WordPress中运行了这个自定义上传脚本,因为我不想/不需要使用内置的WordPress脚本。
    The file test.csv could not be copied, try again 无法理解为什么会出现此错误!

    <?php
    $updir = 'wp-content/themes/ddpp/upload_csv/';
    $max_size = 100;  
    $allowtype = array('csv'); 
    if (isset($_FILES['field_name'])) {
    
    if ($_FILES['field_name']['error'] > 0) {
    echo 'Error: '. $_FILES['field_name']['error']. '<br />';
    }
    else {
    // get the name, size (in kb) and type (the extension) of the file
    $fname = $_FILES['field_name']['name'];
    $fsize = $_FILES['field_name']['size'] / 1024;
    $ftype = end(explode('.', strtolower($fname)));
    
    // checks if the file already exists
    if (file_exists($updir. $fname)) {
      echo 'The file: '. $fname. ' already exists';
    }
    else {
      // if the file not exists, check its type (by extension) and size
      if (in_array($ftype, $allowtype)) {
        // check the size
        if ($fsize <= $max_size) {
          // uses  function to copy the file from temporary folder to $updir
          if (!move_uploaded_file ($_FILES['field_name']['tmp_name'], $updir. $fname)) {
            echo 'The file '. $fname. ' could not be copied, try again';
          }
          else {
            echo $fname. ' ('. $fsize. ' kb) was successfully uploaded';
          }
        }
        else {
          echo 'The file '. $fname. ' exceeds the maximum permitted size, '. $max_size. ' KB';
        }
      }
      else {
        echo $fname. ' - invalid file type';
      }
    }
      }
    }
    ?>
    <div style="display: inline-block; width: 25%; vertical-align: top;">
    <form action="" method="post" enctype="multipart/form-data">
        <input type="file" name="field_name" /><br />
        <input type="submit" name="submit" value="Submit" />
    </form>
    

    更新

    Strict Standards: Only variables should be passed by reference in /Applications/MAMP/htdocs/ddpp/wp-content/plugins/ddpp_csv_upload/index.php on line 182
    
    Warning: move_uploaded_file(wp-content/themes/ddpp/upload_csv/test.csv): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/ddpp/wp-content/plugins/ddpp_csv_upload/index.php on line 194
    
    Warning: move_uploaded_file(): Unable to move '/Applications/MAMP/tmp/php/phpvT6vFM' to 'wp-content/themes/ddpp/upload_csv/test.csv' in /Applications/MAMP/htdocs/ddpp/wp-content/plugins/ddpp_csv_upload/index.php on line 194
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Blue    7 年前

    让我们了解为什么会发生这种情况。你得到了错误 The file test.csv could not be copied, try again ,因此问题在于:

    if (!move_uploaded_file ($_FILES['field_name']['tmp_name'], $updir. $fname))
    

    move_uploaded_file() ,以下是错误的原因:

    如果文件名不是有效的上载文件,则不会执行任何操作,move\u upload\u file()将返回FALSE。

    如果文件名是有效的上载文件,但由于某种原因无法移动,则不会发生任何操作,move\u uploaded\u file()将返回FALSE。 此外,将发出警告。

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

    最常见的问题是文件目录不可写,因为PHP无法在目标目录中创建文件名。确保目录存在,并且PHP进程可以写入该文件夹。

        2
  •  0
  •   Dan Hastings    7 年前

    代码在此失败

      if (!move_uploaded_file ($_FILES['field_name']['tmp_name'], $updir. $fname)) {
        echo 'The file '. $fname. ' could not be copied, try again';
      }
    

    这意味着move_uploaded_文件无法执行您需要它执行的操作。检查php错误日志,可能会出现权限错误。您可能使用FTP或其他方法创建了目录,因此apache无法写入该目录。

    chown -R apache /wherever/the/dir/is