All I want to check is that if a image exists in the link. I load it into an iframe. It was working fine but it seems they have removed the image but a blank.gif still exits.
NOTE: The link is a different domain
I tried the following codes in vain:
<?php
$varia = file_get_contents($url);
echo $varia;
echo "<pre>";
print_r(get_headers($url));
?>
and
$variablee = get_data($url);
pr($variablee);
All I get in the output is:
HTTP ERROR 404
Problem accessing
I want to put the condition that if blank.gif exits......some condition else some other condition.
What should I do?
In my opinion, the best way to test if an URI actual is an image, is to see what getimagesize returns. If it returns an array, it is an image :
function imageExists($uri) {
$info = #getimagesize($uri);
return is_array($info);
}
if (imageExists($uri)) {
...
}
Try
$content = #file_get_contents($url);
if ($content !== false) {
// FILE EXISTS
} else {
// FILE NOT EXISTS
}
?>
You can use this code:
if(getimagesize($url))
{
//image exist
}
else
{
//image not exist
}
Related
Firstly, I am "new"(only been coding for a couple months) to PHP and am trying to get a file for a logged in user to display.
I have tried a few options including baseband and url but it just dosn't seem to work the way I need it to.
Here is a snippet of my code:
$personCalendar = '/folder\calendars\people';
$personFiles = scandir($personCalendars);
$personID = $_SESSION['Person_ID'];
$test = preg_grep('/'.personID.'/',$personFiles);
print_r($test);
echo basename($test);
The output from the print_r gives me Array ( [312] => s15399.ics ) which is great, but I just need the s15399.ics part and have it append to the end of the page url something like https://servername.com/index.php/calendar?s15399.ics so they can take the file and "subscribe" to their calendar.
baseband does not being anything but I am not that surprised by that.
Is this possible, if not, what would be the best way you recommend to do this?
// using \ in a file path is not going to work. Use / instead.
// $personCalendar = '/folder\calendars\people';
$personCalendar = '/folder/calendars/people';
// scandir will work but it will eventually run into problems
// if you have lots and lots of files.
$personFiles = scandir($personCalendars);
$personID = $_SESSION['Person_ID'];
function url(){
return sprintf('%s//%s%s',
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
$_SERVER['SERVER_NAME'],
$_SERVER['REQUEST_URI']
);
}
// shouldn't the following be $personID, not personID?
$test = preg_grep('/'.$personID.'/', $personFiles);
print_r($test);
$filename = reset($test);
if ( $filename ) {
$fullUrl = url() . $filename;
echo $fullUrl;
} else {
echo "File not found\n";
}
If you need to find a file and you know the file name, you don't need to read all the files. If the file name is the same as the person id, then:
$filename = "s{$personID}.ics";
if ( file_exists($filename) ) {
//
} else {
//
}
You can use current();
$link=current($test);
I'm new to PHP so I wanted to know how do I make a script which you have to put a GET parameter (index.php?test=value) and it checks is value is in a txt file provided. If the value exists then print yes if no then print no, if there isn't a GET parameter print like ''empty''. Please help me how to do this.
$_GET['test'] = 'value';
if(empty($_GET['test'])) {
echo 'empty';
} else {
$file ='asdfasdfasvalue';
if(strpos($file, $_GET['test'])) {
echo 'yes';
} else {
echo 'no';
}
}
You should populate $file with the file's contents.
Functions being used...
http://php.net/manual/en/function.empty.php
http://php.net/strpos
http://php.net/manual/en/function.file-get-contents.php
I work on two servers (A and B, and I need to show on A some images from B. The problem is I need to test if the image exists (else I show only the name of the client).
When I test in local, this works:
if (file_exists(http://www.example.com/images/3.jpg)) {
// I show the file here
} else {
// I just show the name
}
But, that's don't works now. Help please !
You can't use file_exists for a checking remote files.
Use get_headers function and check, if the file returns 200 or not.
$file_headers = get_headers('http://www.example.com/images/3.jpg');
if ($file_headers[0] == 'HTTP/1.1 200 OK') {
echo "The file exists";
} else {
echo "The file doesn't exist";
}
try to use this
$array= get_headers("http://images.visitcanberra.com.au/images/canberra_hero_ima.jpg");
echo $h= $array[0];
it will return HTTP/1.1 404 Not Found if not exists and HTTP/1.1 200 OK if exists
function is_file_on_url_exists($url)
{
if (#file_get_contents($url, 0, NULL, 0, 1))
{
return true;
}
return false;
}
try this one out !!
Currently I am using file_get_contents to get contents of an HTML page I have.
if(file_exists($fileName)) {
echo "file exists!";
$current = file_get_contents("./docs/page1.html");
echo $current;
} else {
echo "file doesn't exist";
file_put_content($fileName, "testing creating a new page");
}
when the HTML file exists, I store the content using file_get_contents in variable $current and then echo current. However, nothing displays. Just a black page (which I'm guessing is the css in the html page)
If I can't spit out the page, is there a way to extract each element of the html page (divs, etc)?
file_put_content is not a function. Try file_put_contents
Also look at the changes i did to your script. It will give you information if the file can be created/read or not.
<?php
$fileName = './docs/page1.html';
if($current = file_get_contents($fileName)){
echo "file exists!";
echo $current;
} else {
echo "file doesn't exist";
if (file_put_contents($fileName, "testing creating a new page")){
echo 'created a new file!';
} else {
echo 'Error, couldnt create file! Maybe I dont have write permissions?';
}
}
?>
I tested this script, and it works unless it doesnt have file permissions. So make sure you set file permissions on the folder containing the file as well.
You are showing the page, but are probably lacking content on the page that uses relative paths, such as CSS and images.
If you wish to only use part of the page, I suggest looking into DOMDocument.
http://www.php.net/manual/en/domdocument.loadhtml.php
Is there any way to check if an included document via include('to_include.php') has returned anything?
This is how it looks:
//to_include.php
echo function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
include('to_include.php');
if($the_return_of_the_include != '') {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}
So after I've included to_include.php in my main document I would like to check if anything was generated by the included document.
I know the obvious solution would be to just use function_that_generates_some_html_sometimes_but_not_all_the_times() in the main_document.php, but that's not possible in my current setup.
make function_that_generates_some_html_sometimes_but_not_all_the_times() return something when it outputs something and set a variable:
//to_include.php
$ok=function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
$ok='';
include('to_include.php');
if($ok != '') {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}
If you are talking about generated output you can use:
ob_start();
include "MY_FILEEEZZZ.php";
function_that_generates_html_in_include();
$string = ob_get_contents();
ob_clean();
if(!empty($string)) { // Or any other check
echo $some_crap_that_makes_my_life_difficult;
}
Might have to tweak the ob_ calls... I think that's right from memory, but memory is that of a goldfish.
You could also just set the contents of variable like $GLOBALS['done'] = true; in the include file when it generates something and check for that in your main code.
Given the wording of the question, it sounds as if you want this:
//to_include.php
return function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
$the_return_of_the_include = include 'to_include.php';
if (empty($the_return_of_the_include)) {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
} else {
echo $the_return_of_the_include;
}
Which should work in your situation. That way you don't have to worry about output buffering, variable creep, etc.
I'm not sure if I'm missing the point of the question but ....
function_exists();
Will return true if the function is defined.
include()
returns true if the file is inclued.
so wrap either or both in an if() and you're good to go, unless I got wrong end of the stick
if(include('file.php') && function_exists(my_function))
{
// wee
}
try
// to_include.php
$returnvalue = function_that_generates_some_html_sometimes_but_not_all_the_times();
echo $returnvalue;
//main_document.php
include('to_include.php');
if ( $returnvalue != '' ){
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}