• 吴裕雄--天生自然PHP-MySQL-JavaScript学习笔记:实用PHP技术


    <?php
      $month = 9;    // September (only has 30 days)
      $day   = 31;   // 31st
      $year  = 2018; // 2018
    
      if (checkdate($month, $day, $year)) echo "Date is valid";
      else echo "Date is invalid";
    ?>
    <?php // testfile.php
      $fh = fopen("testfile.txt", 'w') or die("Failed to create file");
    
      $text = <<<_END
    Line 1
    Line 2
    Line 3
    _END;
    
      fwrite($fh, $text) or die("Could not write to file");
      fclose($fh);
      echo "File 'testfile.txt' written successfully";
    ?>
    <?php
      $fh = fopen("testfile.txt", 'r') or              
        die("File does not exist or you lack permission to open it");
    
      $line = fgets($fh);
      fclose($fh);
      echo $line;
    ?>
    <?php
      $fh = fopen("testfile.txt", 'r') or
        die("File does not exist or you lack permission to open it");
    
      $text = fread($fh, 3);
      fclose($fh);
      echo $text;
    ?>
    <?php // copyfile.php
      copy('testfile.txt', 'testfile2.txt') or die("Could not copy file");
      echo "File successfully copied to 'testfile2.txt'";
    ?>
    <?php // copyfile2.php
      if (!copy('testfile.txt', 'testfile2.txt'))
        echo "Could not copy file";
      else echo "File successfully copied to 'testfile2.txt'";
    ?>
    <?php // movefile.php
      if (!rename('testfile2.txt', 'testfile2.new'))
        echo "Could not rename file";
      else echo "File successfully renamed to 'testfile2.new'";
    ?>
    <?php // deletefile.php
      if (!unlink('testfile2.new')) echo "Could not delete file";
      else echo "File 'testfile2.new' successfully deleted";
    ?>
    <?php // update.php
      $fh   = fopen("testfile.txt", 'r+') or die("Failed to open file");
      $text = fgets($fh);
    
      fseek($fh, 0, SEEK_END);
      fwrite($fh, "$text") or die("Could not write to file");
      fclose($fh);
    
      echo "File 'testfile.txt' successfully updated";
    ?>
    <?php
      $fh = fopen("testfile.txt", 'r+') or die("Failed to open file");
      $text = fgets($fh);
    
      if (flock($fh, LOCK_EX))
      {
        fseek($fh, 0, SEEK_END);
        fwrite($fh, "$text") or die("Could not write to file");
        flock($fh, LOCK_UN);
      }
    
      fclose($fh);
      echo "File 'testfile.txt' successfully updated";
    ?>
    <?php
      echo "<pre>"; // Enables display of line feeds
      echo file_get_contents("testfile.txt");
      echo "</pre>"; // Terminates pre tag
    ?>
    <?php
      echo file_get_contents("http://oreilly.com");
    ?>
    <?php // upload.php
      echo <<<_END
        <html><head><title>PHP Form Upload</title></head><body>
        <form method='post' action='upload.php' enctype='multipart/form-data'>
        Select File: <input type='file' name='filename' size='10'>
        <input type='submit' value='Upload'>
        </form>
    _END;
    
      if ($_FILES)
      {
        $name = $_FILES['filename']['name'];
        move_uploaded_file($_FILES['filename']['tmp_name'], $name);
        echo "Uploaded image '$name'<br><img src='$name'>";
      }
    
      echo "</body></html>";
    ?>
    <?php //upload2.php
      echo <<<_END
        <html><head><title>PHP Form Upload</title></head><body>
        <form method='post' action='upload2.php' enctype='multipart/form-data'>
        Select a JPG, GIF, PNG or TIF File:
        <input type='file' name='filename' size='10'>
        <input type='submit' value='Upload'></form>
    _END;
    
      if ($_FILES)
      {
        $name = $_FILES['filename']['name'];
    
        switch($_FILES['filename']['type'])
        {
          case 'image/jpeg': $ext = 'jpg'; break;
          case 'image/gif':  $ext = 'gif'; break;
          case 'image/png':  $ext = 'png'; break;
          case 'image/tiff': $ext = 'tif'; break;
          default:           $ext = '';    break;
        }
        if ($ext)
        {
          $n = "image.$ext";
          move_uploaded_file($_FILES['filename']['tmp_name'], $n);
          echo "Uploaded image '$name' as '$n':<br>";
          echo "<img src='$n'>";
        }
        else echo "'$name' is not an accepted image file";
      }
      else echo "No image has been uploaded";
    
      echo "</body></html>";
    ?>
    <?php // exec.php
      $cmd = "dir";   // Windows
      // $cmd = "ls"; // Linux, Unix & Mac
    
      exec(escapeshellcmd($cmd), $output, $status);
    
      if ($status) echo "Exec command failed";
      else
      {
        echo "<pre>";
        foreach($output as $line) echo htmlspecialchars("$line
    ");
        echo "</pre>";
      }
    ?>
    <?php
      echo "<pre>"; // Enables viewing of the spaces
    
      // Pad to 15 spaces
      printf("The result is $%15f
    ", 123.42 / 12);
    
      // Pad to 15 spaces, fill with zeros
      printf("The result is $%015f
    ", 123.42 / 12);
    
      // Pad to 15 spaces, 2 decimal places precision
      printf("The result is $%15.2f
    ", 123.42 / 12);
    
      // Pad to 15 spaces, 2 decimal places precision, fill with zeros
      printf("The result is $%015.2f
    ", 123.42 / 12); 
    
      // Pad to 15 spaces, 2 decimal places precision, fill with # symbol
      printf("The result is $%'#15.2f
    ", 123.42 / 12); 
    ?>
    <?php
      echo "<pre>"; // Enables viewing of the spaces
    
      $h = 'Rasmus';
    
      printf("[%s]
    ",        $h); // Standard string output
      printf("[%12s]
    ",      $h); // Right justify with spaces
      printf("[%-12s]
    ",     $h); // Left justify with spaces
      printf("[%012s]
    ",     $h); // Zero padding
      printf("[%'#12s]
    
    ",  $h); // Use the custom padding character '#'
    
      $d = 'Rasmus Lerdorf';
    
      printf("[%12.8s]
    ",    $d); // Right justify, cutoff of 8 characters
      printf("[%-12.12s]
    ",   $d); // Left justify, cutoff of 12 characters
      printf("[%-'@12.10s]
    ", $d); // Left justify, pad '@', cutoff 10 chars
    ?>
  • 相关阅读:
    I firmly believe
    深度学习常见的专业术语
    阿里、网易和腾讯面试题 C/C++
    Winfrom中关于toolStrip工具栏中按钮背景的设置
    非常完善的Log4net详细说明
    C#中 Var关键字
    C#中Dynamic关键字
    Python中threading的join和setDaemon的区别[带例子]
    pycharm常用快捷键
    ABP发布到iis
  • 原文地址:https://www.cnblogs.com/tszr/p/12380930.html
Copyright © 2020-2023  润新知