B4J Question A question about php & folder creation

Mashiane

Expert
Licensed User
Longtime User
Hi there

My app is deployed on

1680121278124.png


and this is where my php file is.

I need to create a folder in the drive on this path using php from my app.

1680121383466.png


What should be the path to pass to my folder creation. For example, if I pass "./assets/Anele", the folder will be created inside the riuso folder.

Thanks in advance for your help.
 

teddybear

Well-Known Member
Licensed User
Hi there

My app is deployed on

View attachment 140733

and this is where my php file is.

I need to create a folder in the drive on this path using php from my app.

View attachment 140735

What should be the path to pass to my folder creation. For example, if I pass "./assets/Anele", the folder will be created inside the riuso folder.

Thanks in advance for your help.
if you passed "/root/Files/assets/Anele", what happened?
 
Upvote 0

PaulMeuris

Active Member
Licensed User
If you have access to the destination folder you could use absolute and relative paths.
PHP:
echo realpath($_SERVER['DOCUMENT_ROOT']) . "<br>";
echo realpath($_SERVER['DOCUMENT_ROOT'] . '/../../../_temp/dbconnection.php');
And this is the result:
1680148128088.png

In this example the file dbconnection.php is located in the _temp folder of the E-drive.
The ../ goes up one level of the folder tree.
If you reach the root folder then you can go down the folder tree to the destination folder (in my case it was E:\_temp
Happy coding!
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
In simple terms this is my process flow and what I need to achieve.

1. Upload an image to assets folder in the app space. This is deployed in /var/www/html/riuso


1680189074544.png



This upload script is a file named upload.php inside the assets folder. This works well.

B4X:
<?php
header("Access-Control-Allow-Origin: *");
set_time_limit(0);
if (isset($_FILES['upload'])) {
    // Example:
    if(move_uploaded_file($_FILES['upload']['tmp_name'], "../assets/" . $_FILES['upload']['name'])){
        echo '{ "status": "success" }';
    } else {
        echo '{ "status": "error" }';
    }
    exit;
} else {
    echo '{ "status": "error" }';
}
?>

2. Now in the drive of the server in the folder named /root/Files, this is outside the app space. I need to create a sub folder and then move the image file there, renaming the image file to be img.png. The code to create folders is in the ruiso.php file above. This is the part I dont know how to achieve.

1680187806143.png


In the php file above, I have this code to create a folder

B4X:
function DirectoryMake($dirpath) {
    $target_exists = is_dir($dirpath);
    if ($target_exists) {
    die("yes");
    }
    mkdir($dirpath, 0700, true);
    //return directory existence
    $res = DirectoryExists($dirpath);
    die($res);
    }

B4X:
function DirectoryExists($path) {
    $target_exists = is_dir($path);
    if (!$target_exists) {
    //source does not exist
    die("no");
    }
    die("yes");
    }

I have done a call like

DirectoryMake("/root/Files/Image1"), I am assuming this will assume a process from where the php file resides, it does not work.

Please help.
 

Attachments

  • 1680187301817.png
    1680187301817.png
    37.3 KB · Views: 33
  • riuso.zip
    8.8 KB · Views: 51
Last edited:
Upvote 0

teddybear

Well-Known Member
Licensed User
B4X:
function DirectoryMake($dirpath) {
    $target_exists = is_dir($dirpath);
    if ($target_exists) {
    die("yes");
    }
    mkdir($dirpath, 0700, true);
    //return directory existence
    $res = DirectoryExists($dirpath);
    die($res);
    }

B4X:
function DirectoryExists($path) {
    $target_exists = is_dir($path);
    if (!$target_exists) {
    //source does not exist
    die("no");
    }
    die("yes");
    }
I have tested the 2 functions they work fine . what is php deployed on or you run php directly? perhaps some permissions are not allowed?
 
Last edited:
Upvote 0
Top