PHP - unlink('file_name') not working [duplicate] - php

This question already has answers here:
unlink PHP works when file is in root, not if file is in folder
(2 answers)
Closed 5 years ago.
I have issues while trying to delete files in PHP, using unlink('filename').
I have tried with a complexe file and it didn't worked. I've been using relative paths as adviced on other post about this.
So i've made the simpliest script possible :
<?php
unlink("acs.gif");
?>
The script is located in the same folder as my asc.gif is, tho it still doesn't work.
I've got no fatal errors, and a warning when enabling error_reporting() and init_set(). But the file is still there.
I've tried to set the permissions to both my folder, my image and my script to 0777 but it didn't help.
I'm getting quite confused about what is happening.
Do you guys have any ideas ?

I recommend you to use absulte filepaths. In case you want to delete a file which is in the same directory of the called script, poleteaw answer should work (besides the missing / in the path):
unlink(__DIR__ . '/' . $filename);
Nevertheless take look of php's directory function realpath() and the predefined constants.
So what if you want to delete a file which is not inside your directory:
You can use the realpath() method to generate an absolute path out of a relative path. So realpath('/one/two/three/../..') results in '/one' - or for your case you can do something like realpath(__DIR__ . '/../../') to get into the root directory of you project.
The recommended way is to use a variable which holds the absolute path to the directory where you want to store and administrate your files like $filesDir = '/path/to/my/files'. With this approach you have two wins: your users files do not reside in your php project files and you have a way much better overview of which files are uploaded/administrated.

You shouldn't use relative path to file. If it lies in the same folder as a PHP script, use unlink(__DIR__ . '/' . $filename);. In other cases set a full path to unlink.

Related

What it mean when we perfix with dots for require function to indicate filename in php [duplicate]

This question already has answers here:
What is double dot(..) and single dot(.) in Linux?
(4 answers)
Closed 5 years ago.
I always use require('dbc.php'); to include file but what is the difference when I prefix 2 dots ../ as below, is there is any extra security.
require('../dbc.php');
require('../lib/bootstrap.php');
require_once '../../../conf/config.php';
If you do
../../
You've gone back two directory
../
You've gone back one directory
This basically going out the current directory the file u are working on is in. It depends on the location of the db file relative to the file that needs it. It has nothing to do with security.
The . gives you the ability to set the path of the included files relatively to the path of the original file that run (the file that included them). The ./ indicates the current directory. So if including a file like such:
require('./config.php')
You are telling PHP to look in the current directory for "config.php". Which is the same as
require('config.php')
The ../ indicates the directory above or "parent directory"
require('../dbc.php');
This is telling PHP to go one directory up and look for "dbc.php".
These commands can be chained like so:
require('../lib/bootstrap.php');
require_once '../../../conf/config.php';
The dots simply are used to traverse the directory structure. What is double dot(..) and single dot(.) in Linux?, though you should avoid using relative paths and use absolute paths. Absolute vs. relative paths.
Security:
In its self, it introduces no security benefits, except if you get it wrong your app won't work at all!
It does add some protection against code disclosure if PHP fails to parse. This applies ONLY if you store your main code outside of the webroot, though I have never encountered or seen this issue spontaneously happen, though it possibly could. Storing script files outside web root.

relative vs absolute path of a file for php includes [duplicate]

This question already has answers here:
Get relative path to a parent directory
(2 answers)
Closed 8 years ago.
So I have this sort of file set up. /cloud/ and /embed/ being two different subdomains.
www
-cloud
--config.php
--files
---formSubmit.php
-embed
--index.php
in /embed/index.php I have the following code:
include("/www/cloud/files/formSubmit.php");
in /cloud/files/formSubmit.php I have the following code:
include("../config.php");
If I am on cloud.website.com and I go to the formSubmit.php, everything works fine and the config file is included.
However, If I am on embed.website.com and I go to the index.php, I get an error saying that config.php was not found.
Does anyone know what do I need to do to include my formSubmit.php from either location and have my config.php included?
In this case, it seems your usage of relative paths is working and absolute paths are not. Whether that means the absolute path of /www/cloud/files/ is incorrect or not, I do not know. In my code, I tend to try to reference files relatively as much as possible like so:
// In embed/index.php
include_once dirname(dirname(__FILE__)) . '/cloud/files/formSubmit.php';
What that does is get the directory of the currently executing file and then it's parent directory, which would be www, and then goes back down the path from there to the file I need.
Subdomains should not make a difference when accessing files server side (as long as the files are hosted on the same server).

PHP special symlink usage [duplicate]

This question already has answers here:
How to get the relative directory no matter from where it's included in PHP?
(5 answers)
Closed 8 years ago.
I have the following test structure:
/www/index.php
<?php
require_once(dirname(__FILE__).'/linked/linked.php');
/www/linked/ which is a symlink to /symlinkedfolder/
/symlinkedfolder/linked.php
<?php
echo __FILE__;
The output for this script is:
/symlinkedfolder/linked.php
Is there any way/technique with PHP or Apache or Linux which would make symlink behave not symlink instead like a normal filesystem folder/file?
I need that my example give back the following output:
/www/linked/linked.php
(But in real it would be still a symlinked file which originally located in its original folder)
UPDATE #1
We are working with version control system and we would like to keep the checked out folder in a global folder and we would like to symlink each folders to its proper path in the actual platform(Joomla or WordPress etc...). It would allow us to only update and commit from one folder, but still refresh every platform with a single update. (This could work until we not use FILE or DIR or any related things what symlink can mix up.)
It’s a pain. As the official PHP documentation explains:
The full path and filename of the file. If used inside an include, the
name of the included file is returned. Since PHP 4.0.2, FILE
always contains an absolute path with symlinks resolved whereas in
older versions it contained relative path under some circumstances.
Which is a pain. This is why I have decided it’s best to set a base path explicitly as I explain here. So in your case you would set:
$BASE_PATH = '/www/';
And then your require_once would be like this:
require($BASE_PATH . '/linked/linked.php');
This question & answer is similar to yours and recommends using $_SERVER['SCRIPT_NAME'] but in my experience, that setting can change radically between server to server for odd reasons. Which is why I have defaulted to the $BASE_PATH method when I code. You set it once, forget it & no worries.

require_once not working as I expect it to [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
relative path in require_once doesn’t work
I have a project structure as such:
ProjectName/
src
test
TestClass.php
tests
TestTestClass.php
WhenI try and do require_once '/ProjectName/src/test/TestClass.php'; into tests/TestTestClass.php I get an error from PHP stating that: no such file or directory.
I have checked the spelling, I have checked everything. I cannot give it the full /home/userName/bla/bla path as I need to hand this off to some one else.
Any ideas?
This is expected behaviour. When you execute TestTestClass.php, your working directory is set to ProjectName/tests.
It would be better to use ../src/test/TestClass.php.
Your path is actually absolute, so you will be working straight from /, which is not what you're expecting. If your include_path is set to your server's directory root, then your code will work without the first /. If you don't wish to rely on include_path or arbitrary levelling (with ..), you can always set an environment variable in your first file that defines the full path to your application root, then use that for all includes.
For example, /ProjectName/index.php:
define('APPPATH', __DIR__);
.. and in /ProjectName/tests/TestTestClass.php:
require_once APPPATH . '/src/test/TestClass.php';
absolute filepaths passed to require/include/require_once/include_once all work from the filesystem root, not to the webserver root
Use the special DIR to get the current path of the script and like so it will be easier for you to find script paths:
require_once(__DIR__ . '../folder/whatever.php');
You'd benefit from creating a path constant for your project. This is quiet easy to do with the __FILE__ constant and dirname().
define('ROOT_PATH', dirname(__FILE__));
The above could be put in a configuration file so it is easier to include files throughout your project.
require_once ROOT_PATH . '/src/test/TestClass.php';
I'd also suggest looking into an autoloader.

How do I include a PHP script from a base path other than the current script?

I have a PHP application I'm trying to include, but it's causing problems because I'm trying to include its files from a subdirectory. For instance, here's the directory structure (more or less) I'm trying to work with:
/site
/scripts
/local
some_script.php
lib_file_a.php
lib_file_b.php
lib_load.php
lib_load.php has an include command for files lib_file_a.php and lib_file_b.php (relative path).
What I want to accomplish is to include lib_load.php from some_script.php, but there's a problem with the way I'm including lib_load.php. At first I tried require('../../../lib_load.php');.
As you would expect I saw the error, "Failed opening required 'C:\xampp\htdocs\site\scripts\local\lib_file_a.php'". As you can see, PHP is trying to include a file that does not exist. I found a Stack Overflow post that told me about using an absolute path (link), but this did not help. I next tried this code:
require(realpath(dirname(__FILE__) . '../../../' . 'lib_load.php'));
However, this time, the same error comes back. What am I doing wrong?
EDIT: I realized that the issue was actually in the library loader, which turned out to be including files based on an absolute path, not a relative path. (I should have read more carefully.) I resolved the issue by changing the path used by the library. This question should be closed if a mod sees this request.
To include lib load
require $_SERVER['DOCUMENT_ROOT'] . '/scripts/lib_load.php';
inside of lib load
require dirname(__FILE__) . '/lib_file_a.php';
if I got your problem right
Try this:
require dirname(__FILE__) . '/../../../lib_load.php');
Or if you're using PHP 5.3 or higher use this instead (it looks nicer):
require __DIR__ . '/../../../lib_load.php');

Categories