PHP getimagesize() alternatives without javascript? - php

Say I have the URL of an image, and URL file-access is disabled in the server configuration, and that's not something I can (or want) to change, is there an easy way to get it's height/width using PHP?
I'd prefer not to use javascript if possible.

umm, so you're going to pull the image over the network into your PHP application "find" the size, and then presumably emit an HTML img take with the size? seems kind of funky to me.

This doesn't really make much sense since if you can view the image on a web page, the image should be directly accessible via the URL. Why, in other words, would you have a url to an image that can't be accessed via url?
Regardless, you can use any number of http/url libraries to set a user-agent in the header of the request. Essentially, you trick the server into believing that a browser is accessing the file, rather than an anonymous service.
Here is a list of browser user agents you can use for this purpose.

In order to examine a remote image, you're going to have to request it somehow. You can't determine properties of something you can't see. If remote file access is disabled for your PHP installation, you might have to farm this out to a system level process, such as wget, cURL, or ftp.
Odds are if your host has disabled remote file access, they've probably disabled shell access too, but as an example, you could do something like:
exec("wget ".$url);
Another option might be to use the built-in cURL package, if by some odd configuration it's installed and active despite not having remote file access.
If you can figure out a way to download the image from the remote server, you can use getimagesize() on it.

Related

How can I serve image files with PHP with the same speed as if the image was linked directly?

This is what I need to achieve:
Request: http://www.example.com/image5.jpg
Should rewrite, for example:
RewriteRule ([^.]+)\.jpg$ /image.php?image=$1
Now, in my image.php, how do I serve the image, if I know where it is?
For example:
<?php
$path = '/images/5/2/3/1/small/latest/'.$_GET['image'].'.jpg';
?>
What is the best way to handle this request, so that it behaves like a image file (sends an image header) and displays the image?
There are various alternatives around the net, mainly X-Sendfile and readfile(), but I'm not sure what is the optimal solution and why.
Assuming web server can still serve the images directly via full path, I would just redirect directly to image:
header("Location: /images/5/2/3/1/small/latest/{$_GET['image']}.jpg");
exit;
The exit is good to have after sending headers. Also, make sure you do not send any other output, as header will not work then.
The fastest way to do this is to leverage the operating systems zero-copy support, whereas the file is sent by telling the network driver to move data directly from the OS file system buffer and onto the network. This way, no RAM coping is necessary, and the bottleneck will be in your network bandwidth.
I can't find any mention of PHP supporting this, however, which means that if you must serve the file with PHP, you will have to copy the file from the OS file cache into a PHPs memory space, and then have PHP ask the driver to copy it.
I would assume the built in method for doing that will be the most performant way to do it: http://php.net/manual/en/function.http-send-file.php

forcing browser to cache images in php website

I've a php based website and would like browser to cache the images for 30 days .. i am using a shared hosting solution where I do not have access to apache config to enable mod-headers or other modules and so can not use htaccess mechanisms for this.
my site is a regular php app, and has both html contents and images. I would like browser to cache images only. I've seen php's "header" function, but couldn't find a way to force only image cache .. How do i go about it ?
Thanks
As far as I know, if you can't get access to Apache to set the headers, your only other option is to serve images from a PHP script so you can use the PHP Header methods to set the headers.
In this case, you'd need to write a PHP image handler, and replace all your image tags with calls to this handler (e.g. http://mysite.com/imagehandler.php?image=logo.png). You would then have you imagehandler.php script retrieve the image from the file system, set the mime type and cache control headers, and stream the image back to the client.
You could write your own, or if you google, you will find image handler PHP scripts. Either way, make sure you focus on security - don't allow the client to retrieve arbitrary files from your web server, because that would be a fairly major security hole....

how to upload a file from database without user interface in php

Help!!!
i want to upload a file from database(or from local m/c) to server in php without user interface. Is it possible.
Please Suggest.
Since you mention Ajax I assume you mean you want to upload it using JavaScript running in a web browser (and the server side component happens to use PHP).
This is impossible in the normal browser security context.
Users must explicitly select files to upload (otherwise webpages could go around stealing all sorts of private files behind users backs).
Users cannot do this without a user interface.
You could try curl from the command line:
curl --upload-file your.file.txt http://example.com/upload.php

How to setup PHP to be a "pipe" for downloads from another location?

