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.
Related
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";
I'm using codeigniter to create a CSV file, and I can write successfully inside the application structure, but not outside. The reason why I want to create the file outside the application structure cause I get a 403 permissions error when linking to the file.
Either my folder permissions are wrong (I've used 777) or my code is wrong. Please help.
The application is sitting at: domain.com/mysite/ci/
The files created in: domain.com/mysite/ci/_/files/ (I can create the file here, but can't link to download it
I would like to create the file in: domain.com/mysite/downloads/ (I cannot create the file here, but I can link stuff to it to download.
CodeIgniter
$this->load->dbutil();
$this->load->helper('file');
$delimiter = ",";
$newline = "\r\n";
$query = $this->db->query("SELECT * FROM songlist");
$data = $this->dbutil->csv_from_result($query, $delimiter, $newline);
$filePath = '_/files/songlist.csv';
echo "filePath=". $filePath. "</br>";
if (! write_file($filePath, $data)){
echo 'not done';
} else {
echo anchor(base_url(). $filePath);
}
}
I think the problem is that "_" is not a valid folder for web. Try changing it.
If you want to create the file in "mysite/downloads" you filepath would be:
$filePath = '../downloads/songlist.csv';
And i see no reason you shouldn't be able to create it there.
I am trying to use dompdf to save a form to an easily-readable .pdf file, and my processing script is below. I am receiving the error Warning: file_put_contents(/files/grantapps/NAME0.pdf) [function.file-put-contents]: failed to open stream: No such file or directory in /home/username/public_html/subdir/apps/dompdf/filename.php on line 39. (Line 39 is the final line in my full script.) I don't know how to resolve this issue.
allow_url_fopen is on, and I have tried all kinds of file paths, including the full directory (/home/username/public_html/subdir/files/grantapps/) and what's in the code below, but nothing worked.
require_once("dompdf_config.inc.php");
date_default_timezone_set('America/Detroit');
$html = 'code and whatnot is here';
$name = str_replace(" ", "", $_POST['form2name']);
$i = 0;
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
while(file_exists("files/grantapps/" . $name . $i . ".pdf")) {
$i++;
}
file_put_contents("files/grantapps/" . $name . $i . ".pdf", $dompdf->output());
There is definitly a problem with the destination folder path.
Your above error message says, it wants to put the contents to a file in the directory /files/grantapps/, which would be beyond your vhost, but somewhere in the system (see the leading absolute slash )
You should double check:
Is the directory /home/username/public_html/files/grantapps/ really present.
Contains your loop and your file_put_contents-Statement the absolute path /home/username/public_html/files/grantapps/
I was also stuck on the same kind of problem and I followed the simple steps below.
Just get the exact url of the file to which you want to copy, for example:
http://www.test.com/test.txt (file to copy)
Then pass the exact absolute folder path with filename where you do want to write that file.
If you are on a Windows machine then
d:/xampp/htdocs/upload/test.txt
If you are on a Linux machine then
/var/www/html/upload/test.txt
You can get the document root with the PHP function $_SERVER['DOCUMENT_ROOT'].
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);
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.