php code for uploading documents


#1

Please am working on a form that has upload section, which is meant to upload strictly word document but am having issues with the code, it’s like I have messed up the whole stuff, dont even know where to check. Can somebody help with a full script that I can just implement and get out of this till am able to figure out the error in mine. Tks for the response


#2

You need to make sure the form is multipart/form-data as it’s enctype.

See the attached file for a full script. By the way, best practice for hyour da


#3

Sorry, I was saying the best practice for your database will be not to store the image as a blob, it takes up so much space.

My example is a generic one, modify it to suit.


#4

Post your code and let us correct the errors…


#5

You can get PHP Manual from www.php.net online and get a step by step process on how to go about that or go to this address http://w3schools.com/php/php_file_upload.asp


#6

@OlSylas

<?php // Check if a file has been uploaded if(isset($_FILES['uploaded_file'])) { // Make sure the file was sent without errors if($_FILES['uploaded_file']['error'] == 0) { // Connect to the database $dbLink = new mysqli('contact', 'classe', '123456, 'cv'); if(mysqli_connect_errno()) { die("MySQL connection failed: ". mysqli_connect_error()); } // Gather all required data $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']); $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']); $data = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name'])); $size = intval($_FILES['uploaded_file']['size']); // Create the SQL query $query = " INSERT INTO `cv` ( `name`, `mime`, `size`, `data`, `created` ) VALUES ( '{$name}', '{$mime}', {$size}, '{$data}', NOW() )"; // Execute the query $result = $dbLink->query($query); // Check if it was successfull if($result) { echo 'Success! Your file was successfully added!'; } else { echo 'Error! Failed to insert the file' . " ``` {$dbLink->error} ``` "; } } else { echo 'An error accured while the file was being uploaded. ' . 'Error code: '. intval($_FILES['uploaded_file']['error']); } // Close the mysql connection $dbLink->close(); } else { echo 'Error! A file was not sent!'; } // Echo a link back to the main page echo '

Click here to go back

'; ?> MySQL file upload example

See all files

This is the error i get
Notice: Undefined index: file in C:\wamp\www\jquerysample\upload_file.php on line 2

Notice: Undefined index: file in C:\wamp\www\jquerysample\upload_file.php on line 3

Notice: Undefined index: file in C:\wamp\www\jquerysample\upload_file.php on line 4
Invalid file