I'm wanting to setup a php script and host it on my server that will let me download files from other locations, but making it look like it's coming from my server. Maybe using curl or htacess. Also I was hoping that there would be a way to get around having my server deal with the bandwidth. Does that make sense? Is this doable?
-- Update
Kind of like a proxy, but without the file downloading to memory and then sending it to the client.
You can do this by simply passing the target url to you script, open the url with file_get_contents(), curl or other file functions and echo the data. ensure to set the Content-Type header to "application/octet-stream" to force the browser to save the file instead of displaying it.
As for the bandwidth: You'll have to deal with it. If your server downloads a file, it will use up the bandwidth. It will even use it up twice because it has to receive AND send the data.
I don't know why you mention htaccess, because that has nothing to do with your problem.
Also I was hoping that there would be a way to get around having my server deal with the bandwidth. Is this doable?
No.
I'd recommend setting up a linking system on your site like http://example.com/download.php?id=12 that would then forward directly to the remote file, that way you'd save on bandwidth and if someone look at the link on your page it would look like it coming from your server. It would still show the other site in the download manager but if your trying to save bandwidth it's a small price to pay.
Thanks for the help... I figured out what I was needing to do, I'm going to use mod_xsendfile. It lets you set an external source for where the file is located, and then lets the user download the file without knowing where the file actually is located.

Easiest way to execute local file from Firefox?

I am developing a web application.
I would like to extend my error messages (and their backtraces) so that I can click on the mentioned file, and have it opened automatically in my PHP IDE.
I would like to make this a easy-to-activate feature so that whoever works on the web application, can easily map the error message to point to their local copy of the site, and open their IDE.
What - short of developing a custom FF extension - is the simplest way you can think of to execute a local command (a batch file that calls the IDE) on click in Firefox on Windows(7)?
I have looked for extensions but had no luck. Maybe using another extension like Firebug or Greasemonkey?
Security is not an issue, as this is supposed to work on the developer's workstation only and I can change my local Firefox's settings.
You can add a new protocol (like "edit://") to windows (http://msdn.microsoft.com/en-us/library/aa767914(VS.85).aspx) and write a small handler program that picks a filename from the "edit://" url and passes that file to the editor. This way i taught windows to understand txmt links (http://blog.macromates.com/2007/the-textmate-url-scheme/) in the way my mac does.
There's not a way to do this with javascript. But it looks possible with a firefox addon. Have a look at this.
http://mozex.mozdev.org/
MozEX is an extension which allows the user to use external programs for these actions:
* edit content of textareas (possibly utilizing a spell-checker, color syntax etc.)
* view page source
* handle mailto, news, telnet and FTP links
* download files
* ... and many more :)
The universal handler lets you enter a list of protocol schemes, e.g., "abc://,def://" and a
command to handle them. So you just have your application generate a url that begins with
your chosen (made up) protocol, and mozex will intercept a click on the url and send the
url to your chosen command as a paramater.
I think this is exactly what you want.
I think that the closest you can get to this, is by having the configuration of the web browser associate a particular mime type with a given "helper application" (here the IDE program), and to have the HTTP server return such a file.
Otherwise, security concerns dictate that browser would not run any "abritrary" program/logic on the client.
Pekka,
After reading the thread so far, it seems that you want to build an application that somehow authenticates with the server--i.e.: the "average user" wouldn't have access to it. If this were the case, then delivering it through the browser is an impossibility without writing a custom extension.
Running authentication through GreaseMonkey is difficult, but once the client is authenticated, there is no real way to "run" the trace.
If the server generates a batch file or some kind of instruction set (script, shortcut, etc.), you can simply configure the browser (or have the local instance of your app configure the browser) to run the file. The problem here is that you have no way to automatically authenticate!
The only other way I can imagine that you could get this to work is via a Java applet, which would only be cumbersome and require Java to initialize every time you wanted to import a trace.
The problem you have is that the browser is inherently secure. It's designed to protect the computer from malware, rogue websites, etc etc., and so without developing a custom extension for the browser, there's no way to make the hop to any applications that run in tandem with the browser.
So on that note, I'll suggest that you reconsider writing a Firefox XUL extension as mentioned above. You'll probably need to implement some XPCOM code to make it work, too. Here are some resources that will help get you started:
https://developer.mozilla.org/en/xpcom
https://developer.mozilla.org/En/XUL
http://ted.mielczarek.org/code/mozilla/extensiondev/
https://developer.mozilla.org/en/XUL_Tutorial/Introduction
I don't know which IDE you're using, but in for example Eclipse you can also use the built-in webbrowser to test your webapp and the exceptions/traces in the Eclipse console log already have links to the source code in question. Easy as that. See if your IDE provides something similar.

Categories