国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

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); //設(shè)置隨機(jī)數(shù)長(zhǎng)度
    $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/";//設(shè)置文件保存目錄 注意包含/
$type=array("jpg","gif","bmp","jpeg","png");//設(shè)置允許上傳文件的類型
//獲取文件后綴名函數(shù)
function fileext($filename)
{
  return substr(strrchr($filename, '.'), 1);
}
//生成隨機(jī)文件名函數(shù)
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>";
}
//生成目標(biāo)文件的文件名
else{
  $filename=explode(".",$_FILES['file']['name']);
  do{
    $filename[0]=random(10); //設(shè)置隨機(jī)數(shù)長(zhǎng)度
    $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
    {//輸出圖片預(yù)覽
      echo "<tr><td>您的文件已經(jīng)上傳完畢 上傳圖片預(yù)覽: <br><img src='$uploadfile'></td></tr>";
      echo "<tr><td><a href='tu2.php'style='margin-left: 3%;'>繼續(xù)上傳</a></td></tr>";
    }  //可以在前端HTML頁(yè)面顯示上傳的文件預(yù)覽
  }
}
?>


Continuing Learning
||
<?php $uploaddir = "upfiles/";//設(shè)置文件保存目錄 注意包含/ $type=array("jpg","gif","bmp","jpeg","png");//設(shè)置允許上傳文件的類型 //獲取文件后綴名函數(shù) function fileext($filename) { return substr(strrchr($filename, '.'), 1); } //生成隨機(jī)文件名函數(shù) 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>"; } //生成目標(biāo)文件的文件名 else{ $filename=explode(".",$_FILES['file']['name']); do{ $filename[0]=random(10); //設(shè)置隨機(jī)數(shù)長(zhǎng)度 $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 {//輸出圖片預(yù)覽 echo "<tr><td>您的文件已經(jīng)上傳完畢 上傳圖片預(yù)覽: <br><img src='$uploadfile'></td></tr>"; echo "<tr><td><a href='tu2.php'style='margin-left: 3%;'>繼續(xù)上傳</a></td></tr>"; } //可以在前端HTML頁(yè)面顯示上傳的文件預(yù)覽 } } ?>
submitReset Code