I have Apache 2 and PHP installed on a Raspberry Pi 1 B+ (RASPBIAN STRETCH LITE
). I have a website running on it with a textbox and a PHP script that is supposed to save the contents of the textbox to a text file on the server when the user submits.
I tryed basically everything but the php script just wont save.
PHP does get the textbox-content (I tested it - it works just fine).
This is my PHP:
<?php
include "code/multiPage/topBar.html";
$dir = "/data/searches.txt";
if ($_REQUEST) {
$input = $_REQUEST["search"];
file_put_contents($dir, $input, FILE_APPEND);
}
?>
PHP is working properly aside from this problem.
The folder has read-write permissions for everyone.
I have also tryed to let PHP create its own folder with code similar to this:
if (!file_exists('path/to/directory')) {
mkdir('path/to/directory', 0777, true);
}
PHP can't even do that.
Thanks in advance for any help!
I figured it out. The problem was that the webserver didn't have permission to write to the directory. problem solved by running sudo chown -R www-data var/www/html/data thanks for the help! Have a nice day :D
First just try this separately from other code
<?php
file_put_contents("/data/searches.txt", "bla", FILE_APPEND);
?>
if it's not working try to get a last error
echo print_r( error_get_last ( ), true)
In order to make sure that you can write to the file you can always check if it's writable
if(is_writable($dir))
Also for debugging it's nice to see how much bites was written by file_put_contents
so the final code easy to debug will be like this:
<?php
include "code/multiPage/topBar.html";
$dir = "/data/searches.txt";
if(!is_writable($dir)){
echo "cannot write to file";
}
if (!empty($_REQUEST["search"])) {
$input = $_REQUEST["search"];
$bitecount = file_put_contents($dir, $input, FILE_APPEND);
}
echo $bitecount . " bites were written to the file";
?>
There is a thing. If $_REQUEST["search"]="" or $_REQUEST["search"]=null the if($_REQUEST) will be TRUE anyway
Related
I try to use/call Scilab with php. I followed instruction form https://www.ibm.com/developerworks/library/os-php-scilab/os-php-scilab-pdf.pdf but I cant make it work. I tried listing 1 and what I got is Array (). do I need to save sci/sce file in root folder before calling them in php? is Scilab can be called directly as the instruction?
I had exactly the same problem. I use scilab 6.0.1
This code works for me :
<?php
$path = 'C:\\wamp64\\apps\\scilab-6.0.1\\bin\\';
$path_script = 'C:\\wamp64\\www\\tests\\log.sce';
$command = $path.'WScilex-cli.exe -nb -f '.$path_script;
echo $command;
exec($command, $output);
echo '<pre>';
print_r ($output);
echo '</pre>';
?>
In the log.sce file, I saw matrix on firefox web page, save file and picture on my folder.
Best regards
I am having trouble using fopen() to create a text document for later use as a cookie file.
I have read the documentation for this function, but to no avail.
Notes:
Ubuntu
read / writable ("w+")
I have tried several storage locations including:
/home/jack/Desktop/cookie
/var/www/cookie
/home/jack/Documents/cookie
PHP
echo "debug";
echo "\r\n";
$cookie = fopen("/home/jack/Documents/cookie", "w+");
fclose($cookie);
if(!file_exists($cookie) || !is_writable($cookie))
{
if(!file_exists($cookie))
{
echo 'Cookie file does not exist.';
}
if(!is_writable($cookie))
{
echo 'Cookie file is not writable.';
}
exit;
}
Result
file is not created
Output to browser: debug Cookie file does not exist.Cookie file is not writable.
Other Fun Facts
I have tried using fopen(realpath("/home/jack/Documents/cookie"), "w+")
echo "\r\n" gives a space. Why not a newline?
I believe the problem must be something to do with my permissions to create the file, but I have no problem "right-click" creating the text document on the Desktop.
THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS
echo "debug";
echo "\n";
$jack = "jack";
$cookie = "/home/jack/Documents/cookie";
touch($cookie);
chmod($cookie, 0760);
if(!file_exists($cookie) || !is_writable($cookie))
{
if(!file_exists($cookie))
{
echo 'Cookie file does not exist.';
}
if(!is_writable($cookie))
{
echo 'Cookie file is not writable.';
}
exit;
}
fclose($cookie);
THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS
Instead of fopen()..
touch() to create
chmod() for permissions
I also added user name jack to www-data group.
chmod($path, 0760) group read / write
Reference
chmod() octal values here.
Look at the documentation for file_exists again. It does not take a file handle as an argument, it takes a string filename. The same is true for is_writable. Even if it did, you are opening the file handle and then immediately closing it, so I'm not sure why you're trying to use the file pointer at all after it's been closed.
You may be correct in that you have improper permissions set, but I would start here, first.
Also, if you're only trying to create the file, you may look into using the touch method, instead:
if( touch( $filename ) ) {
// It worked!
} else {
// It didn't work...
}
The web server is not executing as your user. touch /home/jack/Documents/cookie && chmod 777 /home/jack/Documents/cookie to allow the web server user to access the file.
Note this is BAD in production environments.
It looks like a permission issue. What user is PHP running as? It's likely running as www-data or something similar. You should make sure that the folders you are trying to write to are writable by either the user or group that PHP is running as. If you created those folders while logged in a jack, they probably belong to jack:jack and are not accessible by www-data:www-data.
You can also add jack to the www-data group, to make things a bit easier for development.
I am a beginner in PHP. I am writing a file program, but before I could properly use the logic and code it, I got stuck here.
I am writing this PHP program in Linux Ubuntu Terminal. Please don't worry about what I am trying to do with the program. Just helping me understand the error itself would be a great help.
I have a PHP file(a.php) with the following contents:
<?php
$input=fopen($argv[1],"r");
$col=$argv[2]-1;
while(!feof($input))
{
$x=fgets($input);
$array=explode("\t",$x);
}
fclose($input);
?>
Now when I give this in the command prompt:
php a.php 1
I should be getting nothing as an output, but I get around 3 to 4 blank lines. Can anybody tell me why?
The problem must be your file path that is $argv[1].
Try this
$filePath = 'Your file path here';
if( !#$input = fopen($filePath, "r") ) {
die("Could not open $filePath");
}
while(!feof($input))
{
$x=fgets($input);
$array=explode("\t",$x);
}
fclose($input);
This question already has answers here:
PHP file that should run once and delete itself. Is it possible?
(4 answers)
Closed 9 years ago.
I am trying to create a self-destruct file. Wht I mean is, if a conditional equates to true, the file deletes itself.
Seems to me the following code should do the trick. However, it does nothing. What am I doing wrong?
<?php
phpinfo();
// The following should be activated when the url is
// selfdestruct.php?delete=1, correct?
if ($_GET['delete']==1) {
$file = 'selfdestruct.php';
unlink($file);
}
?>
Thanks for your hep in advance! I appreciate it! :-)
I usually do it like this: (using your GET)
if ($_GET['delete']==1) {
unlink( __FILE__ ) or die("Please delete this file.");
echo "This file has been deleted.<br />";
}
If the file can't be deleted it exit the script and the last echo won't be shown.
And also, your code should work, a quick test you can do to check if the condition is met, but just unable to delete the file is:
if ($_GET['delete']==1) {
echo "Works!";
}
Have you checked your web server and php log files? Could be a permissions issue or that the web server is keeping the file open so it can't be deleted.
Also, try to pass the entire local path to the file to unlink. You could probably use unlink(_FILE_)
Also, is this on Windows or Linux? They handle "open" files a bit differently. I tested this on Linux and it works fine with
unlink(__FILE__);
Information about OS and running web server etc is probably good to add to a question of this nature.
Check these steps:
Put error_reporting(E_ALL); on the first line
Check your permissions
Put the delete code above anything else, after "error_reporting"
You should do the following:
if (isset($_GET['delete']) && $_GET['delete'] == '1') {
unlink(FILE);
}
I make a site and it has this feature to upload a file and that file is uploaded to a server
Im just a newbie to php I download xampp and I run this site that i made in my local machine.
My site is like this you upload a file then that file will be uploaded to a server, but when i tried unlink() because when i try to remove the filename to a database I also want to remove that pic on the server, but instead I got an error and it says "Permission denied".
question:
How can I got permission to use unlink();?
I only run this on my localmachine using xampp
Permission denied error happens because you're trying to delete a file without having enough/right permissions for doing that.
To do this you must be using superuser account or be the same user that have uploaded the file.
You can go to the directory from your command line and check the permissions that are set to the file.
The easiest solution is to loggin as administrator/root and delete the file.
Here is another work around:
// define if we under Windows
$tmp = dirname(__FILE__);
if (strpos($tmp, '/', 0)!==false) {
define('WINDOWS_SERVER', false);
} else {
define('WINDOWS_SERVER', true);
}
$deleteError = 0;
if (!WINDOWS_SERVER) {
if (!unlink($fileName)) {
$deleteError = 1;
}
} else {
$lines = array();
exec("DEL /F/Q \"$fileName\"", $lines, $deleteError);
}
if ($deleteError) {
echo 'file delete error';
}
And some more: PHP Manual, unlink(), Post 106952
I would recommend, always first to check PHP Manual (in case your question concerns PHP), just go to the page with function that you have problems with and just click search CTRL+F in your browser and enter, for example, Windows, and as a result, in your case, you would find at least 7 related posts to that or very close to that what you were looking for.
Read this URL
How to use Unlink() function
I found this information in the comments of the function unlink()
Under Windows System and Apache, denied access to file is an usual error to unlink file. To delete file you must to change file's owern. An example:
<?php
chown($TempDirectory."/".$FileName,666); //Insert an Invalid UserId to set to Nobody Owern; 666 is my standard for "Nobody"
unlink($TempDirectory."/".$FileName);
?>
So try something like this:
$Path = './doc/stuffs/sample.docx';
chown($Path, 666);
if ( unlink($Path) )
echo "success";
else
echo "fail";
EDIT 1
Try to use this in the path:
$Path = '.'.DIRECTORY_SEPARATOR.'doc'.DIRECTORY_SEPARATOR.'stuffs'.DIRECTORY_SEPARATOR.'sample.docx';