最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugins - How can sanitize $_FILES['haq_slider'] field

matteradmin4PV0评论

I have a function

function haqSliderHandleUpload() {
    global $haq_settings, $haqSliderImage;

    //  upload the image
    $sliderfile = $_FILES['haq_slider'];
    $upload = wp_handle_upload($sliderfile, 0);
    extract($upload);
    $uploadDirPath = str_replace(basename($file), '', $url);
    list($imageWidth, $imageHeight) = getimagesize($file);     }

I want to SANITIZE this field $sliderfile = $_FILES['haq_slider']; How can i do it

I have a function

function haqSliderHandleUpload() {
    global $haq_settings, $haqSliderImage;

    //  upload the image
    $sliderfile = $_FILES['haq_slider'];
    $upload = wp_handle_upload($sliderfile, 0);
    extract($upload);
    $uploadDirPath = str_replace(basename($file), '', $url);
    list($imageWidth, $imageHeight) = getimagesize($file);     }

I want to SANITIZE this field $sliderfile = $_FILES['haq_slider']; How can i do it

Share Improve this question asked Apr 6, 2019 at 6:09 Husain AhmedHusain Ahmed 731 silver badge13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You don't say where this code is running - for users or just for admins. Here are a few tips, taken heavily from this article on Wordfence.

The first check you can run is current_user_can to see if the current user is allowed to upload files using:

if(current_user_can('upload_files')) { ....

Next you can use wp_check_filetype to see if it's a valid extension.

$fileInfo = wp_check_filetype(basename($_FILES['haq_slider']['name']));
if (!empty($fileInfo['ext'])) {
   // This file is valid
} else {
   // Invalid file
}

The final test that Wordfence suggest is a call to PHPs getimagesize which will return FALSE if it fails to read a valid image file.

if (!@getimagesize($_FILES['haq_slider']['tmp_name']))
   wp_die(__('An invalid image was supplied.'));
Post a comment

comment list (0)

  1. No comments so far