Advertisement
Guest User

Untitled

a guest
Oct 31st, 2018
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.71 KB | None | 0 0
  1. <?php
  2. $target_dir = "/home/ce2/ch63/public_html/Lab1/uploads/";
  3. $filename = basename($_FILES["doc"]["name"]);
  4. $target_file = $target_dir . $filename;
  5. $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
  6. // Check if image file is a valid image
  7. /**
  8.  * @param $target_file
  9.  * @param $filename
  10.  * @param $user
  11.  */
  12. function fileExists($target_file, $filename, $user)
  13. {
  14.     if (!file_exists($target_file)) {
  15.         if (move_uploaded_file($_FILES["doc"]["tmp_name"], $target_file)) {
  16.             echo "<h2>The file <a href=\"http://www2.macs.hw.ac.uk/~ch63/Lab1/uploads/$filename\">$filename</a> has been uploaded by $user!</h2>";
  17.         } else {
  18.             echo "Sorry, there was an error uploading the file.";
  19.         }
  20.     } else {
  21.         unlink($target_file) or die("Couldn't delete file");
  22.         if (move_uploaded_file($_FILES["doc"]["tmp_name"], $target_file)) {
  23.             echo '<h2>The file <a href="http://www2.macs.hw.ac.uk/~ch63/Lab1/uploads/' . $filename . '">' . $filename . '</a> has been uploaded by ' . $_POST["user"] . '!</h2>';
  24.         } else {
  25.             echo "Sorry, there was an error uploading the file.";
  26.         }
  27.     }
  28. }
  29.  
  30. if (isset($_POST["Submit"])) {
  31.     if ($_FILES["doc"]["size"] < 500000) {
  32.         if ($imageFileType == 'gif' || $imageFileType == 'jpg') {
  33.             if (getimagesize($_FILES["doc"]["tmp_name"]) !== false) {
  34.                 // Create file if it doesn't exist
  35.                 fileExists($target_file, $filename, $user);
  36.             }
  37.         } else if ($imageFileType == 'docx' || $imageFileType == 'doc') {
  38. // Create file if it doesn't exist
  39.             fileExists($target_file, $filename, $user);
  40.         }
  41.     }
  42. }
  43. // If there are photos, loop across photos and show them
  44. if (sizeof($photos) > 0) {
  45.     foreach ($photos as $photo) {
  46.         echo "<p>Photo: $photo <input type='checkbox' name='picker' value='$photoID'>";
  47.     }
  48. }
  49. ?>
  50. <html>
  51. <body>
  52. <h4> Submit Your Photo and Name </h4>
  53. <form action="http://www2.macs.hw.ac.uk/~ch63/Lab1/myfirstscript.php"
  54.       method="post" enctype="multipart/form-data"><p>
  55.         File <input type="file" name="doc">
  56.     <p>
  57.         Name <input name="user"> <input type="submit" name="Submit">
  58. </form>
  59. </body>
  60. </html>
  61. <?php
  62. //DATABASE CODE:
  63. /*
  64. $db_connected = connectDB('yourDatabase');
  65. $sql = "SELECT * FROM users WHERE username='".$_POST["user"]."'";
  66. $result=mysql_query($sql) or die($sql."<br>\n".mysql_error());
  67. while($row = mysql_fetch_array($result)) {
  68.     echo "Hello ";
  69.     foreach($row as $col) echo " $col "; echo "<br>\n";
  70. }
  71. exit;
  72. function connectDB($database='') {
  73.     global $db, $mysqluser, $mysqlpwd;
  74.     // initiate a database connection by giving a database name, username and password:
  75.     if($database=='') $database = 'yourDatabase';
  76.     if($mysqluser=='') $mysqluser = 'yourUsername';
  77.     if(!isset($mysqlpwd)) $mysqlpwd = "yourPassword";
  78.     $db = new db_connection("mysql");
  79.     if($db->connect("mysql-server-1.macs.hw.ac.uk", "", $mysqluser, $mysqlpwd, 0,$database)) return true;
  80.     else return false;
  81. }
  82. class db_connection {
  83.     var $connection;
  84.     // create a new connection object
  85.     function db_connection($type="") { }
  86.     // connect to the database server
  87.     function connect($host, $port, $login, $password, $pconnect, $database="") {
  88.         if($port) { $host .= ":$port"; }
  89.         if( !($this->connection = @mysql_connect($host, $login, $password)) ) return false;
  90.         if($database) if(!@mysql_select_db($database, $this->connection)) return false;
  91.         return true;
  92.     }
  93.     function query($query) {
  94.         return mysql_query($query, $this->connection);
  95.     }
  96.     function error() {
  97.         return mysql_error($this->connection);
  98.     }
  99. }
  100. */
  101. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement