Permission of CSV-file in PHP - php

My php file has multiple errors. The first one has to do something with permissions. Normally there's nothing wrong with it it, but now I use a csv-file. I think that's the problem. But I don't understand the other warnings.
Which permission do I have to add?
$filename = "menuitems.csv";
$fp = fopen($filename, "r");
while ($data = fgetcsv($fp, filesize($filename), ",")) {
if (trim($data[0]) == '') break;
$href = strtolower(substr($data[1], 0, 7));
if ($href == "http://")
print "<li> " . $data[0] . "</li>\n";
else
print "<li> <a href=\"" . data[1] . "\" >" . $data[0] . "</a></li>\n";
}
fclose($fp);
echo "</ul>\n";
echo "</div>\n";
Warning: fopen(menuitems.csv): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/basis/php/index.php on line 10
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/basis/php/index.php on line 11
Warning: fclose() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/basis/php/index.php on line 21

the csv file that was moved into the document root does not have permissions set correctly. You can do this several ways, including changing permission in the file properties dialog (via right click menu) or use the php command:
chmod("/somedir/somefile", 0644);
but installing xampp in its own folder off the root directory instead of Program Files directory will fix this issue in most XAMPP versions.

Related

ZipArchive ExtractTo() Permission Denied

I'm trying to extract zip files from a folder into a subfolder. The error I'm recieving is:
Warning: ZipArchive::extractTo(XXXX): failed to open stream: Permission denied in /var/www/html/update_alerts2.php on line 97
extracted
Code :
public function extractFiles($inputDir,$outputDir) {
$files_to_extract = $this->getFiles($inputDir);
$zip = new ZipArchive();
foreach ($files_to_extract as $file) {
echo $file;
$res = $zip->open($inputDir . $file);
if ($res) {
$zip->extractTo($outputDir);
$zip->deleteName($inputDir . $file);
$zip->close();
} else {
echo 'failed, code:' . $res;
}
}
}
I've tried setting permissions to 777 but I still get the error. What are the possible ways I can fix this issue (I'm using centos7 and apache) ?

move_uploaded_file(/Advanced Java Programming.pdf): failed to open stream: Permission denied

So, I am working on a system where people working in a company can upload files to a system which will be sorted by departments. I have managed to get the files info(name, size, type), but the problem occurs while trying to upload the file.
I get:
move_uploaded_file(/Advanced Java Programming.pdf): failed to open stream: Permission denied
move_uploaded_file(): Unable to move 'C:\xampp\tmp\php1B99.tmp' to '/Advanced Java Programming.pdf'
I have set the permissions of the folder where the file needs to be uploaded to everybody (777).
Here's my code
<?php
$department = $_POST['department'];
$file = $_FILES['fileToUpload'];
echo "<b>Department: </b>" . $department . "<br>";
echo "<b>Name: </b>" . $file['name']. "<br>";
echo "<b>Size: </b>" . $file['size'] . " bytes<br>";
echo "<b>Type: </b>" . $file['type'];
move_uploaded_file($file['tmp_name'], "/". $file['name']);
?>
Try using an absolute path for your destination or at least start it with the DIR-constant, "/" ist not a valid (Windows)-path.
Also think about using the constant DIRECTORY_SEPARATOR as "/" is a *nix-standard, but as you're running on Windows, it should be "\" - using the constant will hold the right slash for every system.

PHP fopen() with command line

