This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 7 years ago.
I would like to implement a PHP web page that, given a certain URL, is going to sniff some images from that page.
Do to so, I need :
1) to access the html source-code of that page and find out the URLs of the images I want.
2) to download these images on my FTP
I don't know how to do these two tasks, I guess I will have to use third party libraries, but this is the first time I need to do so and I am not sure.
Any advices ?
Thank you.
This is actually quite a simple task in PHP:
Use file_get_contents() to fetch the HTML from any random page (cURL would work too).
Using DOMDocument, find all img tags inside the page (see getElementsByTagName() method)
Extract the src attribute from each node.
Download the images somewhere with cURL.
Use the FTP functions to upload them to your server (or use a library like this one).
Did you mean grab html page's images and download them and then upload these images to your own FTP server?
If I catch on you correctly, this task will be completed by plain PHP code, no need of third party libraries.
Use preg_match_all to match all images out
Download them(use curl or file_get_content)
upload them to your FTP server, you can simply use curl again, put that image you downloaded above(you don't need save image in your PHP located machine, it is a File Stream in memory) in POST request body then send the request. Or upload image using FTP related functions(find that function in PHP net Document)
You need do step 2 and 3 in a loop basing on data generated from step 1.
Tell me if you need help about them.
Related
This question already has answers here:
PHP Parallel curl requests
(3 answers)
php get all the images from url which width and height >=200 more quicker
(4 answers)
Closed 9 years ago.
Im working on script that integrates online shops.
I have code like this (simplified):
// $asImageurls - array of string with image url's
foreach($asImageurls as $sImageUrl)
{
$imageContent = #file_get_contents($image);
// create filename, save image etc.
}
Connecting with remote server, downloading image takes a lot of time, and this is not good when I have like 500 products to import.
I was thinking about some parallel downloading, but I don't know how to start.
What can I do, to make it faster?
There are two main solutions to this problem:
1) Instead of downloading your images directly, store all urls in a file (also eventually the destination path). Then, use cron to call a script every n minutes that will do the downloads for you. This is the best way to avoid server overload if a lot of people submit downloads at the same time.
2) Use the exec() PHP function. This way you could call every system command you want. Typically curl in your case. This way you can add & at the end to throw it in background. You can even store warnings and errors redirecting them to a file.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to check file types of uploaded files in PHP?
Creating a text file and rename it to anything.jpg and try uploading it on facebook, facebook detects that the file is not an image and says Please select an image file or something like that. How do they do it?
I tested it out on my localhost by creating a dummy html form along with a <input type="file"... element and uploaded an image file created by renaming a text file to something.jpg and the file type in $_FILES['control_name']['type'] showed image/jpeg... How do I block users from uploading such 'fake' images. I think restriction using $_FILES['control_name']['type'] is not a solution, right?
When you process image on server, use image manipulation library (getimagesize for example) to detect it's width and height. When this fails, reject the image. You will probably do it anyway to generate thumbnail, so it is like one extra if.
There are many ways of checking the actual files. How Facebook does it, only the ones who created it know i think :).
Most likely they will look at the first bytes in the file. All files have certain bytes describing what they truely are. For this however you need loads of time/money creating a database or such against which you can validate the uploads.
More common solutions are;
FORM attribute
In a lot of browsers, of course excluding Internet Explorer, you can set an accept attribute which checks on extensions client side. More info here: File input 'accept' attribute - is it useful?
Extension
This is not realy secure, for a script can be saved with an image extension
Read file MIME TYPE
This is a solution like you stated in your question. This however is also easy to bypass and relies on the up-to-date status of your server.
Processing the image
The most reliable (for most developer skills and available time) would be to process the image as a test.
Put it in a library like GD or Imagic. They will raise errors when an image is not realy an image. This however will require you to keep that software up to date.
In short, there is not a 100% guarantee to catch this without spending tons of hours. Even then you only get 99,9%. You should weigh your available time against the above options and choose which best suits you. As best practice i recommend a combination of all 3.
This topic is also discussed in Security: How to validate image file uploads?
Headers in your file won't be the same.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to stream a WAV file?
I am developing a website where users can compose their own music, and the site will generate a .wav file for their creation. This is working correctly (inasmuch as I can play it on the page). However, I would like to save this file to the server to be listened to/downloaded at a later time, and the saved version of the file can no longer be opened and played by the HTML audio tag.
What, if anything, must I put into the file besides the file besides the raw data? Instead of setting the src attribute of the audio tag to the location of the file, will I actually need to open it and generate a URI?
At the moment, what I'm doing to play the wav file looks like this:
wav = [headerChunk, fmtChunk, dataChunk].join('');
var URI = "data:audio/wav;base64," + escape(btoa(wav));
document.getElementById("player").setAttribute("src", URI);
To save this, I'm just writing 'wav' directly into a file; I then try to play it back by setting the audio tag's src to be the location of that file on the server.
I hate to be the bearer of bad news, in an ideal world you will need to post the file with JQuery, but I don't think you have the file as you think you do, (you may do, but it's not really likely, there are too many security walls around things) it's much more likely that you a) have a Java Object with the file in it, or b) (much more likely) a flash object with the music in it.
In which case it's this "Box-of-tricks" that needs to be told to post it: i.e. if you are in browser client with a music file as a physical file then you probably downloaded it, if you edited/created it in the browser somehow you probably have a flash object with it in memory within -- in which case getting flash to post it is your only way
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to save webpage as a image file using PHP?
I would like to create an image on the server using a url string that is given.
I want a screen shot of a website to be created.
Is it possible with PHP GD , if so how ?
Thanks
You need something like PhantomJS to do it. This can be called from PHP.
Pretty sure GD can't handle this by itself. You'd need an HTML renderer too.
As far as I know, there is no existing PHP-Lib, which can do this. PHP-GD has to do with graphics, but you cant do this with it. Even if: What will happen, if the Site uses Javascript to enable certain important parts of a site? Or even images or other resources, that require long running requests? Or consider sites using flash... There are some tools for linux to take screenshots in batch - configurable with timer etc.
i have an url="http://some url";
Is it possible to create an image of the url using php?
I tried using imagecreatefromjpeg but it accepts only image file as the input and not the url like "http://"
I'm not sure what you mean - do you mean create an image of the page
itself? Then yes, it's possible. All you need to do is parse the html,
fetch any css and parse it, add in images and process any javascript in
the page.
Of course, it might take you a few years to build such an application,
but it can be done. And at the end you'll have a browser written in
php, which will be quite slow.
If you need to take a snapshot of a web url using just php you need an external tool like cutycapt.
It is quite invasive for a server (you need an X environment) but it the easiest solution to go at present.
If you want to create an image with your url text in it then use something like, imagefttext, for more visit php.net/imagefttext, in case you want to take a screenshot of the webpage at given url, go to this SO link: Command line program to create website screenshots (on Linux)