How to copy an external image to local? - php

I want to retrive an http external image an copy it to a folder on my server (eg. /images/example.jpg)
Is there a way to do this with symfony 2.0 or with basic PHP 5 functions?

you can do it like this:
file_put_contents('tmp.jpg', file_get_contents('http://static.adzerk.net/Advertisers/3603.jpg'));
But, be careful, if it's a web page, it maybe very slow, depending on the speed of image page.

Related

Store the number of views of an image

I have a folder on my web server containing lots of PNG images. I would like to store in a database the number of views each image gets. I do not really know how to achieve this. I can only think of using a .htaccess file to rewrite urls in that folder to a php script that would serve those images and also store the visit on MySQL. But maybe there is a better way than serving all the images through a PHP script. I am looking for the simplest possible way to do this. And also, current urls should not be changed.
There isn't a single "good" way to do this. But I cat give you few ideas.
(recommended) Make a proxy PHP script which will take ID (could be file name) of the picture. It will increment the counter in the DB and then redirect to original image.
This is very clear way to achieve expected behaviour. Disadvantage is it changes current filenames. So you have to use following schema
CURRENT_URL -> (mod_rewrite) -> PHP PROXY SCRIPT -> NEW_URL
Redirect all image urls to PHP proxy script which will directly server image (set appropriate headers and output content of file).
This is 100% transparent way to do this but obvious disadvantage is that every image is processed by PHP. So when you have heavy-loaded server this could be problem.
If you have access to logs of web server (access.log for Apache) you can process this file say once a day and update counters in DB. This is very good when approximation is good enough and your server is heavy-loaded because you can parse logs on different machine.
Apache already keeps a log file of all requests (/var/log/apache2/access.log). This file contains the URL requested, time of request etc.
A possible approach could be:
Create a script which parses the access log and updates the database
Configure a cronjob which invokes the script periodically

Application Cache for Dynamic Sites

I am making a php mysql web app, my idea is to install in the customer home a web server so they can connect with whatever device they want, probably most of the time they will be using an ipad to connect to the app.
Sometimes the client needs to take out the app with them in the ipad, so after discarding other options(like phonegapp because i need to mantain a mysql db for some functions)i realized that Application Cache may be a good solution: They can use the application with the web server(using db functions like randomize the content, generate Statistics)and when they are offline they can access a local copy of the content, with limited function but working.
The problems that i have is that the site have images,video and audio so at least there are 20mb to cache, i read that with application cache you can only store 5mb and the other problem is that my content is dynamic so i cant add all the files that i need to the cache manifest. I want something like make a wget of the site(save an static html file) and to use the dynamic content when online. I dont know if i can make something like that.
Thanks
the cache.manifest for the ipad can store more than 5mb.
the currently ios limit is 50mb.
if you cache more files, automatically the ipad ask if you want increase the storeage to 50mb.
take a look at this
it explains you how to create and implement the cache.manifest. its a great tutorial
hope this help.

Yii Best way to serve dummy RSS in Test

I have a PHP Yii application that uses an RSS feed reader. I wanted to develop some good tests, and I wanted to attempt to read an RSS feed under my own control as part of my test suite. The idea is I request this feed from "my.localhost/testfeed/{name}" and my application is served locally at "my.localhost"
I made a controller (TestFeedController) which uses the CViewAction to serve static rss files (stored in .php files). I had to put these in "protected/views/testFeed/pages/" to make it work.
I wanted to store the files in "protected/tests/views/testFeed/pages/" to separate them better from actual application code, but was unable to get this to work (I tried to overload getViewPath()). Is there a way to get a view file in CViewAction that is NOT in "protected/views/*"?
Is there a better way to test reading a "remote" RSS file which is under my local control? I considered serving it on another virtual host, but I wanted to keep the project tests with the project.
You could use RenderInternal for your test files and pass it an absolute filepath (e.g., dirname(FILE).'/../tests/views/..etc..'

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 copy the file from one folder to another using js

i am facing the problem in coping the images from one folder to another . It is possible through JS means please guide me, i had the image path (eg : C:\Program Files\xampp\htdocs\gallary\images\addnew.gif ) just i want to copy the images to another folder using js .thanks in advance.
You cannot use javascript to do this within the web browser. Javascript can only execute code in the browser of the person viewing the web page, not on the web server. Even then, javascript is "sandboxed" for security so it cannot access the users files, etc. Imagine the privacy problems if every webpage you visited had access to your My Documents folder!
PHP, however, can do this on the web server (I assume you have PHP instaled because you have XAMPP in the path to your image). The relevant PHP function is copy:
bool copy ( string $source , string $dest [, resource $context ] )
In your case, you probably want to call it like this:
success = copy('C:\\Program Files\\xampp\\htdocs\\gallary\\images\\addnew.gif', 'C:\\images\\addnew.gif')
if (!success){
echo "Could not copy!"
}
The simplest way to trigger this file copy is when a PHP web page is loaded. However, if you want to trigger this file-copy via javascript, you might want to look into using an AJAX style technique, where a javascript event sends an HTTP request to your web-server in the background. The web-server can then do the file copy in PHP. If you do take this approach, I would recommend that you:
Use a javascript API like jQuery which has built in functions to make this easier.
Be very very careful about security. You don't want someone snooping around on your website to be able to delete or copy arbitrary files.
You could use MS JScript http://msdn.microsoft.com/en-us/library/e1wf9e7w(VS.85).aspx
fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CopyFile ("c:\\mydocuments\\letters\\*.doc", "c:\\tempfolder\\")
this can't be done from a browser, but you can run it in windows (using the windows script host) directly. You could also do it with node.js (server side javascript) which would be a more cross platform way. If you're trying to do it from in the browser on the client side it is not possible from any language for obvious security reasons.

Categories