0

I have an admin form for a plugin I am making. However, there are a couple of fields I need to add where the admin need to upload a document or image. When these files are uploaded, I do not want them to be added to the media library of WordPress as these files won't be used on any web pages. All I need is just the URL of where the file has been uploaded to on the server to use in the database.

What would be the best way to achieve this?

Thanks for your time!

2 Answers 2

1

HTML

Okay, you'll of course want to set up an HTML file. I would use this code or something like it:

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

Of course, you don't have to use upload.php as your filename, that's just what w3schools used.

PHP

<?php
$target_dir = "your/file/upload/path";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>

Change $target_dir to whatever directory you want the files to go into and there you go. There are of course many jQuery plugins to do this in a fancy or pretty way but this is the basics using jQuery.

Other information


Source of information: W3Schools

A great Ajax file uploader script can be found here: http://demo.tutorialzine.com/2013/05/mini-ajax-file-upload-form/

A great jQuery file upload plugin can be found here: http://blueimp.github.io/jQuery-File-Upload/index.html

According to their website it supports:

File Upload widget with multiple file selection, drag&drop support, progress bars, validation and preview images, audio and video for jQuery. Supports cross-domain, chunked and resumable file uploads and client-side image resizing. Works with any server-side platform (PHP, Python, Ruby on Rails, Java, Node.js, Go etc.) that supports standard HTML form file uploads.

0

I think it depends on how you're handling the file input field in your form. I've used wp_upload_bits() in a plugin with a custom post type that has file attachments. The file is attached and uploaded using a metabox for the CPT and wp_upload_bits is called in the 'save_file_function' function called with add_action( 'save_post', 'save_file_function' ); This would all go in your main plugin .php file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.