插件11:上传文件

本文详细介绍了PHP文件上传插件的使用方法,包括插件功能、参数说明及常见错误处理。通过实例演示如何接收并处理上传的文件,确保上传过程顺利进行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php // Plug-in 11: Upload File

// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link
// You will need a jpg of under 100Kb on your PC for testing

echo <<<_END
<form method="post"  action="$_SERVER[PHP_SELF]"
     enctype="multipart/form-data">
<input  type="hidden"  name="flag" value="1" />
<input  type="file"    name="test" />
<input  type="submit" value="Upload" /></form>
_END;

if (isset($_POST['flag']))
{
   $result = PIPHP_UploadFile("test",
      array("image/jpeg", "image/pjpeg"), 100000);
   if ($result[0] == 0)
   {
      file_put_contents("test.jpg", $result[2]);
      echo "File received with the type '$result[1]' and saved ";
      echo "as <a href='test.jpg'>test.jpg</a><br />";
   }
   else
   {
      if ($result[0] == -2) echo "Wrong file type<br />";
      if ($result[0] == -3) echo "Maximum length exceeded<br />";
      if ($result[0] > 0)   echo "Error code: $result<br />";
      echo "File upload failed<br />";
   }
}

function PIPHP_UploadFile($name, $filetypes, $maxlen)
{
   // Plug-in 11: Upload File
   //
   // This plug-in saves an uploaded file to the hard disk
   // The arguments required are:
   //
   //    $name:      Name of form field used to upload file
   //    $filetypes: Array of Accepted mime types
   //    $maxlen:    Maximum allowable file size
   //
   // The plug-in returns a three-element array, the first of
   // which has one of the following numeric values:
   //     0 = Success
   //    -1 = Upload failed
   //    -2 = Wrong file type
   //    -3 = File too large
   //     1 = File exceeds upload_max_filesize defined in php.ini
   //     2 = File exceeds MAX_FILE_SIZE directive in HTML form
   //     3 = File was only partially uploaded
   //     4 = No file was uploaded
   //     6 = PHP is missing a temporary folder
   //     7 = Failed to write file to disk
   //     8 = File upload stopped by extension
   //
   // Upon success, the second element of the returned
   // array contains the uploaded file type and the third
   // the contents of the file.
   
   if (!isset($_FILES[$name]['name']))
      return array(-1, NULL, NULL);

   if (!in_array($_FILES[$name]['type'], $filetypes))
      return array(-2, NULL, NULL);
 
   if ($_FILES[$name]['size'] > $maxlen)
      return array(-3, NULL, NULL);

   if ($_FILES[$name]['error'] > 0)
      return array($_FILES[$name]['error'], NULL, NULL);
      
   $temp = file_get_contents($_FILES[$name]['tmp_name']);
   return array(0, $_FILES[$name]['type'], $temp);
}

?>

插件说明:

插件11接受一个表单域的名称,利用这个表单域把文件上传到web服务器,并返回这个上床文件名,上传成功后,发返回一个两元素的数组,第一个元素的值为0,第二个元素是上传的文件名。上传失败时,返回只有一个元素的数组,这个元素的值科恩那个是一下值之一:

“-1” 代表上传失败;

“-2” 代表文件类型错误;

“-3” 代表文件太大;

“1“ 代表文件大小超过php.ini文件中upload_max_filesize参数规定的大小;

”2“ 代表文件大小出熬过HTML表单中MAX_FILE_SIZE指令规定的最大值;

”3“ 代表文件只部分上传;

”4“ 代表没有文件上传;

”6“ 代表PHP没有临时文件夹;

”7“ 代表无法把上传文件保存到磁盘里;

”8“ 代表由于扩展名的原因,文件的上传被中止。

本插件需要以下参数:

$name 代表表单域中输入的上传文件名。

$filetype 数组,包含了受支持的文件类型(mime).

$maxlen 整数,表示上传文件的最大值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值