ANSWER I WENT WITH BELOW!
So I have a PHP script that works prefect via the web. I would like to set it up on a scheduled task on the server that is running the web hosting. It is a windows 2008 R2 server. I seems to run fine minus the fact it won't make the output files. Do I need to have the full path name? Ex C:\logs.... or will below work? I would like to keep the script working both in web and command line.
$File3 = "" . $log_dir . "/" . date('m-d-Y') . ".txt";
$Handle3 = fopen($File3, 'a+');
$Data3 = "blah";
fwrite($Handle3, $Data3);
more info:
here are the errors from the log file:
fwrite() expects parameter 1 to be resource, boolean given in C:\Websites\wordpress\win\import.php on line 686
PHP Warning: file_get_contents(bins/bins-10-09-2013.txt): failed to open stream: No such file or directory in C:\Websites\wordpress\win\import.php on line 692
PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\Websites\wordpress\win\import.php on line 699
PHP Warning: fclose() expects parameter 1 to be resource, boolean given in C:\Websites\wordpress\win\import.php on line 709
WORKING!! Well, this is what I went with and it seems to work prefect.
$File = "" . $uploads_dir . "/import-" . date('m-d-Y-g-ia') . ".txt";
$Handle = fopen($file1, 'a+');
if ( $Handle === false ) {
$File = "C:\Websites\wordpress\win\\".$uploads_dir."\import-" . date('m-d-Y-g-ia') . ".txt";
$Handle = fopen($File, 'a+');
}
This way I can Use the php script both via web and on a scheduled task via windows.
It seems fopen is failing to open the file . Make sure the file is successfully opened :
$Handle3 = fopen($File3, 'a+');
if( $Handle3 === false ) echo 'Unable to open file';
If it fails , check below items one at a time :
Check file / folder permissions .
Check with different slashes "/" , "\" in the path .
Check if it works with absolute path ( full path ) .
Check by changing directory before fopen : chdir('path\to\php\script')
etc .
If the issue is with the path , one way make it work both cases is :
// First try with the path that works from web
$File3 = "" . $log_dir . "/" . date('m-d-Y') . ".txt";
$Handle3 = fopen( $File3, 'a+' );
if( $Handle3 === false )
{
// It failed so may be scheduled task invoked the script
// So use the path that works in invoked by scheduled task
$File3 = "path\that\works\from\command\line";
$Handle3 = fopen( $File3 ,'a+' );
}
if( $Handle3 === false )
{
echo 'Failed to open file';
exit;
}
else
{
// What ever
}
I've never used the full url for an fopen or fwrite. These are the things I would look at first:
What is the output when you echo $log_dir?
Does the directory exist?
Are permissions set correctly?
If you create the file before running the script, does it then successfully write to the file?
What is the value of $Handle3 after the fopen?
What is the return value of the fwrite that you call on the last line?
Is error handling set to all? (it should be for debugging)

PHP Script fopen failed to open stream: > Invalid argument Error

I am getting a warning.
Warning: fopen(76561197992146126 .txt): failed to open stream:
Invalid argument in C:\wamp\www\Download\t3.php on line 6
For anyone wondering, this is the contents of main.txt below (http://pastebin.com/53chSRRz)
<?php
$APIkey = 'APIKeyHere';
$file = file('C:\wamp\www\Download\main.txt');
foreach ($file as $link) {
$link2 = "http://api.steampowered.com/ITFItems_440/GetPlayerItems/v0001/?key=" . $APIkey . "&SteamID=" . $link . "&format=json";
$downloaded = file_get_contents($link2);
$fh = fopen($link . ".txt", "a"); //or die("can't open file");
fwrite($fh, $downloaded);
}
echo "Finished";
?>
If I replace "fopen($link . ".txt", "a")" with a static file-name it works. But I need $link to be the filename. It is imperative to my setup.
I am running Windows 7 x64 using WAMP 2.2 with PHP 5.4.3
Your link IDs have whitespace in them. Try adding $link = trim($link); before creating $link2.

PHP recursively removing folders

Here is function that I'm using for recursively deleting folders and files
function rmdir_recursively($dir) {
if (!is_dir($dir) || is_link($dir)) return unlink($dir);
foreach (scandir($dir) as $file) {
if ($file == '.' || $file == '..') continue;
if (!rmdir_recursively($dir . DIRECTORY_SEPARATOR . $file)) {
chmod($dir . DIRECTORY_SEPARATOR . $file, 0777);
if (!rmdir_recursively($dir . DIRECTORY_SEPARATOR . $file)) return false;
};
}
return rmdir($dir);
}
The problem is, when I send some folder inside root, it deletes this folder. But when I send root folder itself like that
rmdir_recursively("./");
It returns bunch of errors like below
PHP Warning: unlink(.//wp/wp-admin/network) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
[07-Oct-2012 02:16:09] PHP Warning: unlink(.//wp/wp-admin/user) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
[07-Oct-2012 02:16:09] PHP Warning: unlink(.//wp/wp-content) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
[07-Oct-2012 02:16:09] PHP Warning: unlink(.//wp/wp-content/plugins) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
[07-Oct-2012 02:16:09] PHP Warning: unlink(.//wp/wp-content/plugins/akismet) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
What am I missing?
You say you're sending it rmdir_recursively("./");?
But look at the code, you are later calling rmdir_recursively($dir . DIRECTORY_SEPARATOR . $file).
This means that you'll be trying to eliminate files in ".//".
Which is why your error message contains unlink(.//wp/wp-admin/user)
Use rmdir_recursively("."); instead.

Categories