problem with Create File dynamically in php - php

I want to create File that have Full permission dynamically, that means every change for ID of session create new file .
Unfortunately I faced some problem .
Warning: fopen(test.txt) [function.fopen]: failed to open stream: Permission denied in /home/teamroom/public_html/1/3.php on line 2
Warning: fwrite(): supplied argument is not a valid stream resource in /home/teamroom/public_html/1/3.php on line 3
Warning: fclose(): supplied argument is not a valid stream resource in /home/teamroom/public_html/1/3.php on line 4
code :
<?php
session();
$member_Id=$_SESSION['user_id'];
if (isset($member_Id)){
$file = fopen("test.txt","x+");
fwrite($file,"test");
fclose($file);
}
?>
can you help me ?
or can you tell another way to do this idea ?

It would appear that the process PHP is running as (often the web server, e.g. www-data) does not have write permissions for the folder you're trying to create the file in
(e.g. /home/teamroom/public_html/1/).
You also should be doing error checking on the fopen() call. Then there's the security assect to think of.

you have no permissions to access directory. Use php-function chmod ("/somedir/somefile", 755); or change directory permissions by ftp-client.
And why are you trying to open file with x+ if you need only writing:
Modes:
r - Reading only, beginning of file
r+ - Reading and writing, beginning of file
w - Writing only, beginning of file
w+ - Writing and reading, beginning of file
a - Writing only, end of file
a+ - Writing and reading, end of file
x - Create and open for writing only, beginning of file
x+ - Create and open for reading and writing, beginning of file
If the file does not exist and you use w, w+, a or a+ it will attempt to create the file.
I think you can use w+ or a+
And for your another problem:
<?php
$fp = fopen ('/path/to/file', "r");
while (!feof ($fp))
{
$value = fgets($fp);
if(!empty($value))
{
//Here do what you want with your value
}
}
?>
This was string-by-string reading code. Also you can use file_get_contents(); php-function and work with it lika string.
P.S> Sorry for my english

Related

Files not opening in php

I am getting file path from MySQL which is stored in my db to read .txt file content but cannot figure out what's going wrong.
$myfilepath=$record['CodeFilePath']; // here getting file path
$content=file($myfilepath); // giving it to function file()
foreach($content as $val){ //reading the content from file .txt
echo $val;
}
The error is
WARNING: FILE(TUTORIAL1.TXT ): FAILED TO OPEN STREAM: INVALID ARGUMENT IN
Ensure that you don't have spaces around the name using....
$content=file(trim($myfilepath));
Also if your using a *nix platform, file names are case sensitive, so TUTORIAL1.TXT is not the same file as tutorial1.txt. Windows is a lot more forgiving.

Why is PHP mixing two fopen handles on one file?

I noticed while testing two fopen() handles on one file, that the handles or channels mix, and the file contents empty when i call fread(). One handle is read, and one handle is write.
Example code:
$rh = fopen('existingfilewithcontent.txt', 'r');
$wh = fopen('existingfilewithcontent.txt', 'w');
echo fread($rh, 1000);
fclose($rh);
fclose($wh);
// file is now blank
This is tested on Linux & Windows.
I could not find anything in the PHP docs about it.
Please do not ask my why I would want two handles on one file as that is not the question.
Thankyou
Opening a file for write is destructive.
Manual says:
'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.
You probably want:
'r+' - Open for reading and writing; place the file pointer at the beginning of the file.
OR
'a+' - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() only affects the reading position, writes are always appended.
See manual: http://php.net/manual/en/function.fopen.php

PHP- Moving file using URL

