I have a custom page template. This template have a custom form, the user can upload a file in that form and input his/her personal infos. The file will save in the file system.
I want to create a download link for each file and display it to a table row with the other values from MYSQL. I already have a PHP snippet that will display the personal infos from MYSQL to a table rows.
Now I want to display the download link to the file that the user uploaded with their personal infos.
And how to rename the filename of the upload file? E.g. lastname_firstname.docx
Can you help me with this? My snippet for creating a link is not working.
Here's the function:
To upload a file:
function upload_user_file($file = array()){
require_once(ABSPATH . 'wp-admin/includes/admin.php');
$file_return = wp_handle_upload($file, array('test_form' => false));
if(isset($file_return['error']) || isset($file_return['upload_error_handler'])){
return false;
} else {
$filename = $file_return['file'];
$attachment = array(
'post_mime_type' => $file_return['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $file_return['url']
);
$attachment_id = wp_insert_attachment($attachment, $file_return['url']);
require_once(ABSPATH . 'wp-admin/includes/file.php');
$attachment_data = wp_generate_attachment_metadata($attachment_id, $filename);
wp_update_attachment_metadata($attachment_id, $attachment_data);
if(0 < intval($attachment_id)){
return $attachment_id;
}
}
return false;
}
To create a link (Not working):
$url = $_SERVER['REQUEST_URI']; //returns the current URL
$parts = explode('/',$url);
$dir = $_SERVER['SERVER_NAME'];
for ($i = 0; $i < count($parts) - 1; $i++){
$dir .= $parts[$i] . "/";
}
echo '<td id="message"><a href="' . $dir . $file . '">Download</a></td>';
Form:
<?php
$locations="";
$timestamp = date('Y-m-d G:i:s');
$locations = $wpdb->get_results('SELECT location FROM tablename ORDER BY location ASC', OBJECT);
?>
<div class="resume-form">
<div class="rForm"></div>
<div class="rForm">
<h1 class="title" style="color: #d31716;">Apply Now!</h1>
<form action="" method="post" enctype="multipart/form-data" name="ResumedataBank" id="ResumedataBank">
<input type="hidden" name="form_title" value="Resume"/>
<div class="input-container">
<input type="text" name="lastName" value="" size="40" id="lastName" required/>
<label for="lastName">Last Name *</label>
<div class="bar"></div>
</div>
<div class="input-container">
<input type="text" name="firstName" value="" size="40" id="firstName" required/>
<label for="firstName">First Name *</label>
<div class="bar"></div>
</div>
<div class="input-container">
<input type="text" name="middleName" value="" size="40" id="middleName" required/>
<label for="middleName">Middle Name *</label>
<div class="bar"></div>
</div>
<div class="input-container">
<input type="email" name="email" id="email" value="" size="40" required/>
<label for="Email">E-mail Address *</label>
<div class="bar"></div>
</div>
<div class="input-container">
<input type="text" name="mobile" value="" minlength="11" maxlength="" onkeydown="sanitizeNumber()" id="mobile" required/>
<label for="mobile">Contact Number *</label>
<div class="bar"></div>
</div>
<div class="input-container">
<label for="resumeFile" style="margin: 0 0 3em; position: relative; top: 0; left: 0; color: #757575; font-size: 1.25em; font-weight: 300; line-height: 50px;">Upload</label>
<input type="file" name="resumeFile" value="" size="40" id="resumeFile" accept=".doc,.docx,.docx"/>
<div class="bar"></div>
</div>
<div class="input-container">
<label for="Message" style="margin: 0 0 3em; position: relative; top: 0; left: 0; color: #757575; font-size: 1.25em; font-weight: 300; line-height: 50px;">Message</label>
<textarea name="message" size="40" id="message" rows="4"></textarea>
<div class="bar"></div>
</div>
<div class="resume-form-button">
<input type="submit" name="submit" id="submit" class="buttonv2" value="Submit">
</div>
</form>
</div>
</div>
<?php
ob_start();
$firstName = "";
$middleName = "";
$lastName = "";
$email = "";
$mobile = "";
$resume = "";
$location ="";
$position = "";
$message = "";
if(isset($_POST['submit'])){
$firstName = isset($_POST['firstName']) ? $_POST['firstName'] : '';
$middleName = isset($_POST['middleName']) ? $_POST['middleName'] : '';
$lastName = isset($_POST['lastName']) ? $_POST['lastName'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$mobile= isset($_POST['mobile']) ? $_POST['mobile'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
if( ! empty($_FILES)){
$file=$_FILES['resumeFile'];
$attachment_id = upload_user_file( $file );
}
$sql=$wpdb->query("INSERT INTO tablename(submit_time,last_name,first_name,middle_name,mobile_number,email,message) VALUES (now(),'$lastName','$firstName','$middleName','$mobile','$email','$message')");
exit();
}