Jump to content

tottedaman

New Members
  • Posts

    3
  • Joined

  • Last visited

tottedaman's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. This code worked flawlessly! Thank you so much for taking the time to fix it! But I have one question, why is there filetypes jpg, jpeg, jpe etc defined in the if statement preg_match? I understand that the line is checking filenames but does it mean that uploads are limited to only those filetypes? I tried uploading a pdf-file and that worked as well as a .png file. Limiting what type of files is allowed to be uploaded isn't really necessary in my case since the webserver its hosted on is only locally and I'm don't plan to make it accessible over the internet either. And I'm probably gonna use it the most myself. And is it any file size limitations specified in the code? I can control the maximum file size in the php.ini instead.
  2. index.php <!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1, maximum-scale=1"> <title>Startsida</title> <link href="jquery-mobile/jquery.mobile.theme-1.0.min.css" rel="stylesheet" type="text/css"> <link href="jquery-mobile/jquery.mobile.structure-1.0.min.css" rel="stylesheet" type="text/css"> <script src="jquery-mobile/jquery-1.6.4.min.js" type="text/javascript"></script> <script src="jquery-mobile/jquery.mobile-1.0.min.js" type="text/javascript"></script> </head> <body> <div data-role="page" id="page"> <div data-role="header"> <h1>Selct a option</h1> </div> <div data-role="content"> <a href="upload_form.php" data-role="button">Upload</a> </div> </div> </body> </html> upload_form.php <!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1, maximum-scale=1"> <title>Sign in</title> <link href="jquery-mobile/jquery.mobile.theme-1.0.min.css" rel="stylesheet" type="text/css"> <link href="jquery-mobile/jquery.mobile.structure-1.0.min.css" rel="stylesheet" type="text/css"> <script src="jquery-mobile/jquery-1.6.4.min.js" type="text/javascript"></script> <script src="jquery-mobile/jquery.mobile-1.0.min.js" type="text/javascript"></script> </head> <body> <div data-role="page" id="page"> <div data-role="header"> <h1>Sign in</h1> <a href="index.php" data-role="button" data-icon="home">Back to start</a> </div> <div data-role="content"> <form action="login_upload.php" method="post" enctype="multipart/form-data"> <?php if (isset($_GET['error'])) { ?> <p class="error"><?php echo $_GET['error']; ?></p> <?php } ?> <label>Username:</label> <input type="text" name="uname"><br> <label>Password:</label> <input type="password" name="password"><br> <button type="submit">Sign in</button> </form> </div> </div> </body> </html> login_upload.php <?php session_start(); include "db_conn.php"; if (isset($_POST['uname']) && isset($_POST['password'])) { function validate($data){ $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } $uname = validate($_POST['uname']); $pass = validate($_POST['password']); if (empty($uname)) { header("Location: upload_form.php?error=Username is required!"); exit(); }else if(empty($pass)){ header("Location: upload_form.php?error=Password is required!"); exit(); }else{ $sql = "SELECT * FROM users WHERE user_name='$uname' AND password='$pass'"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) === 1) { $row = mysqli_fetch_assoc($result); if ($row['user_name'] === $uname && $row['password'] === $pass) { echo "Logged in!"; $_SESSION['user_name'] = $row['user_name']; $_SESSION['id'] = $row['id']; header("Location: upload_home.php"); exit(); }else{ header("Location: upload_form.php?error=Incorrect username or password"); exit(); } }else{ header("Location: upload_form.php?error=Incorrect username or password"); exit(); } } }else{ header("Location: index.php"); exit(); } upload_home.php <?php session_start(); if (isset($_SESSION['id']) && isset($_SESSION['user_name'])) { ?> <!DOCTYPE html> <html> <head> <title>Upload files</title> <link href="jquery-mobile/jquery.mobile.theme-1.0.min.css" rel="stylesheet" type="text/css"> <link href="jquery-mobile/jquery.mobile.structure-1.0.min.css" rel="stylesheet" type="text/css"> <script src="jquery-mobile/jquery-1.6.4.min.js" type="text/javascript"></script> <script src="jquery-mobile/jquery.mobile-1.0.min.js" type="text/javascript"></script> </head> <body> <div data-role="page" id="page"> <div data-role="header"> <h1>Upload files</h1> <a href="logout.php" data-role="button" data-icon="home">Sign out</a> </div> <div data-role="content"> <form action="fileUpload.php" method="post" enctype="multipart/form-data"> <input type="file" name="Upload"> <input type="submit"> </form> </div> </div> </body> </html> <?php }else{ header("Location: index.php"); exit(); } ?> db_conn.php <?php $sname= "localhost"; $unmae= "My username for mysql"; $password = "My password för mysql"; $db_name = "fildelning"; $conn = mysqli_connect($sname, $unmae, $password, $db_name); if (!$conn) { echo "Connection failed"; } fileUpload.php <html> <head> <title>Fileupload</title> </head> <body> <?php $dir = "./folder/"; $timestamp = time(); $filename = $dir.$timestamp.basename($_FILES['Upload']['name']); var_dump($_FILES); echo "<br><br>"; if (move_uploaded_file($_FILES['Upload']['tmp_name'], $filename)){ echo "<p>File was uploaded --> ".$_FILES['Upload']['name']; } else { echo "Upload failed".$_FILES['Upload']['name']; } echo "<p>Information about file from $ FILE array</p>"; echo "File Name: ".$_FILES['Upload']['name']."<br>"; echo "File Type: ".$_FILES['Upload']['type']."<br>"; echo "File Size: ".$_FILES['Upload']['size']."kB<br>"; ?> </body> </html> fileUpload.html <html> <body> <h1>File Upload Form</h1> <form action="fileUpload.php" method="post" enctype="multipart/form-data"> <input type="file" name="Upload"> <input type="submit"> </form> </body> </html>
  3. Hi So I'm pretty new to php and web development in general and I'm quiet stuck in my first project. The idea of my project is to make a simple login system where you can sign in with a user account and then get to a file upload where you can submit files to the server and user some jquery to make it look nice. I've gotten the login system to work as I want it to with connection to a mysql db and all. But I can't for the life of me get file uploads to work with my login system. It keeps saying that _FILES is empty and that a array key is undefined. But the weird part is that if I simply skip the entire login system and create just a simple HTML-form with a upload button and a seperate php-file then it works perfectly but as soon as I try to use that code in my project (after you get logged in using the login system) it just won't work and I can't for the life of me figure out what's wrong, my code seams fine and if I put the "simple files" in the same directory on the web server as my whole project the file uploads works. My code is quiet long an there's a few files, I will post them down below.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.