Change Location In tempnam - php

I am having some trouble providing the directory path in tempnam();. When i use tempnam('', 'xyz'); , the file is getting stored in tmp directory. But i need to store the file in a specific directory permanently. I am using CodeIgniter. I want to save the files in folder docs which is located in the same directory as application and system. What location should I give in tempnam();? UPDATE: I tried providing with location path ie tempnam($_SERVER['DOCUMENT_ROOT'].'/exotel/docs' , 'xyz'); but still the file is saved in tmp folder

RTLM: http://www.php.net/manual/en/function.tempnam.php
As per the above docs, if the specified directory does not exist, tempnam() may use the system temp dir. You're specifying an empty string directory (''), which will not exist, so PHP is free to use whatever directory it wants.
Try
$temp = tempnam('/path/to/where/you/want/the/file', 'xyz');
instead.

see documentation for tempnam here. First parameter is a directory.
Example from docs:
<?php
$tmpfname = tempnam("/tmp", "FOO");
$handle = fopen($tmpfname, "w");
fwrite($handle, "writing to tempfile");
fclose($handle);
// do here something
unlink($tmpfname);
?>

Related

fopen failing in Kernel Command but not in controller Laravel

I am using fopen to generate a csv file. The function works as expected in a test controller but when I move to a Console/Commands file, it fails with 'failed to open stream: No such file or directory'.
This is the fopen line;
$fp = fopen('dataFiles/dataFile_'.date('Ymd').'.csv', 'w');
I have tried to change the url declaration to;
$fp = fopen('/dataFiles/dataFile_'.date('Ymd').'.csv', 'w');
No dice. The dataFiles folder is in my projects public dir.
What could be the issue here?
This is a 5.3 project.
Thanks!
you have to use the public_path() to specify the public folder location. otherwise the file location is taken from the existing folder. in your case it is checking if the is file existing in Console folder, in your second example it is taking the file path from the project root.
$fp = fopen(public_path('dataFiles/dataFile_'.date('Ymd').'.csv', 'w'));
I have one suggestion for you. if you are not planning to make this files public you should put them in the storage folder. That's the place for these kind of files
Path helper functions

PhP fopen creates file with Path name

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+");

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();
}

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));

Editing files outside of the public_html folder from inside the public_html folder

Using php, how can I edit a file that is located in a folder outside of the public_html folder from a file that is located inside the public_html folder? I tried using the include function for the filename of the file outside of the public_html folder, but it runs the external file displaying contents of it (using fputs) for the current file instead of just updating the external file.
<?php
include "hits.txt";
$filename = "hits.txt";
$count = file($filename);
$count[0]++;
$file = fopen ($filename, "w") or die ("Cannot find $filename");
fputs($file, "$count[0]");
fclose($file);
?>
From what I understand, you want to simply edit a file in the parent directory? To do this, you'll need to provide the open function with either an absolute/relative path to your file. That is:
$file_relative = "../../file.txt";
$file_absolute = "/some_file/www/file.txt";
fopen($file_relative, "w");
// Edit your file as necessary
Please note, that while this is technically feasible, it may be disallowed. You may not have the appropriate permissions to edit anything above public_html.

Categories