我在下载链接中遇到了一个问题,URL变得重复。像这样http://localhost/test/files/http://localhost/test/files/2016/05/testonly.docx
与live网站相同:http://www.homecredit.ph/wp-content/uploads/home1/homecre1/public_html/files/News-26.jpg, URL出错
如何修复此问题?我没有在.htaccess
.htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test/
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test/index.php [L]
</IfModule>
# END WordPress
以下是下载链接的代码:
功能。php
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;
}
自定义页面模板
echo \'<td id="resumeFile\'.$optionId.\'"><a href=\' . wp_upload_dir($record_s->attachment_resume_id) . \'>Download Resume</a></td>\';
SO网友:Karthikeyani Srijish
我刚检查了你的URLhttp://www.homecredit.ph/wp-content/uploads/home1/homecre1/public_html/files/News-26.jpg 编码并注意到,您正在将图像保存在http://www.homecredit.ph/files/News-26.jpg 路径,但正在尝试从WordPress的上载目录访问。/home1/homecre1/public_html/files/News-26.jpg - 这是存储在数据库中的附件ID路径。/home1/homecre1/public_html/ - 这是安装WordPress的根目录。因此,最好只在db中保存文件名,或者从数据库值中删除根路径,并附加站点url。EG:
$filePath = str_replace(\'/home1/homecre1/public_html\', \'\', $record_s->attachment_resume_id);
echo \'<td id="resumeFile\'.$optionId.\'"><a href=\' . $filePath . \'>Download Resume</a></td>\';
这只是一个例子。您不应该在str\\u replace函数中硬编码路径。您需要使用PHP函数来获取根路径并将其放置在函数中。首先尝试上面的代码。如果工作正常,则继续执行上述步骤。