So the scenario is, I have an ecommerce site which involves users uploading files & details, the links to the files & the text of the details is saved into a text file. All this stuff is uploaded into a temporary folder.
The payment system I have integrated is Paypal. I have Paypal send a response to the IPN. In this file, I send some emails, but I also wish to move the files into a permanent folder and thus edit the links in the text file. But I can't seem to access the files properly.
This is my error codes:
file_get_contents( ../uploads/tmp/file/*FILE NAME HERE*.doc) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: No such file or directory in /home/*username here*/public_html/*name*/*dir*/ipn.php on line 103
PHP Warning: file_put_contents( ../uploads/tmp/file/*FILE NAME HERE*.doc) [<a href='function.file-put-contents'>function.file-put-contents</a>]: failed to open stream: No such file or directory in /home/*username here*/public_html/*name*/*dir*/ipn.php on line 103
PHP Warning: copy( ../uploads/tmp/file/*FILE NAME HERE*).doc) [<a href='function.copy'>function.copy</a>]: failed to open stream: No such file or directory in *link to ipn.php* on line 106
//This is my code
if ($key == 'Book File '){
$oldBookFileName = $result['Book File '];
$oldBookLink = str_replace('*BASE URL IS HERE*', '../',$oldBookFileName);
if (strpos($result['Book File '],'/tmp/') !== false) {
$newBookFileName = str_replace("/tmp/","/perm /",$oldBookFileName);
$newBookLink = str_replace('*BASE URL IS HERE*', '../',$newBookFileName);
}
//update file names in file & move files
//include ('updatefile.php');
file_put_contents($oldBookLink, str_replace($oldBookFileName, $newBookFileName, file_get_contents($oldBookLink)));
//copy/move files from tmp to perm
copy($oldBookLink, $newBookLink);
}
I've tried using the full path (www.example.com/dir/file.php) and using the relative path (../dir/file.php). Also, all the links are correct, I echo'ed them out in an email and they are correct.
Anyone know what Im doing wrong? Something totally retarded? Please help.
Thank you.
You likely have the relative path wrong. Try using realpath() to see what path PHP is actually using.
For the full path you need to use the path on the server, not the http URL. So something like /home/username/public_html/dir/filename.doc instead of www.example.com/dir/filename.doc

Warning: move_uploaded_file() failed to open stream

I am trying to upload file on ftp.
here is my code
$jname= "Accounts of Biotechnology Research";
if (!is_dir('/Trade/upload/ '.$jname)) {
mkdir('/Trade/upload/ '.$jname); // line 63
}
move_uploaded_file($_FILES["submission_file"]["tmp_name"], "/Trade/upload/$jname/" . $dup_name ); // line 67
Trade is a folder inside public_html folder.
When i am uploading a file it gives me a warning like,
Warning: mkdir() [function.mkdir]: No such file or directory in /home/my_username/public_html/Trade/upload.php on line 63
Warning: move_uploaded_file(/Trade/upload/Accounts of Biotechnology Research/76164762-sm.pdf) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/my_username/public_html/Trade/upload.php on line 67
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phphZXp0O' to '/Trade/upload/Accounts of Biotechnology Research/76164762-sm.pdf' in /home/my_username/public_html/Trade/upload.php on line 67
First:
You have a space here mkdir('/Trade/upload/ '.$jname);. Suppose you should have mkdir('/Trade/upload/'.$jname); (same for is_dir)
Second:
Ensure that you can write into Trade/upload directory.
Third (and I suppose that is the real problem):
It looks like you are trying to upload into a directory with full path:
/home/my_username/public_html/Trade/upload/, but your code will try to create a directory with full path:
/Trade/upload/.
You need to change
if (!is_dir('/Trade/upload/ '.$jname)) {
mkdir('/Trade/upload/ '.$jname); // line 63
}
to
if (!is_dir('upload/'.$jname)) {
mkdir('upload/'.$jname); // line 63 (or maybe there should be Trade/upload, but suppose current working dir will be /home/my_username/public_html/Trade, so only upload/)
}
Another option is to force mkdir to create directories recursively:
mkdir('/Trade/upload/'.$jname, 0755, true);
But in that case, files will be uploaded into /Trade/upload/... instead of /home/my_username/public_html/Trade/upload/...
There are two things you should be aware of based on the error messages you received. I'm guessing /Trade isn't the root path on your machine since it's clear in the error that your actual path is /home/my_username/public_html/Trade/, so the first adjustment should be
$root_path = "/home/my_username/public_html/Trade/upload/";
The second adjustment I'd suggest is that you avoid pathnames with space in them:
$jname= "Accounts of Biotechnology Research"; //could be changed to
$jname= "Accounts_of_Biotechnology_Research"; //$jname = str_replace(" ","-",$jname) OR
$jname= "Accounts-of-Biotechnology-Research"; //$jname = str_replace(" ","-",$jname)
Finally take note of the space character on the following lines, they affect you final result:
if (!is_dir('/Trade/upload/ '.$jname)) { //AND
mkdir('/Trade/upload/ '.$jname);
}
Note the [space] between upload/ '.$jname in both strings.

Include error in writing html file from php

I seem to have some problem with my code here. It creates a file from the php file, but I get an error on the include path.
include('../include/config.php');
$name = ($_GET['createname']) ? $_GET['createname'] : $_POST['createname'];
function buildhtml($strphpfile, $strhtmlfile) {
ob_start();
include($strphpfile);
$data = ob_get_contents();
$fp = fopen ($strhtmlfile, "w");
fwrite($fp, $data);
fclose($fp);
ob_end_clean();
}
buildhtml('portfolio.php?name='.$name, "../gallery/".$name.".html");
The problem seems to be here:
'portfolio.php?name='.$name
Any way I can replace this, and still send the variable over?
Here's the error I get when I put ?name after the php extension:
Warning: include(portfolio.php?name=hyundai) [function.include]: failed to open stream: No such file or directory in D:\Projects\Metro Web\Coding\admin\create.php on line 15
Warning: include(portfolio.php?name=hyundai) [function.include]: failed to open stream: No such file or directory in D:\Projects\Metro Web\Coding\admin\create.php on line 15
Warning: include() [function.include]: Failed opening 'portfolio.php?name=hyundai' for inclusion (include_path='.;C:\php\pear') in D:\Projects\Metro Web\Coding\admin\create.php on line 15
Now I saw your code in the comment to a previous answer I'd like to point few things out
function buildhtml($strhtmlfile) {
ob_start(); // redundant
$fp = fopen ($strhtmlfile, "w"); // redundant
file_put_contents($strhtmlfile,
file_get_contents("http://host/portfolio.php?name={$name}"));
// where does $name come from?? ---^
close($fp); // also redundant
ob_end_clean(); // also redundant
}
buildhtml('../gallery/'.$name.'.html');
In PHP as in many other languages you can do things in different ways. What you've done is you took three different ways and followed only one (which is absolutely enough). So when you use functions file_put_contents() and file_get_contents() you don't need the buffer, that is the ob_ family of functions, because you never read anything in the buffer which you should then get with ob_get_contents(). Nor you need the file handles created and used by fopen(), fclose(), because you've never written to or read from the file handle i.e. with fwrite() or fread().
If I'm guessing correctly that the purpose of your function is to copy html pages to local files, my proposal would be the following:
function buildhtml($dest_path, $name) {
file_put_contents($dest_path,
file_get_contents("http://host/portfolio.php?name={$name}"));
}
buildhtml('../gallery/'.$name.'.html', $name);
file_put_contents($strhtmlfile, file_get_contents("http://host/portfolio.php?name={$name}"))
Is it ok?
The output of:
'portfolio.php?name='.$name, "../gallery/".$name.".html";
is:
portfolio.php?name=[your name]../gallery/[your name].html
Are you sure that's what you want ?
include/require statements in PHP allow you to access the code contained in a file which is already stored on the server
What you are trying to achieve is including the output result of executing the code in that file with specific parameters
The suggested example offered by MrSil allows you to request the execution of the code in those files and offer parameters. The reason it shows you a blank page is because file_put_contents 'saves data to a file' and file_get_contents does not echo the result, it returns it. Remove the file_put_contents call, and add an echo at the beginning of the line before file_get_contents and it should work.
echo file_get_contents('http://domain.com/file.php?param=1');
As a warning this approach forces the execution of 2 separate PHP processes. An include would have executed the code of the second file within the first process.
To make the include approach work you need to include the file as you first did but without specifying parameters. Before including each file you need to setup the parameters it is expecting such as $_GET['name'] = $name

Categories