Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- // file has been uploaded?
- if(isset($_FILES['fileuploads']))
- {
- // There is any error?
- if($_FILES['fileuploads']['error'] == 0)
- {
- // Database connection
- $dbconn = new mysqli('mysql17.000webhost.com', 'a2083177_mydb', 'password1', 'a2083177_mydb');
- if(mysqli_connect_errno())
- {
- die("MySQL connection failed: ". mysqli_connect_error());
- }
- // All required data
- $id = $dbconn->real_escape_string($_SESSION['user_id']);
- $name = $dbconn->real_escape_string($_FILES['fileuploads']['name']);
- $type = $dbconn->real_escape_string($_FILES['fileuploads']['type']);
- $data = $dbconn->real_escape_string(file_get_contents($_FILES ['fileuploads']['tmp_name']));
- $size = intval($_FILES['fileuploads']['size']);
- $fileName = $_FILES['fileuploads']['name']; // The file name
- $fileTmpLoc = $_FILES["fileuploads"]["tmp_name"]; // File in the PHP tmp folder
- move_uploaded_file($fileTmpLoc, "uploads/$fileName");
- //move_uploaded_file($_FILES['fileuploads']['name'] , "fileuploads/$name" );
- // Create the SQL query
- $query = "
- INSERT INTO `files` (`id`,`u_id`,`name`, `type`, `size`, `data`, `created`)
- VALUES ('{$id}','{$id}','{$name}', '{$type}', {$size}, '{$data}', NOW())";
- // Execute the query
- $result = $dbconn->query($query);
- // Check if it was successfull
- if($result)
- {
- header("location: homepage.php");
- echo 'Success! Your file was successfully uploaded!';
- }
- else
- {
- echo 'Error! Failed to upload the file'
- . "<pre>{$dbconn->error}</pre>";
- }
- }
- else
- {
- echo 'An error while the file was being uploaded. '
- . 'Error code: '. intval($_FILES['fileuploads']['error']);
- }
- // Close the mysql connection
- $dbconn->close();
- }
- else
- {
- echo 'Error! A file was not sent!';
- }
- // Echo a link back to the main page
- echo '<p>Click <a href="homepage.php">here</a> to go back</p>';
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement