When I use to following PHP code;
<?php if (file_exists("/foto/Maurice.jpg"))
{
echo "<center><img src='/foto/Maurice.jpg'/></center>";
}
else {
echo "<center><img src='/afbeeldingen/kaars1.png'/></center>";
?>
My browser always shows kaars1.png
instead of Maurice.jpg
I also tried !file_exists but then it doesn't show kaars1.png, when Maurice.jpg doesn't exist.
Is there a simpel way to fix this?
file_exists is only for files on your server's (local) filesystem. You need to actually try to request the URL and see if it exists or not.
You can use cURL to do this.
$handle = curl_init('https://picathartes.com/foto/Maurice.jpg');
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_NOBODY, TRUE);
$response = curl_exec($handle);
// Check for 404 (file not found).
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
echo "<center><img src='https://picathartes.com/afbeeldingen/kaars1.png'/></center>";
}
else{
echo "<center><img src='https://picathartes.com/foto/Maurice.jpg'/></center>";
}
curl_close($handle);
(Code from this question: https://stackoverflow.com/a/408416)
This is an answer to your second question
The correct solution depends upon your actual directory structure and the location of the script file in relation to the actual folder and file you are looking for, but to start finding a solution the / in "/foto/Maurice.jpg" say go back to the root directory and look for a directory called /foto
So if this folder is under your DocumentRoot try using
if (file_exists("foto/Maurice.jpg"))
Related
I want to check my images on my server, and I have another subdomain to save the images but they are served with HTTPS
It only works locally, but not remotely by HTTPS, as it always prints "exists directory".
$foto = "https://subdomain.example.com/images/" . $var . "/" . $var2. "/flayer";
if (file_exists($foto . ".jpg")) {
echo "HELLO WORL";
}
if (glob($foto . ".*")) {
if (file_exists($foto . ".jpg")) {
$ruta = "https://subdomain.example.com/images/". $var . "/" . $var2 . "/flayer.jpg";
}else{
echo "no exists";
}
}else{
echo "no exists on directory";
}
glob() is a directory service. If it is a sub directory of your domain, then your file system should be stored on the same server. Inside your index folder, define or store a variable to this path - treat it like an enviromental variable.
define('PUBLIC_ROOT', dirname(__FILE__));
You can now loop through your directory at your sub domain. It will be a folder named what your sub domain is called.
foreach(glob(PUBLIC_ROOT . '/subdomain/images/*.jpg', GLOB_BRACE) as $image)
// Do something with each JPEG image
If for some reason, your sub domain is hosted else where - strange - or it is an external site and you need to use HTTP. Send a cURL request. You can specify nobody in the header.
$ch = curl_init('https://sub.example.com/images/foo.jpg');
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec ($ch);
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200)
// It didn't exist
curl_close($ch);
You cannot use a directory service (DS protocols) cross domain (over HTTP protocols).
Moving on point: If you need a collection of all of the images held on that server, then consider building an API that returns a JSON encoded array of all of the file paths or dynamic URI's on your sub domain, then request it from another domain.
i test with 2 options
function remote_file_exists($url, $ext) {
$ch = curl_init($url . $ext);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode == 200) {
return true;
}
}
AND
function remote($url, $ext) {
if (#exif_imagetype($url . $ext)) {
return true;
} else {
return false;
}
}
Works but is so slow, something to do it more fast?
I have this piece of code in PHP:
if (file_exists($_POST['current_folder'])) {
//do something
}
But file_exists always returns false. The value passed to the function is:
echo $_POST['current_folder']); //This prints: http://localhost/wordpress/wp-content/music
I also tried with different folders on the localhost. The function always returns false.
I also tried is_dir(). But even this function returns false with the above URL.
There are many related questions on Stack Overflow. But most of them suggest that file_exists only works with relative URLs. But from this link it is clear that http:// URLs are also supported by the file_exists function.
What am I missing?
Use directory path; not web URL:
<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
Tested under windows using Apache 2.4.9.
<?PHP
$crl = curl_init("http://localhost/symfony2/");
curl_setopt($crl, CURLOPT_NOBODY, true);
curl_exec($crl);
$ret = curl_getinfo($crl, CURLINFO_HTTP_CODE);
curl_close($crl);
if ($ret == 200)
echo 'File exists';
else
echo 'File does not exist';
?>
It works, just a note, it requires trailing slash for some reason.
Code 200 means OK (success).
I have a piece of a PHP code:
<?php
$image_cdn = "http://ddragon.leagueoflegends.com/cdn/4.2.6/img/champion/";
$championsJson = "http://ddragon.leagueoflegends.com/cdn/4.2.6/data/en_GB/champion.json";
$ch = curl_init();`
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $championsJson);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);
$json_array = json_decode($json, true);
$champions = $json_array["data"];
foreach ($champions as $championdata) {
$image_url = $image_cdn.$championdata["image"]["full"];
$image = file_get_contents($image_url);
file_put_contents("imgfolder/".$championdata["image"]["full"], $image);
}
?>
So the idea of the code is to basically to decode a JSON and to download the images from the following website:
http://gameinfo.na.leagueoflegends.com/en/game-info/champions/
The pictures download perfectly fine and are stored into a folder I have created on my hard drive. The next step is, is it possible for me to use this same piece of code, to download/display the images onto a website I have recently created:
www.lolguides4you.co.uk
the website is basically a day old and I have literally been messing around with some HTML coding. I am new to both HTML and PHP so if someone could point me into the right direction, that would be great!
Assuming that all you want to do it insert the images into a page on your website, then this is quite simple.
However, it may be illegal for you to use an automated scraping tool to save/replicate/duplicate any of the images. Look into that before doing anything on the internet.
The first step is to upload your previous PHP script to your website. That way, rather than downloading the images to your computer and then trying to upload them to your site it allows you to just save the files straight into a directory on the website.
Next, you can create a basic PHP page anywhere on your site:
<?php
echo "Images downloaded:\n";
?>
Next you can use PHP's scandir() function to find every file in the download directory:
<?php
echo "Images downloaded:\n";
$files = scandir('imgfolder/');
foreach($files as $file) {
// Do something
}
?>
Finally, now you just show each image:
<?php
echo "Images downloaded:\n";
$files = scandir('imgfolder/');
foreach($files as $file) {
echo $file . "\n";
}
?>
You could also use Glob, which allows you to ignore any files which aren't a certain type (in case you have other files in the same directory:
<?php
echo "Images downloaded:\n";
$files = glob("imgfolder/*.jpg");
foreach($files as $file){
echo $file . "\n";
}
?>
I am trying to check whether image file is exist on server Or not. I am getting image path with other server.
Please check below code which I've tried,
$urlCheck = getimagesize($resultUF['destination']);
if (!is_array($urlCheck)) {
$resultUF['destination'] = NULL;
}
But, it shows below warning
Warning: getimagesize(http://www.example.com/example.jpg) [function.getimagesize]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in
Is there any way to do so?
Thanks
$url = 'http://www.example.com/example.jpg)';
print_r(get_headers($url));
It will give an array. Now you can check the response to see if image exists or not
You need to check that the file is exist regularly on server or not.you should used:
is_file .For example
$url="http://www.example.com/example.jpg";
if(is_file($url))
{
echo "file exists on server";
}
else
{
echo "file not exists on server ";
}
Fastest & efficient Solution for broken or not found images link
i recommend you that don't use getimagesize() because it will 1st download image then it will check images size+if this will not image then it will throw exception so use below code
if(checkRemoteFile($imgurl))
{
//found url, its mean
echo "this is image";
}
function checkRemoteFile($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// don't download content
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(curl_exec($ch)!==FALSE)
{
return true;
}
else
{
return false;
}
}
Note:
this current code help you to identify broken or not found url image this will not help you to identify image type or headers
Use fopen function
if (#fopen($resultUF['destination'], "r")) {
echo "File Exist";
} else {
echo "File Not exist";
}
Issue is that the image may not exist or you don't have a direct permission for accessing the image, else you must be pointing an invalid location to the image.
you can use file_get_contents. This will cause php to issue a warning along side of returning false. You may need to handle such warning display to ensure the user interface doesn't get mixed up with it.
if (file_get_contents($url) === false) {
//image not foud
}
<?php
set_time_limit(0);
$url = 'http://www.some.url/file.pdf';
$path = 'files/file.pdf';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $data);
?>
This is the code that I use to download a particular pdf file froma given url.
What if there a many files in that url by names file1.pdf, file2.pdf etc. How can I check while running a loop, when to end the loop as the files will be present up to a limited number ?
Please help!
Checking for 404 code:
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
/* file NOT found */
}
Checking mime type:
$mimeType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
if($mimeType == 'application/pdf') {
/* It IS pdf file */
}
But note, that mime type can be other, but it'll be still PDF file! Also, check mime type by your PDF files: echo them to understand what u must look for. I'm not rly sure, that code in if statement is right (it is example only)
You can call curl_get_info() right after curl_exec().
pass image link inside file_get_contents();
then check using preg_match();
<?php $link = $image->img1;
$filecontent=file_get_contents($link);
if(preg_match("/^%PDF-1.5/", $filecontent)){
echo "Valid pdf";
}else{
echo "In Valid pdf";
}
?>
You can also check: to get last three character of your file
<?php
$img_type = substr($image->img1, -3); ?>
<?php if(preg_match("/^%PDF-1.5/", $filecontent) || $img_type == 'pdf' ){ }?>