Simple file upl...LOGIN

Simple file upload to MySql database developed in PHP (3)

In this section, we will set up several custom functions to generate new file addresses and save them in the database.

First of all, the pictures we upload have an address, such as 123.jpg, abc.png, etc.

We need to retain the .jpg, .png suffix

Use two functions to intercept the suffix name of the original file path

strrchr() The function finds the last occurrence of a string in another string and returns all characters from that position to the end of the string.

The substr() function returns a part of a string.

<?php
function fileext($filename)
{  
  return substr(strrchr($filename, '.'), 1);
}
?>

fileext is the function name we set, and filename is the original file name.

Next we will generate a new path name to save in the database

We also need to customize a function random

Set a prefix CR- , Randomly select a few from the letters A-Z, a-z, 0-9 and mix them to generate a new path name prefix

Use the function: strlen() The function returns the length of the string.

<?php
function random($length)
{
  $hash = 'CR-';
  $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
  $max = strlen($chars) - 1;
  mt_srand((double)microtime() * 1000000);
  for($i = 0; $i < $length; $i++)
  {
    $hash .= $chars[mt_rand(0, $max)];
  }
  return $hash;
}
?>

Generate target file name

<?php
$filename=explode(".",$_FILES['file']['name']);
  do{
    $filename[0]=random(10); //设置随机数长度
    $name=implode(".",$filename);
      $uploadfile = $uploaddir.$name;
  }
  while(file_exists($uploadfile));
?>

explode() function breaks the string into an array.

implode() function returns a string composed of array elements.

Finally package all the files into a PHP file upload.php (the name can be created according to needs and functions)

<?php
$uploaddir = "upfiles/";//设置文件保存目录 注意包含/
$type=array("jpg","gif","bmp","jpeg","png");//设置允许上传文件的类型
//获取文件后缀名函数
function fileext($filename)
{
  return substr(strrchr($filename, '.'), 1);
}
//生成随机文件名函数
function random($length)
{
  $hash = 'CR-';
  $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
  $max = strlen($chars) - 1;
  mt_srand((double)microtime() * 1000000);
  for($i = 0; $i < $length; $i++)
  {
    $hash .= $chars[mt_rand(0, $max)];
  }
  return $hash;
}
$a = strtolower(fileext($_FILES['file']['name']));
//判断文件类型
if(!in_array(strtolower(fileext($_FILES['file']['name'])),$type))
{
  $text=implode(",",$type);
  echo "您只能上传以下类型文件: ",$text,"<br>";
}
//生成目标文件的文件名
else{
  $filename=explode(".",$_FILES['file']['name']);
  do{
    $filename[0]=random(10); //设置随机数长度
    $name=implode(".",$filename);
    $uploadfile = $uploaddir.$name;
  }
  while(file_exists($uploadfile));
  
  if (move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile))
  {
    if(is_uploaded_file($_FILES['file']['tmp_name']))
    {
      echo "上传失败!";
    }
    else
    {//输出图片预览
      echo "<tr><td>您的文件已经上传完毕 上传图片预览: <br><img src='$uploadfile'></td></tr>";
      echo "<tr><td><a href='tu2.php'style='margin-left: 3%;'>继续上传</a></td></tr>";
    }  //可以在前端HTML页面显示上传的文件预览
  }
}
?>


Next Section
<?php $uploaddir = "upfiles/";//设置文件保存目录 注意包含/ $type=array("jpg","gif","bmp","jpeg","png");//设置允许上传文件的类型 //获取文件后缀名函数 function fileext($filename) { return substr(strrchr($filename, '.'), 1); } //生成随机文件名函数 function random($length) { $hash = 'CR-'; $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; $max = strlen($chars) - 1; mt_srand((double)microtime() * 1000000); for($i = 0; $i < $length; $i++) { $hash .= $chars[mt_rand(0, $max)]; } return $hash; } $a = strtolower(fileext($_FILES['file']['name'])); //判断文件类型 if(!in_array(strtolower(fileext($_FILES['file']['name'])),$type)) { $text=implode(",",$type); echo "您只能上传以下类型文件: ",$text,"<br>"; } //生成目标文件的文件名 else{ $filename=explode(".",$_FILES['file']['name']); do{ $filename[0]=random(10); //设置随机数长度 $name=implode(".",$filename); $uploadfile = $uploaddir.$name; } while(file_exists($uploadfile)); if (move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile)) { if(is_uploaded_file($_FILES['file']['tmp_name'])) { echo "上传失败!"; } else {//输出图片预览 echo "<tr><td>您的文件已经上传完毕 上传图片预览: <br><img src='$uploadfile'></td></tr>"; echo "<tr><td><a href='tu2.php'style='margin-left: 3%;'>继续上传</a></td></tr>"; } //可以在前端HTML页面显示上传的文件预览 } } ?>
submitReset Code
ChapterCourseware