无法通过自定义PHP上传器上传CSV文件

时间:2014-09-15 21:25:16

标签: php html csv

我试图创建一个非常简单的php上传器,只上传一个.CSV文件,我需要它来覆盖已经在服务器上的那个。我尝试使用上传部分,直到我开始工作。我已经在上传目录中设置了777权限,但是在上传文件时我不断收到错误。覆盖部分我还没有能够编码它,我可能需要一些帮助来实现它。 .csv文件是1.5MB。上传文件时收到的错误是文件扩展名错误,但我确信扩展名是.csv。

以下是代码:

upload.php的

  <?php
  // Configuration - Your Options
  $allowed_filetypes = array('.csv',); // These will be the types of file that will pass the validation.
  $max_filesize = 5000000; // Maximum filesize in BYTES (currently 5MB).
  $upload_path = 'upload/'; // Upload directory

  $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
  $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

   // Check if the filetype is allowed, if not DIE and inform the user.
   if(!in_array($ext,$allowed_filetypes))
  die('The file you attempted to upload is not a *.CSV file.');

   // Now check the filesize, if it is too large then DIE and inform the user.
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
  die('The file you attempted to upload is larger than 5MB.');

   // Check if we can upload to the specified path, if not DIE and inform the user.
   if(!is_writable($upload_path))
  die('You cannot upload to the specified directory, please CHMOD it to 777.');
   ?> 

HTML:

<form enctype="multipart/form-data" action="upload.php" method="GET">
Update CSV File: <input name="uploaded_file" type="file" />
<input type="submit" value="Upload" />
</form> 

2 个答案:

答案 0 :(得分:1)

您似乎正在检查$_FILE索引,其值为userfile,但您的表单正在发送值为uploaded_file

的表单
$filename = $_FILES['userfile']['name'];

应该是

$filename = $_FILES['uploaded_file']['name'];

答案 1 :(得分:0)

您可以通过更改进行以下更正   从GET到POST的方法 此外,因为文件输入的名称是“uploaded_file”。也改变

 $_FILES['userfile']['name'] to  $_FILES['uploaded_file']['name'];

绝对应该有用!