I'm trying to get a few PDF files I need for a project from a website (legally), but am running into some issues.
The URL of the location of the PDF file is f.e. example.com/?download_id=290758&s=d12134cac7ddb2198d232bba75c07d57&t=2-1-2014%2016:19:00
So first of all I parse this URL from the page containing it with the following code:
$html = file_get_html('http://www.example.com/Acer-CR-6530/manual-5-100076.html');
// find the download link containing the session ID
foreach($html->find('a[rel=nofollow]') as $e)
$link = $e->href;
$link = $baseURL . $link;
// Link: example.com/?download_id=290758&s=d12134cac7ddb2198d232bba75c07d57&t=2-1-2014%2016:19:00
echo file_get_contents($link);
This doesn't work. It doesn't output the PDF file. Should I use cURL? If so, I'm not a cURL pro so I would love to see code for doing this.
Thanks in advance!
Related
I have to download an image in a weblink to my local folder using php. Below is the program.
<?php
if (isset ($_POST['submit'])){
$URL = 'http://10.251.13.7/gtz/temp3.php/download.png'; // Like "http:// ...."
$FileToSave = 'uploads/download.png'; // Like "/home/.." or "C:/..."
$Content = file_get_contents($URL);
file_put_contents($FileToSave, $Content); //file_put_contents('uploads/image.jpg',file_get_contents('10.251.13.7/gtz/temp3.php/mtps_files.png'));
} ?>
I could successfully download the image. But when I try to open it's showing the file has been damaged. In one way I am able to right click the image in the weblink and save the file to my local directory. It works.
On the other hand, in php programming I was able to download the file and when I access the file, it is showing as file got damaged.
Please review below code. Please assign the home folder path in file & save file.
$URL = 'C:/home/demo/Downloads.png'; // Like "http:// ...."
$FileToSave = 'D:/demo/download.png'; // Like "/home/.." or "C:/..."
$Content = file_get_contents($URL);
file_put_contents($FileToSave, $Content);
All of this stuff is for example (names aren't actual).
Everything is also located on localhost:8080 (USBWebserver 8.5)
Directory Structure:
(Files located on localhost:8080/[project_name])
/ajax
/ajax_file.php
/img
/250x250
/[image_name].jpg
Code (From ajax_file.php):
$url = 'img/250x250/'.$image_name.'.jpg';
$url = file_exists($url);
This will return false.
I've tried an img_exists($url) function which used cUrl that did not work.
I've also tried:
$url = 'img/250x250/'.$image_name.'.jpg';
$image_check = getimagesize($url);
if (!is_array($image_check))
{
$url = 'img/default_image.png';
}
but this returns a warning for getimagesize() saying no file or directory exists.
When I put $url = 'img/250x250/'.$image_name.'.jpg' into <img src="$url" /> the image shows up...but if the image does not exist then it comes up with a broken image...
How come anything I try to do fails in some way?
I want a default image to show up when the image is broken :/
EDIT
$url = 'img/products/250x250/'.$image_name.'.jpg';
$url = var_dump(file_exists($url));
Returns bool(false)
$url = '../img/products/250x250/'.$image_name.'.jpg';
$url = var_dump(file_exists($url));
Returns bool(false)
It appears as if you need to branch out of the ajax folder before accessing img folder?
Try:
$url = '../img/250x250/'.$image_name.'.jpg';
#Alex Lunix
My guess is that he put the img tag inside of the actual php page, not the ajax script.
If you're in /ajax/ajax_file.php and you look for 'img/250x250/'.$image_name.'.jpg' it will be looking for /ajax/img/250x250/'.$image_name.'.jpg. Instead you should be using
$url = '../img/250x250/'.$image_name.'.jpg';
Although I'm not sure why it shows up in image tags, my guess is you're getting lucky and your browser is fixing the url.
How do I find the filename of an image on a MediaWiki site?
I don't want to put the filename in manually. I need PHP code which will fetch me the filename.
I can use $f = wfFindFile( '$filename' ); but HOW DO I GET $filename?
I've been looking at the FILE class but I can't figure out how to use File::getFilename(); I keep getting an error call to undefined method.
What am I doing wrong?
Explaining in more detail:
I would like to add the pin it button to my site so when you click on the button it post it on the pin it board with the image and description of the image. I need to use php to send the image information so it works on every page on my site. I can't code the image name manually each time.
So far I have the code:
<img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" />
Which works great except I need to put in a value for $f (image name). My question is how do I get the value of $f without having to put in in eg $f = wfFindFile( 'Sunset.jpg' );
I would have thought this would be a really common request for anyone trying to add pinterest to their site.
Thanks
The $filename you are looking for is basically how it is named in MediaWiki when it got uploaded, for example Landscape-plain.jpg. You will just use the wfFindFile() helper function to get a File object. Then call the methods:
$ php maintenance/eval.php
> $file = wfFindFile( 'Landscape-plain.jpg' );
> print $file->getName();
Landscape-plain.jpg
> print $file->getPath();
mwstore://local-backend/local-public/b/b0/Landscape-plain.jpg
> print $file->getFullPath();
/path/to/images/b/b0/Landscape-plain.jpg
> print $file->getTitle();
File:Landscape-plain.jpg
> exit
API documentation:
http://svn.wikimedia.org/doc/classFile.html
http://svn.wikimedia.org/doc/classLocalFile.html
EDIT BELOW
The file informations are available through a File object, so you definitely need to use wfFindFile() to get such an object.
To actually find the filename for the page the user is browsing on, you want to use the query context and get its title:
$context = RequestContext::getMain();
$t = $context->getTitle();
if( $title->getNamespace == 'NS_FILE' ) {
$filename = $title->getPrefixedText;
// do your stuff.
}
Hy!
I parse a website with simplehtml dom to get all links from the pictures.
The problem is that the link is like "/pics/bla.jpg".
I have the full path from the website like "http://xxx.xxx/blob/gulsch".
Now i want to get the full image link from the image (link root + /pics/bla.jpg) (no concat)
like: http://xxx.xxx/pics/bla.jpg
This should work for many websites
I tried it with explode()
$root = explode("/", $link);
echo $root[2];
I never get it working.
Please help.
Try with parse_url:
$r = parse_url($websiteUrl);
$imageUrl = $r["scheme"] . "://" . $r["host"] . "/" . $imageRelativeUrl;
The "root" of the website is simply $r["host"].
Given the parts of a URL, you can build a full URL with http_build_url.
I want to be able to open the provided URL (which is done via a form) that is an URL that will allow the server to save the file into a directory, for example:
http://www.google.co.uk/intl/en_com/images/srpr/logo1w.png
I want to save that logo into this directory:
img/logos/
Then it will add it to the database by giving it a random file name before so, e.g.
827489734.png
It will now be inserted to the database with the following:
img/logos/827489734.png
I do not want to use cURL for this, I like to work with fopen, file_get_contents, etc...
Cheers.
EDIT
$logo = safeInput($_POST['logo']);
if(filter_var($avatar, FILTER_VALIDATE_URL))
{
$get_logo = file_get_contents($logo);
$logo_directory = 'img/logos/';
$save_logo = file_put_contents($logo_directory, $logo);
if($save_logo)
{
$logo_path = $logo_directory . $save_logo;
A part of this code I need helping...
You need to specify a full file name when doing a file_put_contents(). A pure directory name won't cut it.