admin管理员组

文章数量:1122826

I have an issue in the download link, the URL getting duplicate. Like this http://localhost/test/files/http://localhost/test/files/2016/05/testonly.docx

same as the live website: .jpg, the URL messed up

How to fix this? I didn't add anything in the .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

Here's the code for download link:

functions.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;
}

Custom page template

echo '<td id="resumeFile'.$optionId.'"><a href=' . wp_upload_dir($record_s->attachment_resume_id) . '>Download Resume</a></td>';

I have an issue in the download link, the URL getting duplicate. Like this http://localhost/test/files/http://localhost/test/files/2016/05/testonly.docx

same as the live website: http://www.homecredit.ph/wp-content/uploads/home1/homecre1/public_html/files/News-26.jpg, the URL messed up

How to fix this? I didn't add anything in the .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

Here's the code for download link:

functions.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;
}

Custom page template

echo '<td id="resumeFile'.$optionId.'"><a href=' . wp_upload_dir($record_s->attachment_resume_id) . '>Download Resume</a></td>';
Share Improve this question edited May 20, 2016 at 5:41 User014019 asked May 20, 2016 at 1:35 User014019User014019 1533 silver badges13 bronze badges 1
  • The issue might not be related to .htacess file. It might be in how you generated the link. Could you provide the code to the download link so that we might know what's going on – bagpipper Commented May 20, 2016 at 3:44
Add a comment  | 

1 Answer 1

Reset to default 0

I just examined your URL http://www.homecredit.ph/wp-content/uploads/home1/homecre1/public_html/files/News-26.jpg and code and noticed that, you are saving your image in http://www.homecredit.ph/files/News-26.jpg path but trying to access from upload directory of WordPress. /home1/homecre1/public_html/files/News-26.jpg - this is the path stored in database for your attachment ID. /home1/homecre1/public_html/ - This is your root directory where WordPress installed. So better save only the file name in db or remove the root path from the database value and attach with site 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>';

It's just an example. You should not hard code the path in str_replace function. You need to use PHP function to get the root path and place it in the function. First try with the above code. If it works then proceed with the above step.

本文标签: url rewritingHow to remove Base URL Duplication