How to Use FTP to Upload Files to WordPress for Beginners

To complete your WordPress site setup, you need to upload files to the correct folders on the servers. There are multiple file types for you to upload, including media, plugins, themes, and tons more.

Although WordPress allows adding files via the admin dashboard, many site owners prefer doing this task using the FTP client. In fact, the process is tricky for beginners.

That’s why in this article, we’ll guide you through how to upload files using FTP. To make it more specific, we’ll also focus on the way to programmatically add media to the server.

But first, let’s define FTP together.

What is FTP?

FTP or File Transfer Protocol, as its name suggests, permits you to transfer files from your computer to your website hosting account. To achieve this, you need an FTP client running on your computer to transfer files from and to your server. Common FTP clients include Filezilla and WS_FTP.

Connecting to a remote server via FTP requires a server address as well as an FTP username and password. The format of the FTP server address is ftp://domain-name.com. The domain-name.com is your site’s domain. It can be sometimes replaced with your site IP address.

How to Use FTP to Upload Files to a Website

Before start uploading files to the server, make sure that your hosting provider gives you FTP access to your website. Once you have the username and password ready, log into your FTP client and fill in the credential information fields.

Then follow the steps below:

  1. Open Files then choose Site Manager from the menu. It will display a popup.
  2. Click on the New Site button under the Select Entry section and enter your site name.
    pda-use-ftp-to-upload-files
  3. Fill in the Host field with your FTP address.
  4. Select Normal for the Logon Type.
  5. Enter your username and passwords and hit Connect.

Now you will see two panels on your screen, one for file structure on your computer and the other for files on your web hosting. All you need to do is drag files from the left panel and drop them to the right one.

Upload Files to WordPress Media Library Programmatically

WordPress, by default, provides you with a file uploader called wp_upload_bits() letting you upload files programmatically. It’s placed in the wp-includes/functions.php file.

First, you need to create a plugin file named upload.php and enter the following code in that file.

<?php
/* 
Plugin Name: Upload Files Programatically 
Plugin URI: https://wordpress.org/plugins/ 
Description: Just another file uploader plugin. 
Version: 1.0.0 
Author: David Angulo 
Author URI: https://www.your-domain.com/wp/ 
*/

After that, you can add a function to that file to display an uploader in the website frontend. Simply add this code snippet to the file above:

function myFileUploader() {     
  echo '
    <form action="" enctype="multipart/form-data" method="post">
      <input id="fileToUpload" name="fileToUpload" type="file"> 
      <input name="submit" type="submit" value="Upload File">
    </form>
  '; 
​​}

The next step is to render your form by inserting this code.

function myFileUploaderRenderer() {     
  ob_start();    
  myFileUploader();     
  return ob_get_clean(); 
}

To have this function works, you also need to add a hook.

add_shortcode('custom_file_uploader', 'myFileUploaderRenderer');

Lastly, enter the PHP code as follow:

if (isset($_POST['submit'])) {     
  wp_upload_bits($_FILES['fileToUpload']['name'], null, file_get_contents($_FILES['fileToUpload']['tmp_name'])); 
}

That’s it!

Protect Your Files and Folders on the Server

The guide above allows you to create a form for users to upload their files. There may be times when you’re required to protect these files and folders. They can be docs from guest writers or PDF scans of your clients’ private information.

When they upload these files to your site, chances are search engines will index and serve them on search results.

Fortunately, you have the PDA Gold plugin and its Access Restriction extension at hand. They give you perfect solutions to secure your WordPress media files and folders right in the admin dashboard.

Once downloading, installing, and activating the plugins, you can take these 3 steps to start protecting your folders.

  1. Head over Settings under Prevent Direct Access Gold section in your admin dashboard
  2. Open the Folder Protection tab on the Settings page
  3. Pick the folders you want to protect from the public access

It’s possible for you to choose a file type to make it private, for example, pdf or png.

The File Access Permission option allows certain user roles to view your files. The default value is Admin users and you can change it into logged-in users or a custom role. You can choose specific users to open your private files as well.

Save all changes and you’re done.

Final Thoughts

We have walked you through the process of how to use FTP to upload files to a website. Remember to connect with your FTP client before adding files to the server.

You’re able to create a form and allow users to add files to the server through that form programmatically. Still, you must have some coding knowledge to get started on this task.

We recommend protecting your WordPress files and folders too to restrict public access. The PDA Gold plugin and Access Restriction extension make it simple for you to secure private files of users right in your admin dashboard.

Do you have any difficulties in uploading files to WordPress using the FTP client? Share with us your experience in the comment section below.