While running the code which is used to connect to the workbook I get fatal error.
Here's the code:
<?php
$filename = "C:/Users/.../Output.xlsb";
$sheet1 = "DAILY_OUTPUT";
$sheet2 = "DAILY_OUTPUT";
$excel_app = new COM("Excel.application") or Die ("Did not connect");
$Workbook = $excel_app->Workbooks->Open("$filename") or Die("Did not open $filename $Workbook");
$Worksheet = $Workbook->Worksheets($sheet1);
$Worksheet->activate;
$excel_cell = $Worksheet->Range("C4");
$excel_cell->activate;
$excel_result = $excel_cell->value;
print "$excel_result\n";
$Worksheet = $Workbook->Worksheets($sheet2);
$Worksheet->activate;
$excel_cell = $Worksheet->Range("C4");
$excel_cell->activate;
$excel_result = $excel_cell->value;
print "$excel_result\n";
#To close all instances of excel:
$Workbook->Close;
unset($Worksheet);
unset($Workbook);
$excel_app->Workbooks->Close();
$excel_app->Quit();
unset($excel_app);
And when I run it from PhpStorm to Google Chrome it shows:
Fatal error: in C:\Users\...\excCon.php on line 6
Edit: the "..." are just to shorten the URL here
Solution was wrong slashes.
Instead of single / I put double \ and now the path to the file is correctly read.
Related
So I am using the call php_com_dotnet in my code:
$word = new COM("word.application");
// Hide MS Word application window
$word->Visible = 0;
//Create new document
$word->Documents->Add();
// Define page margins
$word->Selection->PageSetup->LeftMargin = '2';
$word->Selection->PageSetup->RightMargin = '2';
// Define font settings
$word->Selection->Font->Name = 'Arial';
$word->Selection->Font->Size = 10;
// Add text
$word->Selection->TypeText("TEXT!");
// Save document
$filename = tempnam(sys_get_temp_dir(), "word");
$word->Documents[1]->SaveAs($filename);
// Close and quit
$word->quit();
unset($word);
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=document_name.doc");
// Send file to browser
readfile($filename);
unlink($filename);
and I got his error:
Fatal error: Uncaught Error: Class 'COM' not found in /var/www/clients/client1/web1/web/nordin/save.php:6 Stack trace: #0 {main} thrown in /var/www/clients/client1/web1/web/nordin/save.php on line 6
so I looked around I added this to my php.ini file:
[COM_DOT_NET]
;extension=php_com_dotnet.dll
enable_dl = On
; extension_dir = "ext"
and it still doesn't work, what am I doing wrong? please help!
EDIT
my files are on a sftp server and I get error's like this:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
The semi-colon before the extension is a comment and means that extension is not enabled. To enable it you must remove that semi-colon:
;extension=php_com_dotnet.dll
becomes
extension=php_com_dotnet.dll
You may need to restart IIS after making this change.
I have searched the internet, and could not get any specific details about it.
The environment is Windows 8, WAMP , MONGODB
I am trying to design a webpage, which have 4 fields: Name,Contact,Device,Email. After an user hits the submit button, the data inserts in the MongoDb. All this works fine.
The issue starts when I try to write the inserted data in the csv file, as this is the requirement. I have tried MongoDB Export command in the cmd, and it works fine, but trying to call the same using exec function in the php script is proving to be futile.
I have tried with this also, by storing the command in a .bat file, and then calling the .bat file using the php's exec function, still, no effect
<?php
echo '<pre>';
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
exec("c:\WINDOWS\system32\cmd.exe /c START C:\wamp\bin\mongodb\mongodb-win32-x86_64-2008plus-2.4.3\conf\export.bat");
?>
I have enabled the checbox interaction with desktop in my WAMP server.
I don't need any specific help related with the coding, all I need is some direction on how to proceed ahead, as I know that I am missing something. Also, I reiterate, did not get anything specific on the Internet, hence, posting the question.
Kindly let me know on how to achieve this.
Thanks to everyone
The following may work
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=example.csv');
header('Pragma: no-cache');
$database = "DATABASE";
$colName = "COLLECTION";
$connection = new MongoClient();
$collection = $connection->$colName->$database;
$cursor = $collection->find();
foreach($cursor as $cur)
echo '"'.$cur['field_1'].'","'.$cur['field_2']."\"\n";
This code will dump your selected database to the a json file
$mongoExport = 'c:\mongodb\mongoexport'; //full path to mongoexport binary
$database = 'test';
$collection = 'foo';
$file = "c:\\temp\\foo.json"
exec(sprintf("%s -d %s -c %s -o %s",
$mongoExport,
$database,
$collection,
$file
));
And this is using pure PHP, for sure will be more fast the mongoexport option, with large collections:
$database = 'test';
$collection = 'foo';
$m = new MongoClient();
$col = $m->selectDB($database)->$collection;
$json = json_encode(iterator_to_array($col->find()));
set_time_limit(0);
ob_start();
error_reporting(E_ALL);
ini_set("display_errors",1);
$conn = new MongoClient("mongodb://Hostname:27017", array("replicaSet" => "rs0"));
if(!$conn){
die("Unable to connect with mongodb");
}
$db = $conn-><DB NAME>; // DB name
$col1 = $db-><colname>;
$col2 = $db-><colname>; // collection name which u want .
$filterCountry = array("status"=>"1"); // where query
$records = $col1->find($filterCountry);
$fp= fopen('exampleTest11.csv', 'w'); // open csv file in which u want write data.
$headings[] = "Code";
$headings[] = "Status" ;
$headings[] = "EMP CODE";
fputcsv($fp, $headings); // PUT Headings .
$cnt =0;
foreach($records as $val) {
$csvarr = array();
$csvarr['code']= $val['code']; // fetch data from database.
$csvarr['Status']= $val['status'];
$csvarr['emp_code']= $val['emp_code'];
fputcsv($fp, $csvarr);
$cnt++;
}
echo "Completed Successfully..".$cnt;
I try to view image in browser that i take from mongodb. the image correctly saved and I can download it perfectly using genghis.php but whenever I tried to load this using my own code whether ny using getBytes() or getResource(), the result only return bytes data such as this:
HDR¿£Ðß$iUßoÛT>‰oR¤? XG‡ŠÅ¯US[¹ÆI“¥íJ¥éØ*$ä:7‰©Û鶪O{7ü#ÙH§kk?ì<Ê»øÎí¾kktüqóÝ
Here is the code that I use to retrieve the image:
<?php
// Config
$dbhost = 'localhost';
$dbname = 'dbzuhra';
$colname = 'testData';
// Connect to test database
$m = new Mongo("mongodb://$dbhost");
$db = $m->$dbname;
$getGrid = $db->getGridFS();
$image = $getGrid->findOne(array('filename'=>'final_design.png'));
header('Content-type: image/png;');
$stream = $image->getResource();
while (!feof($stream)) {
echo fread($stream, 8192);
}
?>
Is there any explanation to why this happen?
Incorrect header:
header('Content-type: image/png;');
^--- don't put a semi-colon here
There is no image type image/png;. It's just image/png, and HTTP headers are delimited by line breaks, not semicolons.
I've got a simple php script for getting a csv file into array and inserting each row into MongoDB (CD Collection). But somehow insert returns an error after first successful one:
"Fatal error: in C:\xampp\htdocs\mongo\lesson1\index.php on line 17"
Here's the code. What could possibly cause such an error? DB receives only one (first).
$filename = 'd:/cd_col.csv'; // Each line: Title;No. of CD with movie
$csvfile = fopen($filename,'rb');
while(!feof($csvfile)) {
$csvarray[] = fgetcsv($csvfile);
}
$m = new MongoClient();
$db = $m->mymovies;
$collection = $db->movies;
$id=0;
foreach($csvarray as $key=>$value)
{
$movie = explode(';', $value[0]);
$fmovie = array('_id'=>++$id, 'title'=>$movie[0], 'cdno'=>$movie[1]);
if($collection->save($fmovie)===true) { // this is line 17
echo 'Successful insert: '.$key;
}
}
SOLVED:
Cannot use save in php, that's it.
You should use $collection->insert(); instead.
I am trying to load a file to my Cloud Files Container from a remote file.
Below is the example code a Cloud Files Support person gave me:
<?php
require('cloud/cloudfiles.php');
$res = fopen("http://images.piccsy.com/cache/images/66653-d71e1b-320-469.jpg", "rb");
$temp = tmpfile();
$size = 0.0;
while (!feof($res))
{
$bytes = fread($res, 1024);
fwrite($temp, $bytes);
$size += (float) strlen($bytes);
}
fclose($res);
fseek($temp, 0);
//
$auth = new CF_Authentication('user','token ');
//Calling the Authenticate method returns a valid storage token and allows you to connect to the CloudFiles Platform.
$auth->authenticate();
$conn = new CF_Connection($auth);
$container = $conn->create_container("example");
$object = $container->create_object("example.jpg");
$object->content_type = "image/jpeg";
$object->write($temp, $size);
fclose($temp);
?>
The problem I am getting is the below error:
Fatal error: Call to a member function create_container() on a non-object in /home2/sharingi/public_html/daily_dose/remote_test.php on line 24
Not sure exactly what I am not noticing here.
It seems $conn did not successfully instantiate an object. That is the problem, but the solution is not clear cut.
Is CF_Connection defined? Does error_reporting(E_ALL) give you more helpful info? What does var_dump($conn instanceof CF_Connection) output?
This should point you in the right direction.