csv文件不会通过php上传程序上传

时间:2017-06-30 10:59:23

标签: php html wordpress csv

我在WordPress中运行了这个自定义上传脚本,因为我不想/需要在WordPress脚本中使用构建。
如果我上传了一个csv文件错误: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>

UPDATE
以下是上传尝试后创建的一些警告

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 个答案:

答案 0 :(得分:1)

让我们理解为什么会这样。您收到错误dequeueOutputBuffer(),因此以下行是问题所在:

The file test.csv could not be copied, try again

根据move_uploaded_file()的文档,以下是错误的原因:

  

如果filename不是有效的上传文件,则不会执行任何操作,move_uploaded_file()将返回FALSE。

     

如果filename是有效的上传文件,但由于某种原因无法移动,则不会执行任何操作,move_uploaded_file()将返回FALSE。 此外,还会发出警告。

确保您已启用警告,以帮助您进一步诊断问题(将其添加到脚本顶部):

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

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

答案 1 :(得分:0)

代码在这里失败

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

这意味着move_uploaded_file无法执行您需要它执行的操作。检查您的PHP错误日志,可能会有关于权限的错误。您可能使用FTP或其他东西创建了目录,因此,apache无法写入目录。

为了获得apache访问权限,您有2个选项。将apache添加到允许写入的组中,或者只是将apache作为所有者。我总是发现,如果它是一个网站的目录,那么使apache成为自己的无所不同。如果您具有SSH访问权限,请运行以下命令。

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