Hi,
I have this script below to upload files to my server, but what I need also is some additional code to insert the file url into the database that has just been uploaded. I have created a column in the database named file_url, might anyone know if this is possible? Thanks,
Phil
Here is the form code that goes with the script.
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288">
<fieldset><legend>Fill out the form to upload a file:</legend>
<p><b>File:</b> <input type="file" name="upload" /></p>
<p><b>Description:</b> <textarea name="description" cols="40" rows="5"></textarea></p>
</fieldset>
<div align="center"><input type="submit" name="submit" value="Submit" /></div>
</form><!-- End of Form -->
// Set the page title and include the HTML header.
$page_title = 'Upload a File';
if (isset($_POST['submit'])) { // Handle the form.
require_once ('./mysql_connect1.php'); // Connect to the database.
// Function for escaping and trimming form data.
function escape_data ($data) {
global $dbc;
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
return mysql_real_escape_string (trim ($data), $dbc);
} // End of escape_data() function.
// Check for a description (not required).
if (!empty($_POST['description'])) {
$d = escape_data($_POST['description']);
} else {
$d = '';
}
// Add the record to the database.
$query = "INSERT INTO uploads (file_name, file_size, file_type, description, upload_date) VALUES ('{$_FILES['upload']['name']}', {$_FILES['upload']['size']}, '{$_FILES['upload']['type']}', '$d', NOW())";
$result = @mysql_query ($query);
if ($result) {
// Create the file name.
$extension = explode ('.', $_FILES['upload']['name']);
$uid = mysql_insert_id(); // Upload ID
$filename = $_FILES['upload']['name'];
// Move the file over.
if (move_uploaded_file($_FILES['upload']['tmp_name'], "./uploads/$filename")) {
echo '<p>The file has been uploaded!</p>';
} else {
echo '<p><font color="red">The file could not be moved.</font></p>';
// Remove the record from the database.
$query = "DELETE FROM uploads WHERE upload_id = $uid";
$result = @mysql_query ($query);
}
} else { // If the query did not run OK.
echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience.</font></p>';
}
mysql_close(); // Close the database connection.
} // End of the main Submit conditional.