Import image from local network in php - 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=";

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);

PHP file_exists does not work. It seems to be reversing true and false

While working on my photo gallery I decided it was best to have an alternate file if image that should display doesn't. I've looked on this site, and may others. They all say to use:
$VariableName = "photography/small/2014-09-21-red-1.png";
if (file_exists ($VariableName)) {echo "Yes!!!";}
else {echo "Nooo!!!";}
or
if (file_exists ("photography/small/2014-09-21-red-1.png")) {echo "Yes!!!";}
else {echo "Nooo!!!";}
For some reason, this will not work for me. It does work, but only when file_exists is set to !file_exists, which is saying: "if this file does not exist, display the image that exists (the image I want, not it's replacement)". In other words:
this is saying if apple exists, display "orange"; and if apple does not exist, display apple.
I've even placed the generated image link in the search bar (when using !file_exists), and after pressing enter, it brings me to the image. I've made sure that the images are set to 0777 in case that's interfering, but it seems to have no effect. All of the $DataRows variables are connected to a database and I've triple-checked that the file names in photography/small match those in the database table.
Why is this happening?
$URLPath = "http://localhost/~matthew/";
if (file_exists ($URLPath . "photography/small/" . $DataRows["DatePublished"] . "-" . $DataRows["FileName"] . "." . $DataRows["ImageExtension"])) {
echo '<img src="' . $URLPath . "photography/small/" . $DataRows["DatePublished"] . "-" . $DataRows["FileName"] . "." . $DataRows["ImageExtension"] . '" alt="' . $DataRows["PhotoName"] . '">' . "\n";
}
else {
echo '<img src="' . $URLPath . 'img/no-image.png" alt="Image Not Here">' . "\n";
}
Thank you very much for the help.
What happens if you change $URLPath from
$URLPath = "http://localhost/~matthew/";
to
$URLPath = $_SERVER['DOCUMENT_ROOT'] . "/~matthew/";
It looks like you are checking for a file but passing a URL into the function. It is probably returning false since the URL is not a valid path on your server. I would suggest using the actual path of the file or if you have to use a URL check out this post: How to check if a file exists from a url
Try using clearstatcache() and double check the file names and paths.
if (file_exists ($VariableName or "path-to-image")) {
This is gibberish. As long as "path-to-image" is not null, the operand will evaluate as (bool) true.
It should be:
if (file_exists ($VariableName)) {
It looks like you should be using
If(is_readable($variable)) {
But looking at your later code you are not using the construct you originally quoted.
file_exists() works as described for me in lots of different contexts.

Cakephp: Show image if uploaded or show text if image was not uploaded

I am developing a system where the user may upload up to four photos. If the user does not upload a photo I would like that the follow text will be displayed: 'empty'.
I have prepared the below code however I didn't managed :/ The problem is that when an image is uploaded it still prints 'empty' and the uploaded image does not show up.
<?php if (file_exists('../files/collection/photo2/' .$collection['Collection']['photo_dir2'] . '/thumb_' . $collection['Collection']['photo2']))
{
echo $this->Html->image('../files/collection/photo2/' . $collection['Collection']['photo_dir2'] . '/thumb_' . $collection['Collection']['photo2']);}
else
{
echo ('empty');
}
?>
I appreciate you guidance and help :)
TLDR: The file you're checking for doesn't exist. Fix your path.
Detail:
You're trying to use the same path for both your HTML <img> as well as the PHP file_exists() check.
The problem is, that the HTML image is looking for a file via the user's browser, where the file_exists() method is looking for the file via your server. The two paths are very rarely the same.
Try using a correct path in your PHP's file_exists() method, and it should pass the check.
For example:
if(file_exists(APP . 'files' . DS . 'collection' . DS . 'photos2' . $collection['Collection']['photo_dir2'] . DS . 'thumb_' . $collection['Collection']['photo2'])) {
echo $this->Html->image('../files/collection/photo2/' . $collection['Collection']['photo_dir2'] . '/thumb_' . $collection['Collection']['photo2']);
}
else {
echo ('empty');
}

google-api-php-client - Get File Contents

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"

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

Categories