creating a textfile in a different directory - php

i created a text a file in the current directory using getcwd(), now i need create the text in a different directory. but i cant figure out how to go back using only php
what it did before
when im at
/var/www/website/mydomain/
the code is
$objData = serialize($name). "\r\n";
$createPath = getcwd().DIRECTORY_SEPARATOR."mytextdir/".$id.".txt";
echo $createPath;
$fp = fopen($createPath, "w");
fwrite($fp, unserialize($objData));
fclose($fp);
it creates the text in
/var/www/website/mydomain/mytextdir/###.txt
but now i need to create this text file on
/var/www/website/allTextfiles/###.txt
is this possible if so, can anybody help me on how to do it using something similer to this technique
getcwd().DIRECTORY_SEPARATOR."#####/".$##.".txt";

With double-dot you get to a higher folder.
So your path changes into this:
getcwd() . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . "allTextFiles/".$id.".txt";

Related

PHP create file in same directory as script

$content = "some text here";
$fp = fopen("myText.txt","w");
fwrite($fp,$content);
fclose($fp);
The above code creates a file in the folder where the PHP script is present. However when the script is called by Cpanel Cron then file is created in home directory.
I want file to be created in the same folder where the php script is present even if its run by cron. How to do that ?
Try using __DIR__ . "/myText.txt" as filename.
http://php.net/manual/en/language.constants.predefined.php
Try something like this, using the dirname(__FILE__) built-in macro.
<?php
$content = "some text here";
$this_directory = dirname(__FILE__);
$fp = fopen($this_directory . "/myText.txt", "w");
fwrite($fp, $content);
fclose($fp);
?>
__FILE__ is the full path of the currently running PHP script. dirname() returns the containing directory of a given file. So if your script was located at /mysite.com/home/dir/prog.php, dirname(__FILE__) would return...
/mysite.com/home/dir
Thus, the appended "./myText.txt" in the fopen statement. I hope this helps.

Codeigniter cannot unlink file created by codeigniter

I hope someone can help me with this.
I am creating a text file from the query results from a mysql DB. I then set the file to auto download. Once that is done I am trying to unlink the file. It fails to remove the file from the server. When I go to the location and manually try to delete the file it states that it is write protected.
I do not have root access to this system so I can not change the permissions on said file.
Here is the code, is there a way to not create a write protected file?
$leagueinfo = $this->livedraft_win_model->get_leagueinfo($sport, $leagueid);
$export = $this->my_model->get_export($sport, $leagueid);
$file_name = $leagueinfo['strat_id'] . '.IOD';
$export=strip_quotes($export);
$export = str_replace(", ",",",$export);
write_file('/tmp/' . $file_name, $export,'x+');
$data = file_get_contents('/tmp/' . $file_name, FILE_BINARY);
ob_clean();
force_download($file_name, $data);
array_map('unlink', glob("/tmp/*.IOD"));
I use the same unlink format to remove files that are uploaded to the same location and that works just fine. It is only when I try to remove the files that are created by codeigniter.
Thanks
Please try with below code.
$this->load->helper("url");
unlink(base_url('/tmp/' . $file_name));
Or try this
delete_files('/tmp/' . $file_name);
For more information please read this doc.
https://ellislab.com/codeigniter/user-guide/helpers/file_helper.html

password protect directory but still allow use on main page

I'm trying to move an image to a tmp directory on the server side and then send it to the client from the tmp folder, but things aren't quite working. Here's what I currently have.
//this is where the image resides permanently
$imgdirectory = "../" . $ms . "msimages/" . $img_filename;
//here I'm trying to get the contents of the img from its permanent location
$content = file_get_contents($imgdirectory);
//here I'm trying to create a new temporary file
file_put_contents('/tmp/img.jpg', $content);
...
echo "img {background:url(\"/tmp/img.jpg\")-" . $xleft . "px -" . $ytop . "px";}"; this is the css trying to use file that is supposed to be in the temp folder
and here's the css output I get when the page loads
img#{width:631px;height:453px;background:url("/tmp/img.jpg")-144px -112px;}
But unfortunately, there is no file at /tmp/img.jpg so something must not quite be working with the file_put_contents 404 image not found
btw, I'm not getting any errors about permissions to put contents in /tmp,
Any advice is appreciated.
Use copy instead of reading and writing files
bool copy ( string $source , string $dest [, resource $context ] )
And
file_put_contents('/tmp/img.jpg', $content);
will put it under root tmp directory not in your sites tmp directory.
Try
file_put_contents($_SERVER["DOCUMENT_ROOT"] . 'tmp/img.jpg', $content);

save xml file to different directory

I am looking to save an xml file to a different directory from the root using php5. any ideas?
//write to the file
$filename = $id . ".xml";
$doc->save($filename);
I want to save the file to the /xml/ directory.
Change the argument to $doc->save to include the path
$filename = '/xml/' . $id . ".xml";
$doc->save($filename);
Now the thing to bear in mind is that this is a filesystem path, not web URL so its literally going to save in /xml not DOCUMENT_ROOT/xml.

read all files in a directory php

Good day every one
I have a problem.
I have a folder in my webroot/files that has some documents in it, mainly doc files.
I want to read all the contents of the files. e.g. if there are 2 files, namely users.docx and funds.docx.
I want to be able to open each file, read its content, and write them to a single document.
so far the code i have writes only the 1st file, but the newly written file has a size of both the read files.
function writeToFile($insId,$path,$hospital){
$data = '';
$my_file = WWW_ROOT . 'files' . DS . 'instructions' . DS . $insId . DS ."Final Report ".$insId.".rtf";
$handle = fopen($my_file, 'w')or die('Cannot open file: '.$my_file); //implicitly creates file
foreach($hospital as $h){
$data .= file_get_contents($path.'/'.$h, false);
$data .= "\n";
echo($h."\n");
}
file_put_contents($my_file, $data);
fclose($handle);
}
A docx file is actually a directory so you will need to open that if you want to actually get to the xml that makes up the file. However, the contents of the directory is pretty complex and consists of many different files.
It's very unlikely that you will be able to find the right parts of each file and combine them together to make a valid file afterwards with a script.

Categories