PhP fopen creates file with Path name - php

Trying to update a file file.xml, which is with folders dirA/dirB/dirC/file.xml where dirA is the current working dir. The file file.xml exists and has write permissions.
Using the following code works in local but on server it created a file by name "dirA\dirB\dirC\file.xml" outside dirA and saves into it
$file = fopen("dirA\dirB\dirC\file.xml", "w+")
fputs($file, $xmlFile);
fclose($file);
Any idea why?

Maybe because you are running another environment on your server?
Windows and Linux are a little bit itchy on their folders.
You may also check if you have to use backslashes or not!
Probably you also have to quote them:
$file = fopen("dirA\/dirB\/dirC\/file.xml", "w+");

Related

I'm trying to creat and store a file locally in my pc using any web programing language

I'm trying to save a txt file in my pc locally with php, but not in the download folder. I need to add in different folder.
Here is my code
$txt = "$dirname/Contact.txt";
$fh = fopen($txt, 'w+');
exit();
fwrite($fh,$txt);
close($fh);
You can use $_SERVER variable to specify your path
$myTxt = $_SERVER['DOCUMENT_ROOT'] . "you/path/directory"
But remember that the path first is in your project, you may go out with "../"
And your exit(); command, this kills your code. Put this at the end of the file. It will never reaches close($fh);
Maybe only fixing this exit(); will solve your problem.

PHP is_writable() to check if file can be written?

I've read that I can use is_writable() to check if a folder or a file is writable.
How do I check if a file can be written to a folder?
Do I check the folder and if the folder is writable? Then the file is allowed to be put into that folder?
What if the file has been written to the folder, how do I check if that is writable again (edited)? Do I need to? If so, do I check the file instead of the folder?
Is it a safe method (correct) to do it?
The PHP function is_writable is exactly for this purpose. If you want to check if file is still writable after you've written the file, you can use the same function.
Read the documentation as linked in the question you pointed to. is_writable() is working on files and directories.
But mind: If you have code like this:
if (is_writeable("foo.txt")) {
$fp = fopen("foo.txt", "w");
/* ...*/
}
This might still fail. For instance there might be a lock or a race condition (permissions change between the two commands). Better simply try to open and then handle the error.
$fp = #fopen("foo.txt", "w");
if (!$fp) {
report_error_in_some_way();
}

unlink PHP works when file is in root, not if file is in folder

So this one is pretty straight forward I want to delete a file on the server using PHP, I have:
$myfile = 'theone.png';
unlink($myfile);
This code deletes the file, howevere if the path to file is /images/theone.png, it doesn't work, I have tried images\theone.png with no luck.
If I try and connect with FTP I get the error message to say that cURL does not support the unlink function... Any help would be great.
Thanks Guys!
What about:
$root = realpath($_SERVER['DOCUMENT_ROOT']);
$myfile = '$root/images/theone.png';
unlink($myfile);
Although to my knowledge, your attempted method should work, unless either I'm missing something, or you haven't included some code here that might be interfering with the unlink.
__DIR__ - this magic constant contains current directory, in case that the file is in the same directory as your PHP script you can use:
unlink(__DIR__ . "/$myfile");
If the file is for example in one directory above your PHP script you can use:
unlink(__DIR__ . "/../$myfile");
If the directory has correct access rights it should work.

permission denied - php unlink

