php copy() not working with an specific url link - php

I want to copy an image from an another server, but it doesn't work and i don't know why. Here is my code:
if(copy('http://demo.swyp.fr/mod_traffiq/thumb/LQ1009C/LQ143559C/LQ157553C-71x100.jpg', 'zzz.jpg')) {
echo "Copy success!";
}else{
echo "Copy failed.";
}
It always returns failed.

You are using the copy function. Although this method works with both remote sources and destinations, you are encouraged to use the file_put/get_contents-methods (see documentation quote below).
$image = file_get_contents('http://demo.swyp.fr/mod_traffiq/thumb/LQ1009C/LQ143559C/LQ157553C-71x100.jpg');
file_put_contents('zzz.jpg', $image);
From the file_get_contents documentation:
file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.
Also note:
A URL can be used as a filename with this function if the fopen wrappers have been enabled.
However, if fopen remote URL is not turned on in your settings, you might test with cURL:
$ch = curl_init('http://demo.swyp.fr/mod_traffiq/thumb/LQ1009C/LQ143559C/LQ157553C-71x100.jpg');
$fp = fopen('zzz.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
There could be other PHP settings (or server configurations) which disables all of these code snippets from running. If this does not work, then it is a matter of configuration issues instead.

Related

Save mp3 file from a download link on your hosting space using PHP

I am fetching data from API of a service provider (Say- http://serviceprovider.com).
From several parameter one is MP3 download Link (example- http://serviceprovider.com/storage/read?uid=475b68f2-a31b-40f8-8dfc-5af791a4d5fa_1_r.mp3&ip=255.255.255.255&dir=recording)
When I put this download link on my browser it saves it to my local PC.
Now My Problem -
I want to save this MP3 file in one of folder on my hosting space, from where I can further use it for playing using JPlayer Audio.
I have tried file_get_contents(), but nothing happened.
Thanks in advance.
Edit:
After reading Ali Answer I tried the following code, But still not working fully.
// Open a file, to which contents should be written to.
$fp = fopen("downloadk.mp3", "w");
$url = 'http://serviceprovider.com/storage/read?uid=475b68f2-a31b-40f8-8dfc-5af791a4d5fa_1_r.mp3&ip=255.255.255.255&dir=recording';
$handle = curl_init($url);
// Tell cURL to write contents to the file.
curl_setopt($handle, CURLOPT_FILE, $fp);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_HEADER, false);
// Do the request.
$data = curl_exec($handle);
// Clean up.
curl_close($handle);
fwrite($fp, $data);
fclose($fp);
This created the file download.mp3 file on my server but with 0 bytes, i.e. empty.
The url used here is a download link example not a mp3 file that can be played with modern browser directly.
Function file_get_contents is used for reading local files. What you have is an URL and in order to fetch the contents, you need to do a HTTP request in your script. PHP comes with the curl extension, which provides you with a stable library of functions for doing HTTP requests:
http://php.net/manual/en/book.curl.php
Using curl to download your file could be done like this:
// Open a file, to which contents should be written to.
$downloadFile = fopen("download.mp3", "w");
$url = "http://serviceprovider.com/storage/read?uid=475b68f2-a31b-40f8-8dfc-5af791a4d5fa_1_r.mp3&ip=255.255.255.255&dir=recording";
$handle = curl_init($url);
// Tell cURL to write contents to the file.
curl_setopt($handle, CURLOPT_FILE, $downloadFile);
// Follow redirects.
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
// Do the request.
curl_exec($handle);
// Clean up.
curl_close($handle);
fclose($downloadFile);
You should probably add some error checking.

PHP Copy Dynamically created TIFF image from remote server

I've written a script that searches through exiting legal case dockets for things like "motion to intervene" and "motion to compel". If the regular expression returns true, then it looks to see if there is a scanned image of the document online for public use. That image is a TIFF file, but not an ordinary tiff file. Here is a link to an example of what I'm trying to copy to my own server.
http://www.oscn.net/applications/oscn/getimage.tif?submitted=true&casemasterid=2565129&db=OKLAHOMA&barcode=1012443256
Here is the error you get if you only try to look at the http://www.oscn.net/applications/oscn/getimage.tif
It is a TIFF file but dynamic. I've used the fopen(), CURL, etc without success. I've used these types of functions with JPG images from random sites just to check to make sure that my server allowed this type of stuff and it worked.
I don't have PDFlib installed on the server (I checked the PEAR and it's not available there either, though I'm not 100% sure that is where it would be.) My host uses cPanel. The server is running Apache. I'm not sure where else to look for a solution to this problem.
I've seen some solutions that used PDFlib but each of those grabbed a normal TIFF image, not one that was dynamically created. My thought though is that it shouldn't matter if I can get the image data to stream, shouldn't I be able to use fopen() and write or buffer that data into my own .tif file?
Thanks for any input and Happy Thanksgiving!
UPDATE: The issue wasn't with CURL, it was with the URL I scraped to pass to CURL. When I printed the $url to the screen, it looked right, but it wasn't. Somewhere & was turned into &, which then threw off CURL because it was fetching an invalid URL (invalid at least according to the remote server where the TIF file is).
For those of you finding this later, here is the script that works perfectly.
//*******************************************************************************
$url = 'http://www.oscn.net/applications/oscn/getimage.tif"
$url .= '?submitted=true&casemasterid=2565129&db=OKLAHOMA&barcode=1016063497';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // set the url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // get the transfer as a string, rather than output it directly
print "Attempting to fetch file...\n";
$img = curl_exec($ch); // get the image
//I used the time() so that in testing I would know when a new file was created rather than always overwriting the old file. This will be changed for final version
if($img){
$fh = fopen('oscn_docs/' . time(). '.tif', 'w'); // this will simply overwrite the file. If that's not what you want to do, you'll have to change the 'w' argument!
if($fh){
$byteswritten = fwrite($fh, $img);
fclose($fh);
}else{
print "Unable to open file.\n";
}
}else{
print "Unable to fetch file.\n";
}
print "Done.\n";
exit(0);
//*******************************************************************************
jarod
For those of you finding this later, here is the script that works perfectly.
//*******************************************************************************
$url = 'http://www.oscn.net/applications/oscn/getimage.tif"
$url .= '?submitted=true&casemasterid=2565129&db=OKLAHOMA&barcode=1016063497';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // set the url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // get the transfer as a string, rather than output it directly
print "Attempting to fetch file...\n";
$img = curl_exec($ch); // get the image
//I used the time() so that in testing I would know when a new file was created rather than always overwriting the old file. This will be changed for final version
if($img){
$fh = fopen('oscn_docs/' . time(). '.tif', 'w'); // this will simply overwrite the file. If that's not what you want to do, you'll have to change the 'w' argument!
if($fh){
$byteswritten = fwrite($fh, $img);
fclose($fh);
}else{
print "Unable to open file.\n";
}
}else{
print "Unable to fetch file.\n";
}
print "Done.\n";
exit(0);
//*******************************************************************************

echo + file_get_contents() adds extra line breaks to the output

The following code transfers an image that is created on the fly from a server to a client site using cURL. It stopped working recently and have not been able to find out what the problem is:
// get_image.php
ob_start();
// create a new CURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, 'url/to/image.php');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// set timeouts
set_time_limit(30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// open a stream for writing
$outFile = fopen($fileDestination, 'wb');
curl_setopt($ch, CURLOPT_FILE, $outFile);
// grab file from URL
curl_exec($ch);
fclose($outFile);
// close CURL resource, and free up system resources
curl_close($ch);
ob_end_clean();
//image.php
/*
* Create image based on client site ...
*/
$filePath = 'path/to/image.png'
$imageFile = file_get_contents($filePath);
header("content-type: image/png");
echo $imageFile;
unlink($filePath);
The file get_image.php is located in a client site and calls the file image.php located in my server.
After running this code the image in the client site is about 7 bytes larger than the original, these bytes seem to be line breaks. After debugging for several hours I found out that these bytes are added when I echo $imageFile. If the 7 bytes are manually removed from the resulting image, the image displays correctly.
There are no errors nor exceptions thrown. The image created in the server is created with no issues. The only output in FF is "The image 'url/to/image.php' cannot be displayed, because it contains errors"
I am not sure what is causing this. Help is greatly appreciated.
Onema
UPDATE:
http://files.droplr.com/files/38059844/V5Jd.Screen%20shot%202011-01-12%20at%2012.17.53%20PM.png
http://files.droplr.com/files/38059844/QU4Z.Screen%20shot%202011-01-12%20at%2012.23.37%20PM.png
Some things to check.
That both files are stored without BOMs
That '<?php' are the first five characters and '?>' the last two in both files.
That when you remove the ob_start() and ob_end-clean(() it should show no error messages.
If you put the unlink before the genereation, you can see the genereated file - check it is valid.
You might want to start the practice of leaving the final ?> from the end of your files - it isn't necessary, and can cause problems if there is whitespace and newlines following the php delimiter.

PHP rel=NOFOLLOW

I want to download files, from an website using PHP.
And i want to create an php script to download files without going on their website to download files. I just want to pun their link on my script an download the file automatically.
I try with CURL, but doesn't work.... The link is like this <a rel="nofollow" href="/download-15866-114621.srt"><b>Download</b></a>
the code :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
'http://subtitrari.regielive.ro/download-15866-114621.srt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
echo $contents;
curl_close ($ch);
I get "download failed!" as content, which means they probably have some sort of download protection. The best thing is probably to ask them what you should do (assuming you have their permission to download the file) or stop trying (assuming you don't).
Eitherway, try setting a referer header with CURLOPT_REFERER. Maybe they check that header to see that no-one is hotlinked to the file.

PHP cURL sending and receive Images Client / Server

I have been researching this for a while and have not been find an answer for this.
I have a Client Site making calls to our API Server. What I would like to transfer an image to the Client Site when a special call is made.
I have some code that downloads the image from the server, but this is causing us to make multiple calls forcing us to create all these images in the server that we don't want to keep, even if we delete them afterward.
$originalFileUrl = createImage('createImage', $fileName);
downloadImage($originalFileUrl, $fileDestination);
deleteFileFromServer('deleteImage', $fileName);
function serverCall ($action, $fileName) {
$serverCall = $SERVER.'/api.php?fileName=' . $fileName . '&action=' . $action;
ob_start();
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $serverCall);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_exec($ch);
$fileContents = ob_get_contents();
curl_close($ch);
ob_end_clean();
return $fileContents;
}
function downloadImage ($originalFileUrl, $fileDestination) {
// Starting output buffering
ob_start();
// create a new CURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $originalFileUrl);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// set timeouts
set_time_limit(30); // set time in secods for PHP
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // and also for CURL
// open a stream for writing
$outFile = fopen($fileDestination, 'wb');
curl_setopt($ch, CURLOPT_FILE, $outFile);
// grab file from URL
curl_exec($ch);
fclose($outFile);
// close CURL resource, and free up system resources
curl_close($ch);
ob_end_clean();
}
Where $originalFileUrl is the current location of the file, and $fileDestination is the path to where I want my new file to be.
My question is: Can I make a call to a PHP file in the Server that will be in charge of create, transfer and delete the image all in one call rather than doing multiple calls?
Also for multiple reasons ftp the file from the server to the client is not a good option.
Thank you
This will not be a trivial task. However, you should be able to design a successful approach. This won't be the most error-safe method of accomplishing the task, though. You're thinking right now of a HTTP-esque stateless protocol, which is manageable. If the description below doesn't sound good enough, consider another protocol which can maintain a constant bi-directional connection (like an SSH tunnel).
You'd likely suffer data overhead, but that would generally be more than acceptable in order to save multiple calls. To that end, I'd advise creating an XML interface. On the receiving end, your XML would have an element with either a Base64 representation of the image, or possibly a gzipped CDATA implementation. You don't have to stick to any XML standard, but if you do, the PHP XML Parser could help with some of the legwork.
So, to recap, in this model, the server end could receive a set of commands which do what you've called out: move the file into a processing folder, create a Base64 string of the file contents, craft the XMl package, and return it. The client will send a request, and process the response. If the client detects an error, it could retry and the server can still grab the file data from the processing queue.
If error becomes an issue and an open socket isn't a good option (because the coding is difficult), you could also develop a delete-batching system, where you track the files in the processing folder and only delete them on request. But, you'd only make delete requests from the client every once in a while, and possibly not as a part of any particular page with a user experience, but from a cron.

Categories