file creation failing in php - php

I am learning file handling in php. I have written the following code to create file:
<?php
$fileName = "testFile.txt";
$fileHandle = fopen($fileName,"w") or die ("can't open file");
fclose($fileHandle);
phpinfo();
?>
The problem is I am getting "can't open file". I changed the permission of the directory containing this file and all the files in the directory to 777 still the problem persists.
Can somebody please help me in solving this issue?
Thanks

have you tried using an absolute path for the $filename
$fileName = "/path/to/file/testFile.txt";
or you can change dir prior to opening the file
chdir('/path/to/file');
http://nz.php.net/manual/en/function.chdir.php
AMENDED ANSWER, TRY THIS
<?php
$path = "/path/to/file";
$fileName = "testFile.txt";
if (! file_exists($path)) {
die ("$path doesn't exist");
}
$fileHandle = fopen("$path/$fileName","w") or die ("can't open file");
fclose($fileHandle);
phpinfo();

is the php script and file in the same folder?
if not you need to specify the relative path to the testFile.txt

Try:
$fileHandle = fopen( dirname(__FILE__) . '/' . $fileName , 'w' ) ....

Related

Unable to open a file from PHP using fopen

i am trying to read contents from a text file in php. i am using wamp on windows. i m getting this error:
Warning: fopen(/input.txt): failed to open stream: No such file or directory in C:\wamp\www\cycle_gps_sender.php on line 3
this is my code:
$location = fopen("/input.txt", "r") or die("Unable to open file!");
echo $location;
fclose($location);
both the php file and input.txt are placed in www folder of wamp.
Hope this will help you:
$File = "log_post.txt";
$fh = fopen ($File, 't') or die("can't open file");
fclose($fh);
$location = fopen("input.txt", "r") or die("Unable to open file!");
echo $location;
fclose($location);
Use this code and keep the input.txt file in the same directory where this code is written.
First check if file exist or not?
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
chmod($filename, 0777);
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
$location = file_get_contents('./input.txt', FILE_USE_INCLUDE_PATH);
echo $location;
or
$location = file_get_contents('input.txt');
echo $location;
hope it will help
Add full path to file.
On the Windows platform, be careful to escape any backslashes used in the path to the file, or use forward slashes.
$location = fopen("C:\\folder\\input.txt", "r");
Remove '/'(Slash)
$location = fopen("input.txt", "r") or die("Unable to open file!");
$file = fopen("path/input.txt","a+") or die("Unable to open file!");
.....
fclose($file);
You have to create file before you READ or if you open file by 'r' , so if you open file by 'a+' , your file will be created automatically.

How to create one file and data store in php?

$myFile = "folder1/folder2/order.txt";
$fh = fopen($myFile, 'a');fwrite($fh, $Message);
fclose($fh);
I am use this code.But file is not create in that particular folder
Try this:
<?php
$myFile = "folder1/folder2/order.txt";
$fh = fopen($myFile, 'a'); or die("Unable to open the file");
fwrite($fh, $Message);
fclose($fh);
?>
If this code shows "Unable to open the file" that means there is something wrong while opening the file. This may happen if the folder1/folder2 doesn't exist. Or may be you are not permitted to open a file to write on that folder. If you are in Linux you can change the permission of you directory like this:
chmod 777 folder/folder2

PHP version: 5.1.6: Cannot write to file

I am using version 5.1.6 and am observing a strange issue. I cannot create and write to a file from the script whereas if I explicitly create a file and then run the script it writes data.
Am I missing something obvious in here?
The test code I am trying is:
$message = "Test";
$myFile = "testFile.txt";
if (file_exists($myFile)) {
$fh = fopen($myFile, 'a');
fwrite($fh, $message."\n");
} else {
chmod("/path/to/dir/*", 0755); //updated code
$fh = fopen($myFile, 'w') or die("Cannot open file \"$myFile\"...\n");
fwrite($fh, $message) ;
}
fclose($fh);
CONCLUSION:
Thanks for the responses everybody. It is a permission issue. I changed the directory path and it works :)
Your code is fine. Only the line where chmod resides, is not required.
Commented out chmod("/path/to/dir/*", 0755); this will chmod all files within the set folder.
Consult the PHP manual on chmod at
http://php.net/manual/en/function.chmod.php
$message = "Test";
$myFile = "testFile.txt";
if (file_exists($myFile)) {
$fh = fopen($myFile, 'a');
fwrite($fh, $message."\n");
} else {
//chmod("/path/to/dir/*", 0755);
$fh = fopen($myFile, 'w') or die("Cannot open file \"$myFile\"...\n");
fwrite($fh, $message) ;
}
fclose($fh);
I had a similar problem and solved it by changing the owner of the folder to the apache user. This should give your php script needed permissions for making files and writing to the files in that folder.
I guess you won't be able to chown the folder from php script, only through server access (ssh or ftp). At least, that was the path I had to go.

creating text file throws error

I am a php beginner.I want every time when the web page is open to create a file that does not exist. But every time when I run the program I have an error teling me that the file was not created.
This is my code:
$ip=$_SERVER["REMOTE_ADDR"];
if(!isset($_COOKIE['firsttime'])){
setcookie('firsttime', 'no');
$myfile = 'file/form'.$ip.'.txt';
if(file_exists($myfile) == FALSE){
$fo = fopen($myfile, 'w');
$code = '<form action = "" method = "post">';
fwrite($fo, $code);
fclose($fo);
}else{
unlink($myfile);
$file = new File();
}
}
where is my mistake?
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
Do this to open a file, and it will create the file if it doesn't exist.
Not completely sure but that would result in a pretty weird file name.
$myfile = 'file/form'.$ip.'.txt';
If my ip is 1.0.0.01.23 (really random and pretty weird) the file name would be:
file/form.1.0.0.01.23..txt
Try saving a file with that name in notepad.

Cannot create file with fopen in new directory

$new_file=$data['url']."_files";
if(!is_dir($new_file))
mkdir ($new_file);
echo $new_file=$new_file.'\class.ticket.php';
$ourFileHandle = fopen($new_file, 'w') or die("can't open file");
echo fwrite($ourFileHandle, $new_data) or die('cannot write');
fclose($ourFileHandle);
fwrite() returns 1473
this does not write new file in other directory but creates file '$newfile\class.ticket.php in the same directory.
Can anone explain how i can put file in new directory.
Thanks
On what operating system is your server running? in linux, directory separator is a forward slash, so maybe that's causing a problem. use the predefined constant DIRECTORY_SEPARATOR to return the correct separator for the OS.
Try this:
$data['url'] = 'foo';
$new_data = '<?php echo \'Hello World!\';';
$new_file=$data['url']."_files";
if(!is_dir($new_file))
mkdir ($new_file);
$new_file=$new_file.'/class.ticket.php';
$ourFileHandle = fopen($new_file, 'w') or die("can't open file");
fwrite($ourFileHandle, $new_data) or die('cannot write');
fclose($ourFileHandle);

Categories