Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file outside web root directory

i want to upload images outside web root directory, but i don't know how to do it.
For example i want to put images in var/media3/, i set the media directory to permissions 777, now i need to modify the code to put images because that can not work.
I tryed even using $_SERVER['DOCUMENT_ROOT'] but it doesn't work (and honestly I have never tried this kind of thing).

This is the upload.php script:

<?php

// A list of permitted file extensions
$allowed = array('png', 'jpg', 'gif');

if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){

    $extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);

    if(!in_array(strtolower($extension), $allowed)){
        echo '{"status":"error"}';
        exit;
    }

    if(move_uploaded_file($_FILES['upl']['tmp_name'], 'var/media3/'.$_FILES['upl']['name'])){ // <--The problem is in this string, it won't work
        echo '{"status":"success"}';
        exit;
    }
}

echo '{"status":"error"}';
exit;



Thanks XD

like image 276
leofabri Avatar asked May 30 '26 00:05

leofabri


1 Answers

Add a leading slash /var/media

like image 150
Parris Varney Avatar answered Jun 01 '26 17:06

Parris Varney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!