I have a question would like to ask you, it was about php. My problem when I tried to use php function to extend image extension from an url which has form below :
http://lh3.googleusercontent.com/i_qpu5lXHddZgNaEbzEEz1CaArLCHEmVNuhwVOuDUl0aIyZHuez3s4Uf878y1n9CqB5rld2a7GSAoWzoMgrC
so , for above url is made by Google which is not show use the file name and extesion name. of course I have try to use this below function but still not work :
$image_name = basename($url);
could anyone help me.
If you are downloading the image, you can get the extension using finfo_file().
Else you can look for the content type in the headers sent by the server using get_headers()
example code
<?php
$url = 'http://lh3.googleusercontent.com/i_qpu5lXHddZgNaEbzEEz1CaArLCHEmVNuhwVOuDUl0aIyZHuez3s4Uf878y1n9CqB5rld2a7GSAoWzoMgrC';
print_r(get_headers($url));
?>
sample output
Array
(
[0] => HTTP/1.1 200 OK
[1] => Access-Control-Allow-Origin: *
[2] => ETag: "v1"
[3] => Expires: Wed, 22 Apr 2015 09:10:30 GMT
[4] => Cache-Control: public, max-age=86400, no-transform
[5] => Content-Disposition: inline;filename="unnamed.png"
[6] => Content-Type: image/png
[7] => Date: Tue, 21 Apr 2015 09:10:30 GMT
[8] => Server: fife
[9] => Content-Length: 20365
[10] => X-XSS-Protection: 1; mode=block
[11] => Alternate-Protocol: 80:quic,p=1
)
Related
I am making a links-checker tool to avoid broken links in our site content and it works when the page doesn't exist or can't be loaded - except when the external site replaces it with a screen saying something like 'This page doesn't seem to exist. Search for the content you are looking for from our menu...'.
Apart from the html/css/js code for this tool, here is the main PHP code that checks the links
$headers = get_headers($url);
$headers = (is_array($headers)) ? implode( "\n ", $headers) : $headers;
$exists = (bool)preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers);
$status = (is_array($headers)) ? $headers[0] : $headers;
Then js use this information including $status but it's not returning error code when the external site shows a 'not found' screen (e.g. http://www.drdansiegel.com/resources/healthy_mind_platter).
You get back redirects before resolving to the 404. I would invert your logic, check to see if you have a 404 or 400 ever present.
$notexists = (bool)preg_match('#^HTTP/.*\s40[04]\s#mi', $headers);
Also, you should use the m modifier so the leading anchor matches each line, not the whole string.
Additionally, note a character class is a list of characters, you can't do groupings in it as you have. [(200|301|302)] says a (, 2, 0, 0 (again), |, 3, 0 (again), etc. are all allowed. You would write that as (200|301|302) if you wanted 200, 301, or 302 to be allowed characters. You could use a character class for the last integer on the redirect status code (and should add 7 and 8 to that as though are valid redirects as well). So it could be (200|30[1278]).
Here's what your $headers contained from example link:
Array
(
[0] => HTTP/1.1 301 Moved Permanently
[1] => Server: nginx
[2] => Date: Sat, 15 May 2021 01:57:44 GMT
[3] => Content-Type: text/html
[4] => Content-Length: 162
[5] => Connection: close
[6] => Location: https://www.drdansiegel.com/resources/healthy_mind_platter
[7] => HTTP/1.1 301 Moved Permanently
[8] => Server: nginx
[9] => Date: Sat, 15 May 2021 01:57:45 GMT
[10] => Content-Type: text/html; charset=UTF-8
[11] => Content-Length: 0
[12] => Connection: close
[13] => Expires: Sat, 15 May 2021 02:57:45 GMT
[14] => Cache-Control: max-age=3600
[15] => X-Redirect-By: WordPress
[16] => Location: https://drdansiegel.com/resources/healthy_mind_platter
[17] => HTTP/1.1 404 Not Found
[18] => Server: nginx
[19] => Date: Sat, 15 May 2021 01:57:46 GMT
[20] => Content-Type: text/html; charset=UTF-8
[21] => Connection: close
[22] => Vary: Accept-Encoding
[23] => Expires: Wed, 11 Jan 1984 05:00:00 GMT
[24] => Cache-Control: no-cache, must-revalidate, max-age=0
[25] => Link: <https://drdansiegel.com/wp-json/>; rel="https://api.w.org/"
)
When I use the function get_headers($url) where $url = "https://www.example.com/product.php?id=15" on my live site then it is not returning any array from given url. I get nothing. But when the same code is used on my localhost, I get following:
Array
(
[0] => HTTP/1.1 200 OK
[1] => Cache-Control: private
[2] => Content-Type: text/html; charset=utf-8
[3] => Server: Microsoft-IIS/8.5
[4] => Set-Cookie: ASP.NET_SessionId=wumg0dyscw3c4pmaliwehwew; path=/; HttpOnly
[5] => X-AspNetMvc-Version: 4.0
[6] => X-AspNet-Version: 4.0.30319
[7] => X-Powered-By: ASP.NET
[8] => Date: Fri, 18 Aug 2017 13:06:18 GMT
[9] => Connection: close
[10] => Content-Length: 73867
)
So, why the function is not working successfully on live?
EDIT
<?php
if(isset($_POST['prdurl']))
{
$url = $_POST['prdurl'];
print_r(get_headers($url)); // not getting any array on live but on localhost
if(is_array(#get_headers($url)))
{
// some code goes here...
}
else
{
echo "URL doesn't exist!"
}
}
?>
One more thing to note down here is that I'm using file_get_html to retrieve the html page from the remote url. It's working on my localhost but not on live as well.
I'm trying to download a zip from an URL, with something like this:
$filename = "path/to/File.zip";
file_put_contents($filename, file_get_contents('http://example.com/Download.zip');
My problem is that I want to store that file with the filename from the Content-Disposition header, not the placeholder latest-stable. When I predefine the path it keeps that new name.
If your try to downloading following in your browser:
http://downloads.wordpress.org/plugin/jetpack.latest-stable.zip
The download dialog will reveal jetpack.3.1.1.zip, which is the filename I want, not jetpack.latest-stable.zip.
You have the filename already (Download.zip) so you can just:
$url = 'http://example.com/Download.zip';
$filename = basename($url);
If you have more complex URLs you can use parse_url to get the various URL components.
Edit
From the comments you provided a URL that results in a different filename. In that case you can inspect the $http_response_header array, which will be populated with the headers after the call to file_get_contents:
$data = file_get_contents('http://downloads.wordpress.org/plugin/jetpack.latest-stable.zip');
print_r($http_response_header);
/* output:
Array
(
[0] => HTTP/1.1 200 OK
[1] => Server: nginx
[2] => Date: Thu, 11 Sep 2014 04:11:03 GMT
[3] => Content-Type: application/zip
[4] => Content-Length: 7303566
[5] => Connection: close
[6] => Cache-control: private
[7] => Content-Disposition: attachment; filename=jetpack.3.1.1.zip
[8] => Last-Modified: Wed, 10 Sep 2014 16:17:52 GMT
[9] => X-Frame-Options: SAMEORIGIN
[10] => X-nc: EXPIRED lax 202
[11] => Accept-Ranges: bytes
)
*/
Look for the Content-Disposition header and strip out the filename (you can use regex and/or http_parse_headers for this).
I am learning PHP and I'm trying to understand the array below,
I need to grab content-type, it should give me
text/xml;charset=UTF-8 and then
grab the encoding which is UTF-8
Anyone good with arrays could you please help?
array(
[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 30 Aug 2014 17:10:12 GMT
[2] => Server: Apache
[3] => X-Gas_TTL: 900
[4] => Cache-Control: max-age=900
[5] => X-GasHost: gas1.usw
[6] => X-Cooking-With: Gasoline-Local
[7] => X-Gasoline-Age: 844
[8] => Last-Modified: Sat, 30 Aug 2014 16:56:08 GMT
[9] => Content-Type: text/xml;charset=UTF-8
[10] => Vary: Accept-Encoding
)
Thank you in advance
Regards,
Mona
.*?Content-Type:\s*(.*?=(.*?))\n.*
With s flag on this will give what's required.
See demo.
http://regex101.com/r/bJ6rZ5/1
The array must be in a variable ( I assume, since you seem to have printed it ), so you simply print/echo the value you're after.
echo $someArrayVariable[9];
Where someArrayVariable is a variable that contains the array.
That is assuming it is in a variable in the first place.
I need to check if a URL returns a PDF document using PHP. Right now I'm using the file_get_mimetype function. A normal URL(https://www.google.com/) returns type as application/octet-stream while a normal pdf link (http://www.brainlens.org/content/newsletters/Spring%202013.pdf) returns application/pdf. But now I also encounter URL's like http://www.dadsgarage.com/~/media/Files/example.ashx or http://www.wpdn.org/webfm_send/241 which also is pdf but returns application/octet-stream. There are other URL's too which opens a Save as dialog box which also has to be detected.
Use get_headers()
Eg:
$url = "http://www.dadsgarage.com/~/media/Files/example.ashx";
if (in_array("Content-Type: application/pdf", get_headers($url))) {
echo "URL returns PDF";
}
print_r(get_headers($url));
returns
Array
(
[0] => HTTP/1.1 200 OK
[1] => Cache-Control: private, max-age=604800
[2] => Content-Length: 194007
[3] => Content-Type: application/pdf
[4] => Expires: Tue, 09 Dec 2014 09:40:20 GMT
[5] => Last-Modified: Wed, 07 Aug 2013 16:46:30 GMT
[6] => Accept-Ranges: bytes
[7] => Server: Microsoft-IIS/8.0
[8] => Content-Disposition: inline; filename="example.pdf"
[9] => X-AspNet-Version: 4.0.30319
[10] => X-Powered-By: ASP.NET
[11] => X-Provider: AWS
[12] => Date: Tue, 02 Dec 2014 09:40:20 GMT
[13] => Connection: close
)
mime types could include:
application/pdf, application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf