Must i use long hand links or am i doing something wrong?

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
tommytx123
php-forum Active User
php-forum Active User
Posts: 35
Joined: Sat Sep 19, 2015 4:57 pm

Wed Sep 22, 2021 5:03 pm

Here is what i have:

Code: Select all

$data = 'any data i want.';
$file = 'home/myusername/public_html/wp-content/plugins/IDXSEO/include/all-leads/bruceZERO.txt';
file_put_contents($file, $data);
Why cant i use this (shorthand):

Code: Select all

$data = 'any data i want.';
$file = '/all-leads/bruceZERO.txt';
file_put_contents($file, $data);
This code will be inside a file inside the /include/ folder.. and the file bruceZero.txt will be in the /all-leads/ folder and works fine with the long hand address....

Next it gets even more confusing...
I have a file named master.php in the include directory and inside a file in the include directory i have an include statement that says
include('master.php') and the very shorthand works fine..
Is there something i am not understanding here?
tommytx123
php-forum Active User
php-forum Active User
Posts: 35
Joined: Sat Sep 19, 2015 4:57 pm

Wed Sep 22, 2021 5:19 pm

I don't see a way to edit so will just add..
Also its a problem that i have to leave my cpanel user name laying around all over the place that does not seem right...
simonbrahan
php-forum Super User
php-forum Super User
Posts: 153
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Thu Sep 23, 2021 12:08 am

Any path starting with / will be treated as an absolute path - a path from the root of the file system. You can use shorthand paths by omitting the starting /, PHP will infer the rest of the path from the current working directory. The current working directory is usually the directory in which your script is stored; you can check this using getcwd.

The usual good practice is to define your storage directory somewhere central in a constant then use that. For example:

Code: Select all

<?php 
define('STORAGE_DIR', 'home/myusername/public_html/wp-content/plugins/IDXSEO/include/');

// Now any other part of your code can do:

$file = STORAGE_DIR . 'all-leads/bruceZERO.txt';
As an aside; I'm not read up on wordpress, but you almost certainly don't want to store data in your plugin folder. When someone updates your plugin, it's likely to delete the data.
tommytx123
php-forum Active User
php-forum Active User
Posts: 35
Joined: Sat Sep 19, 2015 4:57 pm

Thu Sep 23, 2021 7:07 am

Thanks for that SImon... but removing the forward slash on /all-leads/bruceZERO.txt did not help.. I tried it both ways many times.. and the only thing that worked was the full url... while a simple include('master.php") worked just fine a supershort url..
And worse it seems ridiculous that i must use my cpanel user name all over the place in a php file..
I did go to the wp-config.php and add a define for my cpanel username... so that I am not displaying my cpanel user all over the place but its sitting there in the wp-config for anyone to see that can get in...
Are you sure $file='all-leads/bruceZERO.txt' will work as it did not work for me after several attempts with and without the leading forward slash /.
Since the shorty for include('master.php') worked so well i tried the following to no avail..
$data = 'What ever i want.";
$file = "bruceZERO.txt';
file_put_contents($file, $data);
Meaning that i am not using the /all-leads/ directory, but using the actual working directory
and sure enough nothing works but the full URL for me.... with or without the leading / made no difference.
simonbrahan
php-forum Super User
php-forum Super User
Posts: 153
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Thu Sep 23, 2021 9:57 am

If you remove the starting forward slash, PHP looks for your path in the current working directory. The current working directory won't necessarily be the directory containing your script. If the current directory isn't what you expect, there's no way PHP can find your file. I'd recommend you use absolute paths for this reason.

Regarding having your username in your config; that's a problem for your hosting provider. They might be able to help if you want an anonymous directory structure.
tommytx123
php-forum Active User
php-forum Active User
Posts: 35
Joined: Sat Sep 19, 2015 4:57 pm

Fri Sep 24, 2021 2:03 pm

Thanks Simon...that helps me understand a lot more...
Post Reply