473,320 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Image Downloading small problem

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,

Expand|Select|Wrap|Line Numbers
  1. 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.
Jun 13 '07 #1
14 1702
epots9
1,351 Expert 1GB
what is your code for getting the image?
Jun 13 '07 #2
[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]
Jun 13 '07 #3
Motoma
3,237 Expert 2GB
Rather than perform a curl call (which is likely where the headers are coming from), just fopen the URL directly.
Jun 13 '07 #4
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.
Jun 13 '07 #5
Motoma
3,237 Expert 2GB
Will try that in a sec, thanks.

Was going to use that but thought curl was quicker.
Let us know how it turns out.
Jun 13 '07 #6
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.
Jun 13 '07 #7
Motoma
3,237 Expert 2GB
Wait!

Am I correct in my understanding that you are using cron to execute a PHP script?
Jun 13 '07 #8
pbmods
5,821 Expert 4TB
Try using file_get_contents, or better yet, fread instead.
Jun 14 '07 #9
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.
Jun 14 '07 #10
Motoma
3,237 Expert 2GB
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.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $handle = fopen("http://www.example.com/", "rb");
  3. $contents = '';
  4. while (!feof($handle))
  5. {
  6.   $contents .= fread($handle, 8192);
  7. }
  8. fclose($handle);
  9. ?> 
  10.  
Jun 14 '07 #11
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.
Jun 14 '07 #12
Motoma
3,237 Expert 2GB
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.
Jun 14 '07 #13
Just didn't want hackers knowing my script. can you censor like 4 and 40 please.

Thanks :)
Jun 15 '07 #14
Motoma
3,237 Expert 2GB
Done!

Come back with any more questions.
Jun 15 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
2
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...
3
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...
35
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
12
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...
2
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...
5
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....
1
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
1
isladogs
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...
0
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...
0
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...
1
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)...
0
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...
0
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....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.