Hey,
I'm trying to remotely download an image to my server via the web and an URL that the user inputs. I have managed to do this with a cron job, and write the file to the system no problem.
My problem lies with viewing the image, the image wont show. I looked at the source code of the image and it seems to be because PHP is writing data to the top of the file, - HTTP/1.1 200 OK Set-Cookie: JCPARPT=ZLNIZZSSRVDXECKKMO; path=/ Content-Length: 9566 Content-Type: image/jpeg Last-Modified: Fri, 05 May 2006 22:14:11 GMT Accept-Ranges: bytes ETag: "8811773c9170c61:9be" Server: Microsoft-IIS/6.0 X-Powered-By: ASP.NET Date: Wed, 13 Jun 2007 14:13:08 GMT ÿØÿà�JF
Is there anyway I can stop PHP doing this, or remove it from the code with some sort of preg_match before writing it to disk.
Or is there an alternative method I should be using?
thanks in advance for any time and help.
14 1702
what is your code for getting the image?
[PHP]<?php
include ('includes/functions.php');
$Error = "";
$UploadDIR = 'images/';
$ShortenTo = 15;
function RemoteGet($address) {
// Connect to the google website to find the millage
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $address);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
// Make a random string
$RandomString = RandomString(4);
// Get the file extention to put back later
$FileExt = GetFileExt($address);
// Make it lowercase
strtolower($FileExt);
// Check its the correct extention
if (($FileExt == 'jpg') || ($FileExt == 'gif') || ($FileExt == 'png') || ($FileExt == 'bmp')) {
// Remove the file extention from the file
$file = RemoveExtension($address);
// Now clean anything thats not alphanumeric
$file = preg_replace('`[^a-z0-9]`i', '', $file);
// Shoerten the filename
$file = substr($file, 0, $ShortenTo);
// Put the extention back on
$file = $file . '.' . $FileExt;
// Set the path
$uploadfile = $UploadDIR . $RandomString . $file;
// The filename is
$FileName = $RandomString . $file;
// Write the file to disk
$fh = fopen($FileName, 'w') or die("can't open file");
fwrite($fh, $data);
fclose($fh);
return "http://" . $_SERVER[HTTP_HOST] . "/Image-" . $FileName;
}
}
echo RemoteGet("http://google.com/images/google.gif");
?>[/PHP]
Rather than perform a curl call (which is likely where the headers are coming from), just fopen the URL directly.
Rather than perform a curl call (which is likely where the headers are coming from), just fopen the URL directly.
Will try that in a sec, thanks.
Was going to use that but thought curl was quicker.
Will try that in a sec, thanks.
Was going to use that but thought curl was quicker.
Let us know how it turns out.
Ok, now it sort of works lol. But the file only seems to half download, Check here:
[ur]http://piczy.net/Image-MCTWK.gif[/url]
I changed the Cron job with the following:
[PHP]$fh = fopen($address, 'r') or die("Can't open file");
$data = fread($fh, 524288000000);
fclose($fh);[/PHP]
Note all the 0000000 at the end was me upping the limit to see if thats why it was stopping. But it wasnt.
Any ideas? This must be possible and cant be this hard lol.
Thanks for your time.
Wait!
Am I correct in my understanding that you are using cron to execute a PHP script?
Wait!
Am I correct in my understanding that you are using cron to execute a PHP script?
no, I have the above code in a php file, then just run the file directly. I had curl to get the image (i may have said cron before sorry), but changed it to fopen as you said.
Will have a go of file get contents.
Straight from PHP.net When reading from anything that is not a regular local file, such as streams returned when reading remote files or from popen() and fsockopen(), reading will stop after a packet is available. This means that you should collect the data together in chunks as shown in the examples below. -
<?php
-
$handle = fopen("http://www.example.com/", "rb");
-
$contents = '';
-
while (!feof($handle))
-
{
-
$contents .= fread($handle, 8192);
-
}
-
fclose($handle);
-
?>
-
YEY, file get contents works great thanks very much for all the help.
May I have one request?, can I have the code from my second post removed please? Everything else can stay.
Once again thanks all.
YEY, file get contents works great thanks very much for all the help.
May I have one request?, can I have the code from my second post removed please? Everything else can stay.
Once again thanks all.
We don't work like that...I'm sure something could be censored out if you need it to.
Just didn't want hackers knowing my script. can you censor like 4 and 40 please.
Thanks :)
Done!
Come back with any more questions.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Will McGugan |
last post by:
Hi,
I'm writing an app that downloads images. It rejects images that are
under a certain size - whithout downloading them completely. I've
implemented this using PIL, by downloading the first K...
|
by: Dave |
last post by:
Dear Sirs,
Is there a way to get the width and height of an image without
downloading the image, or with just downloading a minimal portion of
the image?
For instance, I have a list of 10,000...
|
by: Meena Desai |
last post by:
Hi,
In my .aspx page there are some .jpg images. When I browse the page using
IE6, it goes into an infinite loop in downloading the image. No image is
displayed after that. If I browse .htm page...
|
by: Stan Sainte-Rose |
last post by:
Hi,
What is the better way to save image into a database ?
Just save the path into a field or save the image itself ?
I have 20 000 images (~ 10/12 Ko per image ) to save.
Stan
|
by: Charlie King |
last post by:
As I understand it, the use of FIR* to replace heading tags with
images in visually enabled CSS browsers is now frowned upon on the
basis that some screen readers will *nor* read back text that is...
|
by: bedges |
last post by:
okay, the scenario:
i have a header image which changes randomly across all pages in the
site. that works fine. i also have an image preloader within the random
header picker which theoretically...
|
by: OBAFGKM_RNS |
last post by:
I have a javascript timer on my page that counts to 10. Every half
second it alternates images on my buttons from a lighter image to a
darker image, giving the appearance of the buttons blinking....
|
by: sberry |
last post by:
I have an image proxy script I use to retrieve images from a remote
server and store locally on the first request. On the second request
(and all requests after) the image is returned from the...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |