PHP symlink(): Permission denied in writeable folder - php

I want to create a symlink with PHP.
The symlink needs to go in the same place as the uploaded file, which should be fine right?
My real problem is that when using symlink(), I get a permission denied error on the same directory that PHP can write to from $_FILES.
I have done a test using text.txt as the test file, and link as the symlink:
symlink("repository/text.txt", "link");
The PHP script is run from content/folder/script.php. What am I doing wrong here? Do symlinks need file extensions (I doubt it) or something?

If this is the code you're using
symlink("repository/text.txt", "link");
And your script runs as /var/www/scripts/script.php the symlink will be created in the directory the script runs in.
Try using an absolute path.

Related

How can I delete the hidden .svn folder on my local working copy using php on a remote server? Unlink() permission denied

My SVN repository contains multiple projects. Each project contains the same structure that looks like trunk -> doc -> log.txt. This log file contains the changes made between revisions.
Currently I created a php script that executes when a server build is run. This php script checks out log.txt file on the server's local directory. C://..../..../doc/log.txt.
It writes into this file and then commits the changes. The problem occurs when someone runs the server build with another project. It tried to check out the log.txt file of that project into the same directory and I get an svn error. I no longer need the checked out files in the local directory once the changes have been committed. The only option I could think of is to delete the doc folder (only in my local directory not the repo) before checking out the new one so that the previous log.txt is no longer there.
I found a recursive algorithmthat deletes all files with unlink() and directories with rmdir. The algorithm works well. Unfortunately, the hidden .svn files do not unlink() with permission denied errors. Using fileperms() on all files and parent directories gives back 16895 which translates to 40777 in octal, so I believe they have full permissions. But I believe permissions for users do not have modify/write. Is there a proper way to delete the hidden .svn folder on my server's working copy? Alternatively if anyone can think of a better method to do this. So if AprojA/doc/log.txt is already in the local directory and I check out BprojB/doc/log.txt. Is there a way to checkout and overwrite the existing log.txt with the log file from another project? --force parameter doesn't seem to do much for me.
I also had a vague idea of maybe checking out the top level svn repo with -depth=empty. and then svn update just the log txt files of each project. So instead of checkingout -> committing changes -> deleting. I can just have all log.txt files available. But the problem is they all have the same folder and file name of (doc/log.txt). Still relatively new to all this so I'd appreciate feedback of any kind.
u can use wild cards to delete the log files with the extension. so all files of the same extension will be deleted under the selected folder.

Deleting video files using unlink() in PHP 7

I'm facing a problem with deleting video files from folder using php unlink() function , image are deleting but when trying deleting videos it says
unlink(file_path) : permission denied.
You (running your script as either through CLI or a webserver) need write access to the directory in which the files are located. so access to the file is not enough.
Your image directory would be different and writable for webserver or cli.
chmod("your/video/dir/path",0777);
try using above code before unlink the video in your script.
Edit: Looks like you are using Windows. Unfortunately, my answer is for Unix-like operating systems (so Linux, MacOS). You could try installing Bash extension for Win8+, but still I'm not sure if this would work. However, I'm keeping my answer in case of anyone with similar problem is looking here for an answer.
Changing permissions through PHP might work in some cases, but not always, because if you don't have permissions to delete the file you might also not have permissions to change them.
The best solution is to create a directory where you will keep files to which PHP will have full access. Let's call it dirname. Once you have created a directory, change its owner and group to the one corresponding to your web server user's name (if you're using Apache, it's "www-data"), for example: chown www-data:www-data dirname.
Once you've done that, change folder's permissions. My suggestion is 744, it will assure that user who owns it will have all permissions, and everyone else will be able only to read it. To do that, execute the following command: chmod -R 777 dirname.
Now you should be able to do anything you want with the files in given directory directly from PHP.

Running a PHP program in Ubuntu - fopen("filename", "w"): Failed to open stream: Permission denied

I am using Ubuntu as my OS, and I have XAMPP in it. In the htdocs folder, I have created a project folder in which I have index.php file. Now inside the index.php, a part of the code does this:
fopen("colors_new.csv", "w");
From the manual,
'w' Open for writing only; place the file pointer at the beginning of
the file and truncate the file to zero length. If the file does not
exist, attempt to create it.
But instead of the file being created, I am getting
Warning: fopen(colors_new.csv): failed to open stream: Permission denied...
And when I look into the project folder, the colors_new.csv file is NOT created.
When I run this program in another computer running Windows OS, it runs successfully. The file is successfully created.
How do I fix this problem in Ubuntu?
WHAT I TRIED:
I tried to run sudo chmod 777 colors_new.csv but I get chmod: can not access colors_new.csv. No such file or directory.. I tried this with just the file name (as shown) while the terminal WAS in the project directory; as well as by giving the full absolute path of the file. Got the same error both times.
NOTE: I have seen other questions, and as indicated above, I tried to give permissions to the file. The other questions did not solve the problem, the accepted answer below did solve it.
This means that you don't have the rights to write a file in the directory where your index.php is. A couple of things you can do to make it work
give filewriting permissions to the directory.
Go with 755 on that and 644 on files, if settings are correct this is sufficient. Never go full 777, which is security risk.
create a subdirectory inside your directory and give that sufficient rights (755) to do what you want.
F.i. Fopen('./files/colors_new.csv)
Provide the write permission on the folder in which you want to write the file. Your problem will be solved.

Linux file uploads, fwrites, does not work

I have installed fedora15 on my system, installed apache and php5.3 and mysql. I have not changed any default setting in any configuration files
Created one ftpuser and uploaded files to Documentroot which is '/var/www/html/' and when I run php files all the files runs successfully.
But when it comes to file-uploads, fopen, fwrite, imagemagick convert etc all the programs which involve creation of files programatically does not work. I have given 777 permissions to the folder where I run the php scripts but still it gives me the access denied error.
Below is a sample code which creates permission denied error.
$handle = fopen("test.txt","w+");
if(!$handle)die("Could not open file for writing");
fwrite($handle,"Testing Uploads Successfull.");
fclose($handle);
The above file is located in '/var/www/html/test/' directory of my newly installed fedora15 system.
Though if I run this file as a root user using php command line it executes correctly creating a new file and the text inside it. If I run the same file in command line with another user it produces permission denied error, though the directory has got 777 permission over it.
Please any one help me with this wierd behaiour,
Thanks in advance.
SELinux is preventing the web server from creating and writing to files. See the httpd_selinux(8) man page for more details.

How to run a PHP file on my computer that is outside the htdocs directory?

I just installed Apache and PHP on my computer to be able to run PHP files locally.
I can run a PHP file if it is located in Apache2.2\htdocs directory.
Is that possible to run PHP files outside this directory ?
I'm looking to a simple solution, because all I need is to write a small PHP code and try it locally.
I would like to be able to run the PHP file by right clicking it -> Open with Firefox
You can run a PHP script from anywhere using the command line:
php yourscript.php
There is some ways to do it, but the simplest way is using 'include' or 'required' php command:
<?
include ('/somewhere/outside/of/your/htdocs/file.php');
?>
Please note you may need to turn off 'open_basedir' php directive.
Newer versions of php come with php-cli: a command line interface. So to run php code, you just need to type:
php some_code.php
Or you can change paths in apache config to another dir
you cannot open it with firefox.
Because firefox has nothing to do win PHP.
So, you need to request this URL from a web-server
Create a windows shortcut with http://127.0.0.1/file.php
or whatever way you prefer to click web links
Yes. You don't say if you want to run it from the web server, or from the command line, but you can use include past the web root.
You can either run it from the command line,
or you can create a "wrapper" script within your htdocs that "includes" the file from outside of the htdocs directory
I had always wondered about it the same manner, say you have a folder in a drive either than the apache/htdocs folder in drive C, and you would like to develop directly in this folder. But think about it this way, would you put your folders else where and expect it to be running on the web? Now Way. Why would you do that?
Goto conf folder and open httpd file change
DocumentRoot "C:/your/path" and
Directory "C:/your/path"
This should work
if it says forbidden access go to this link
Error message "Forbidden You don't have permission to access / on this server"

Categories