I have two files:
b.php and test.txt
<?php
$b = "test.txt";
unlink($b);
?>
and the error is: Warning: unlink(test.txt) [function.unlink]: Permission denied
why? b.php and test.txt is 777 and the same group/login
if I set 777 on the parent directory I can execute unlink but i have to set 777 and back to 755?
You (as in the process that runs b.php, either you through CLI or a webserver) need write access to the directory in which the files are located. You are updating the directory content, so access to the file is not enough.
Note that if you use the PHP chmod() function to set the mode of a file or folder to 777 you should use 0777 to make sure the number is correctly interpreted as an octal number.
You'll first require to close the file using fclose($handle); it's not deleting because the file is in use. So first close the file and then try.
in addition to all the answers that other friends have , if somebody who is looking this post is looking for a way to delete a "Folder" not a "file" , should take care that Folders must delete by php rmdir() function and if u want to delete a "Folder" by unlink() , u will encounter with a wrong Warning message that says "permission denied"
however u can make folders & files by mkdir() but the way u delete folders (rmdir()) is different from the way you delete files(unlink())
eventually as a fact:
in many programming languages, any permission related error may not
directly means an actual permission issue
for example, if you want to readSync a file that doesn't exist with node fs module you will encounter a wrong EPERM error
// Path relative to where the php file is or absolute server path
chdir($FilePath); // Comment this out if you are on the same folder
chown($FileName,465); //Insert an Invalid UserId to set to Nobody Owner; for instance 465
$do = unlink($FileName);
if($do=="1"){
echo "The file was deleted successfully.";
} else { echo "There was an error trying to delete the file."; }
Try this. Hope it helps.
The file permission is okay (0777) but i think your on the shared server, so to delete your file correctly use;
1. create a correct path to your file
// delete from folder
$filename = 'test.txt';
$ifile = '/newy/made/link/uploads/'. $filename; // this is the actual path to the file you want to delete.
unlink($_SERVER['DOCUMENT_ROOT'] .$ifile); // use server document root
// your file will be removed from the folder
That small code will do the magic and remove any selected file you want from any folder provided the actual file path is collect.
In Windows and before PHP version 7.3.0, check that your file has been closed before unlinking it,
as said in https://www.php.net/manual/en/function.unlink.php :
On Windows, it is now possible to unlink() files with handles in use, while formerly that would fail. However, it is still not possible to re-create the unlinked file, until all handles to it have been closed.
As an exemple :
$fullFilePath = 'C:\Users\MyUserName\www\myApp\public\test.txt';
$handle = fopen($fullFilePath , 'w+');
fopen($filePath, 'w+');
fputs($handle, 'Some text in the file');
fclose($handle);
unlink(realpath($insertedLinesFilePath));

Using PHP to create a new directory, and a file in that directory

I recently started to mess around with some HTML and PHP, and have run into what is probably a super easy to solve error, but one I have not for the life of me been able to fix.
In a nutshell, what I'm trying to do is create a directory, then create a .txt file in that directory with the same name, something to the effect of "/number/number.txt". While it seems simple to create a file, or to create a directory, I'm having no end of troubles trying to do both.
Here's my code:
mkdir($postnum, 0777);
chmod($postnum, 0777);
$post = "/" . $postnum . "/" . $postnum . ".txt";
$post = boards(__FILE__) . $post;
$po = fopen($post, 'w+') or die("Can't open file");
chmod($post, 0777);
A few issues I ran into writing this, I read that using mkdir I could set the permissions of the directory I create, but despite doing what I believe to be the right way of doing so, it didn't do anything. So I ran chmod right after.
Then, I had hoped that just /$postnum/$postnum.txt would work for the directory to open, but I get the die text when I try just that, I had to add in the "boards(FILE) part to get it to work. (Side note, "boards" is the directory I'm working in)
Even then, while it doesn't give me "Can't open file", it isn't creating the file or anything.
I've made certain that any file or folder even remotely interacting with the files has had it's permissions set to 0777, so it's likely not an issue with that. (Also, I know that having all my files completely open like that may not be the best idea; once I get this working properly, I'll be sure to set the permissions to something more safe)
Any idea what I'm doing wrong?
making directory and creating path:-
$new_folder=mkdir('uploads\\'.$folder_name, 0777, true);
('uploads\\'.$folder_name)->path where you create folder like www->upload->folder name(whatever you provide)
$path="uploads\\".$folder_name."\\";//if you want to provide path you can provide this way
The mkdir function takes a path relative to the root of the filesystem, not the document root.
If you're on a *nix system, your website document root is likely in a path such as /var/www/sitename/html, but you're trying to create /number/number.txt which your account doesn't have permissions to do. Change the path you pass into mkdir to somewhere in the filesystem you have permissions to create folders and files and your code should work.
Edit:
The code in your question doesn't actually attempt to write anything. By calling fopen you've created a file handle, but you need to call fwrite to actually write data to the file. As suggested by #JamesSwift, you may want to take a look at file_put_contents.
Here, try this :
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
mkdir("$root/root_website_folder/php/testing_folder", 0777);
PHP uses your real path not the virtual one, we assign to $root the real path on your machine than add the virtual path of your website root

Categories