google-api-php-client - Get File Contents - php

Using the following code:
// get the file contents
$file = new Google_Service_Drive_DriveFile();
$fileId = 'The File ID';
try {
echo 'try part <br>';
$result3 = $service->files->get($fileId);
echo 'Contents: ' . $result3->getContent().'<br>';
echo "Title: " . $result3->getTitle() . '<br>';
echo "Description: " . $result3->getDescription() . '<br>';
echo "MIME type: " . $result3->getMimeType() . '<br>';
} catch (Exception $e) {
echo "An error occurred: " . $e->getMessage();
};
Everything works except for this line:
echo 'Contents: ' . $result3->getContent().'<br>';
In the drive.php file in the API, there is a getContent() function, but it's not working. How do I get the content out of the file?

If you refer to the Google documentation at https://developers.google.com/drive/v2/reference/files/get you'll see that there are two forms of the Get method. The default fetches the metadata (eg. title, description, etc), whereas alt=media fetches the content. Alternatively, you can also fetch the content via the downloadUrl property. I don't know if the PHP library exposes the alt=media option (I don't like libraries), but the downloadUrl approach has an example on the page I linked, just scroll down and click "php"

Related

Searching for file in directory using glob, not returning results

I'm trying to make a small script for retrieving configuration files containing mapping instructions for a product import, using php glob. But I fail miserably.
What I have right now:
echo "Shop type: " . $this->shopType . '<br />';
echo "Shop version: " . $this->shopVersion . '<br />';
echo "Glob search: config/mappings/" . $this->shopType . "_" . $this->shopVersion . ".php";
foreach (glob("config/mappings/" . $this->shopType . "_" . $this->shopVersion . ".php") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
exit;
$this->shopType represents the name of the shop from which the export file should be imported. E.g. ccvshop.
$this->shopVersion represents the version of the shop, to be able to write different import mappings for different versions of the shop types.
The value of shopType = ccvshop.
The value of shopVersion = 11.2.
Which would make the search string in the glob function:
config/mappings/ccvshop_11.2.php.
Unfortunately my result is empty, and I can't see what I'm doing wrong.
Any help would be appreciated.
I added an image representing my directory structure:
glob() returns an array of matching files. It looks like you already know the name of the file that you want to read. Try something like this:
$filename = "config/mappings/" . $this->shopType . "_" . $this->shopVersion . ".php";
if (file_exists($filename))
echo "$filename:" . filesize($filename);

Retrieve original uploaded filename using php on OpenShift

I would like to check that a file uploaded to my OpenShift app has a text extension (.txt or .tab). Following some advice given here I wrote the following code, with echoes added to help debug:
$AllowedExts = array('txt','tab');
echo "AllowedExts: " . $AllowedExts[0] . " and " . $AllowedExts[1] . "<br>";
$ThisPath = $_FILES['uploadedfile']['tmp_name'];
echo "ThisPath: " . $ThisPath . "<br>";
$ThisExt = pathinfo($ThisPath, PATHINFO_EXTENSION);
echo "ThisExt: " . $ThisExt . "<br>";
if(!in_array($ThisExt,$AllowedExts) ) {
$error = 'Uploaded file must end in .txt or .tab';
}
echo "error echo: " . $error . "<br>";
On uploading any file, the echoed response was:
AllowedExts: txt and tab
ThisPath: /var/lib/openshift/************/php/tmp/phpSmk2Ew
ThisExt:
error echo: Uploaded file must end in .txt or .tab
Does this mean that OpenShift is renaming the file upon upload? How do I get the original filename and then check its suffix? More generally, is there a better way to check the file type?
$_FILES['uploadedfile']['tmp_name'] contains the name of a temporary file on the server (which can be moved with move_uploaded_file()). If you want to check the original name of the uploaded file on the client machine use $_FILES['uploadedfile']['name'].
That's not an Open Shift issue, it's the standard way of PHP.
For further details see http://php.net/manual/en/features.file-upload.post-method.php
For other ways to detect the file type see http://php.net/manual/en/ref.fileinfo.php

Import image from local network in php

I have a script that fetches some pictures in php and display them on my website. The thing is when I out of my local network and access the site, the images won't display, cause it have the local url. Is there a way to import them temporary?. So, I will be able to see it outside the local network? Without the need of giving public access of the image link.
Here a sample of the code:
foreach($sbJSON->{data}->{soon} as $show) {
$newepisode = "S" . $show->{//.≥≥season} . "E" . $show->{episode};
$text = $show->{show_name} . " " . $newepisode . ": " . $show->{airdate};
$newfeedimage = $feedimage . $show->{tvdbid};
echo "<h1>$text</h1>";
echo "<br>";
echo '<img src="'.$newfeedimage.'"/>';
}
And here the image url of my local network:
$feedimage = "http://".$ip."/api/".$api."/?cmd=show.getbanner&tvdbid=";

print all triples into RDF files

i'm using rdfapi-php library and trying to open my RDF file.
Here are the code edited from the tutorial to show the content parsed into the way the library explain.
//include RDF API
define("RDFAPI_INCLUDE_DIR", "C:/OpenSA/Apache/htdocs/rdf_api/api/");
include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
// Filename of an RDF document
$base="example1.rdf";
// Create a new MemModel
$model = ModelFactory::getDefaultModel();
// Load and parse document
$model->load($base);
// Get Iterator from model
$it = $model->getStatementIterator();
// Traverse model and output statements
while ($it->hasNext()) {
$statement = $it->next();
echo "Statement number: " . $it->getCurrentPosition() . "<BR>";
echo "Subject: " . $statement->getLabelSubject() . "<BR>";
echo "Predicate: " . $statement->getLabelPredicate() . "<BR>";
echo "Object: " . $statement->getLabelObject() . "<P>";
}
that is the output
Statement number: 0
Subject: http://www.w3.org/Home/Lassila
Predicate: http://description.org/schema/Creator
Object: e85740
Statement number: 1
Subject: e85740
Predicate: http://www.w3.org/1999/02/22-rdf-syntax-ns#type
Object: http://description.org/schema/Person
My problem is that in the variable $base i will have an array with the name of all my RDF files. How i can edit the script to get the content of all the file of the array and after that print every RDF triple?
Regards

Word Doc File to PDF on Windows Server

I am trying to convert .docx files to pdf using PHP on a Windows server. I tried several of the solutions from other posts, including phpdocx (which does a very poor conversion that doesn't keep any formatting) and php's Com object. I only have Office 2003, so there is no pdf converter available using Com.
I thought of using OpenOffice/LibreOffice, but haven't found any information about installing and using Com for these on a windows server (I know it can be installed, but I can't figure out how to set up Com for it).
Using a webservice is not an option due to the data on the forms (they have to remain on our server). This means that Zend Framework cannot be used.
Any suggestions would be helpful, or information about using Com with Open Office.
I was finally able to get this working. OUr problem was that Word 2003 didn't have a PDF converter in it. We ended up using a trial version of Office 2010 for now (assuming that everything works correctly, we will purchase the full version). Word 2007 would have worked also. Below is the code I used to get this working:
//Word Doc to PDF using Com
ini_set("com.allow_dcom","true");
try{
$word = new com('word.application') or die('MS Word could not be loaded');
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getTraceAsString();
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
$word->Visible = 0;
$word->DisplayAlerts = 0;
try{
$doc = $word->Documents->Open(DOC_LOCATION. 'test_image.docx');
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
echo "doc opened";
try{
$doc->ExportAsFixedFormat(DOC_LOCATION . "test_image.pdf", 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getTraceAsString();
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
echo "created pdf";
$word->Quit();
$word = null;

Categories