Advertisement
Guest User

Untitled

a guest
Nov 6th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.17 KB | None | 0 0
  1. <?php
  2. $valid_images = array('jpg','gif','png','jpeg');
  3. $valid_docs = array('docx','doc','pdf','rtf');
  4. $valid_maxSize = 500000;
  5. $target_dir = "/home/ce2/ch63/public_html/Lab2/uploads/";
  6. $filename = basename($_FILES["doc"]["name"]);
  7. $target_file = $target_dir . $filename;
  8. $fileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
  9. if(isset($_POST["Submit"])) {
  10.     if($_FILES["doc"]["size"] < $valid_maxSize) {
  11.         if(in_array($fileType, $valid_images)) {
  12.             // Make sure that the file is a valid image
  13.             if(getimagesize($_FILES["doc"]["tmp_name"]) !== false) uploadFile($target_file, $filename);
  14.             else thrown_error('1');
  15.         } else if(in_array($fileType, $valid_docs)) uploadFile($target_file, $filename);
  16.         else thrown_error('2');
  17.     } else thrown_error('3');
  18. } else {
  19.     echo "<p>Nothing to be done</p>\n";
  20. }
  21. function uploadFile($target_file, $filename) {
  22.     // Create file if it doesn't exist
  23.     if (file_exists($target_file)) unlink($target_file) or die("Couldn't delete file");
  24.     if(move_uploaded_file($_FILES["doc"]["tmp_name"], $target_file)) {
  25.         echo '<h2>The file <a href="http://www2.macs.hw.ac.uk/~ch63/Lab2/myfirstcode.php"'.
  26.             $filename.'">'.$filename.'</a> has been uploaded by '.$_POST["user"].'!</h2>';
  27.     } else thrown_error(4);
  28.     return;
  29. }
  30. function thrown_error($e) {
  31.     echo "<p>Error: $e</p>";
  32.     exit;
  33. }
  34. ?>
  35. <html>
  36. <body>
  37. <h4> Submit Your Photo and Name </h4>
  38. <form action="http://www2.macs.hw.ac.uk/~ch63/Lab2/myfirstcode.php"
  39.       method="post" enctype="multipart/form-data"> <p>
  40.         File <input type="file" name="doc"> <p>
  41.         Name <input name="user"> <input type="submit" name="Submit">
  42. </form>
  43. </body>
  44. </html>
  45. <?php
  46. //DATABASE CODE:
  47.  
  48. $db_connected = connectDB('ch63');
  49. $sql = "SELECT * FROM users WHERE username='".$_POST["user"]."'";
  50. $result=mysql_query($sql) or die($sql."<br>\n".mysql_error());
  51. while($row = mysql_fetch_array($result)) {
  52.     echo "Hello ";
  53.     foreach($row as $col) echo " $col "; echo "<br>\n";
  54. }
  55. exit;
  56. function connectDB($database='') {
  57.     global $db, $mysqluser, $mysqlpwd;
  58.     // initiate a database connection by giving a database name, username and password:
  59.     if($database=='') $database = 'ch63';
  60.     if($mysqluser=='') $mysqluser = 'ch63';
  61.     if(!isset($mysqlpwd)) $mysqlpwd = "F68I2StlNu";
  62.     $db = new db_connection("mysql");
  63.     if($db->connect("mysql-server-1.macs.hw.ac.uk", "", $mysqluser, $mysqlpwd, 0,$database)) return true;
  64.     else return false;
  65. }
  66. class db_connection {
  67.     var $connection;
  68.     // create a new connection object
  69.     function db_connection($type="") { }
  70.     // connect to the database server
  71.     function connect($host, $port, $login, $password, $pconnect, $database="") {
  72.         if($port) { $host .= ":$port"; }
  73.         if( !($this->connection = @mysql_connect($host, $login, $password)) ) return false;
  74.         if($database) if(!@mysql_select_db($database, $this->connection)) return false;
  75.         return true;
  76.     }
  77.     function query($query) {
  78.         return mysql_query($query, $this->connection);
  79.     }
  80.     function error() {
  81.         return mysql_error($this->connection);
  82.     }
  83. }
  84.  
  85. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement