我有一个自定义页面模板。此模板有一个自定义表单,用户可以在该表单中上载文件并输入他/她的个人信息。该文件将保存在文件系统中。
我想为每个文件创建一个下载链接,并将其和MYSQL中的其他值一起显示到表行中。我已经有了一个PHP代码片段,可以将MYSQL中的个人信息显示到表行中。
现在,我想显示用户随个人信息上传的文件的下载链接。
如何重命名上传文件的文件名?E、 g.lastname\\u firstname。docx公司
你能帮我做这个吗?我用于创建链接的代码段无效。
功能如下:
要上载文件,请执行以下操作:
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;
}
要创建链接(不工作):
$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>\';
形式:
<?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();
}