Files not opening in php - 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.

Related

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

Reading file properties while using readdir() to get the files

I am trying to print file properties into a table while getting the files with readdir() but I am receiving an error:
"Warning: fileperms() [function.fileperms]: stat failed for 0.54322000 1352164273tunes.txt in C:\Users\Desktop\xampp\tybai5131displayBackups.php on line 24"
The file name is so long because I am naming it using microtime()
I get the same error for every function, not just fileperms()
Here is the PHP code I am using:
<table>
<tr><th>File Name</th><th>Owner ID</th><th>Permissions</th><th>File Size</th></tr>
<?php
//declare backup directory as a variable
$dirBackup = "backups/";
//check if backup directory exists
if(!is_dir($dirBackup)) {
//display error message if backup directory does not exist
print("You do not have a backup directory yet.");
} else {
//else open the directory for reading
$dirOpenedBackup = opendir($dirBackup);
while($backupFile = readdir($dirOpenedBackup)){
if($backupFile !== '.' && $backupFile !== '..'){
print("<tr><td><a href='backups/".$backupFile."'>" .$backupFile. "</a></td><td>".fileowner($backupFile)."</td><td>".fileperms($backupFile)."</td><td>".filesize($backupFile)."</td></tr>");
}
}
}//close !is_dir
?>
</table>
Any ideas of what I can do to get this to work properly?
Next to the physical existance of a file, there can be different other things that can prevent you from accessing the file under a specific user.
You need to verify if you can access the file and the directory the file is located with the user that is used by your PHP script to perform these calls (that depends on your server and PHP configuration). So first find out which is the username.

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

problem with Create File dynamically in 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

PHP filesize() works on all but one file, gives stat failed error

I'm writing a PHP page that generates a podcast feed by scraping an existing HTML page. Everything works great, but one of my mp3 files gives a "filesize(): stat failed" error. As best as I can tell, the file isn't corrupted, and it plays perfectly fine. I've also reuploaded the file to the server. It falls in the middle range of all the file sizes, so I don't think the file is too large. Because every other file returns a file size, I'm assuming the problem is with the mp3 file, not with my server configuration. Is there something else I should be checking?
Here's the relevant part of my code:
$i = 1; //skipping header row on table
do {
$tr = $table->find('tr', $i);
$date = $tr->find('div', 0)->plaintext;
$datetime = new DateTime($date);
$speaker = $tr->find('div', 1)->plaintext;
$title = $tr->find('div', 2)->plaintext;
$url = $tr->find('div', 3)->find('a', 0)->href;
$fullurl = "http://domain.org/resources/".$url;
$filesize = filesize($url); //<---works on every file except one
echo "<item><title>".$title."</title>\n";
echo "<description>".$title." - ".$datetime->format('D, M jS, Y')." - ".$speaker."</description>\n";
echo "<itunes:author>".$speaker."</itunes:author>\n";
echo "<enclosure url=\"".$fullurl."\" length=\"".$filesize."\" type=\"audio/mpeg\"/>\n";
echo "<guid isPermaLink=\"true\">".$fullurl."</guid>\n";
echo "<pubDate>".$datetime->format('r')."</pubDate>\n";
echo "<itunes:explicit>clean</itunes:explicit></item>\n\n";
$i++;
}while ($table->find('tr', $i) != NULL);
As requested: (do people point out edits? This is my first question here..)
The filename is "12-20-09_AM_Podcast.mp3" which follows the naming convention of every other file, and all the files have permissions of 644. The full error code is
<b>Warning</b>: filesize() [<a href='function.filesize'>function.filesize</a>]: stat failed for audio/12-20-09_AM_Podcast.mp3 in <b>/homepages/1/d106955786/htdocs/victory/resources/podcast1.php</b> on line <b>45</b><br />
For some reason the web-server on domain.org isn't returning a Content-Length header field, which is causing filesize() to fail.
If the file is stored locally, filesize() the local copy of the file instead. If not, you cannot fix this issue as it is a problem on domain.org's web-server. You could work around the issue by downloading the file locally and checking filesize() then, but this will slow down your page majorly.
If the file is stored locally, check your file name or your anchor again. You might have misspelled one (or both) and Apache mod_speling is fixing it for you.

Categories