473,800 Members | 3,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Image Downloading small problem

26 New Member
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 1742
epots9
1,351 Recognized Expert Top Contributor
what is your code for getting the image?
Jun 13 '07 #2
Anthony2oo5
26 New Member
[PHP]<?php
include ('includes/functions.php') ;
$Error = "";
$UploadDIR = 'images/';
$ShortenTo = 15;

function RemoteGet($addr ess) {
// 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_RETURNT RANSFER, 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($add ress);
// Make it lowercase
strtolower($Fil eExt);
// 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 Recognized Expert Specialist
Rather than perform a curl call (which is likely where the headers are coming from), just fopen the URL directly.
Jun 13 '07 #4
Anthony2oo5
26 New Member
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 Recognized Expert Specialist
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
Anthony2oo5
26 New Member
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 Recognized Expert Specialist
Wait!

Am I correct in my understanding that you are using cron to execute a PHP script?
Jun 13 '07 #8
pbmods
5,821 Recognized Expert Expert
Try using file_get_conten ts, or better yet, fread instead.
Jun 14 '07 #9
Anthony2oo5
26 New Member
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

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

Similar topics

4
6289
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 and trying to create a PIL image with it. PIL raises an exception because the file is incomplete, but the image object is initialised with the image dimensions, which is what I need. It actualy works well enough, but I'm concerened about...
2
7056
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 image urls, and I would like to get the width and height of every image without spending the time and bandwidth of downloading every image.
3
1531
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 with same images, it downloads immediately. I found one solution to this problem, that is install KB889293 patch for IE6. But this is not the correct way, b'coz if the images are displayed in .htm page, they should be displayed in .aspx page. ...
35
2676
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
2621
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 marked up by CSS to be invisible or hidden. So... If I want to replace headings with images (because my client wants to use his specific font for headings where possible), what's the best way to do it?
2
2075
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 loads the header image before the rest of the graphical content on the page. however, what appears to be happening, judging by the status bar messages, is that the banner always loads last. here's the random header code -
5
1750
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. The problem is, when my Internet Explorer options are set to Check for newer versions of the page: Every visit to the page, IE will download those same 2 images every single time from the server. That's 20 downloads in 10 seconds for 2 images....
1
1478
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 local machine. This has been working fine, but now I am using images that are 4+ MB in size and the script is only saving a small portion of the image rather than the entire thing. Is there a limit to the size of the Image object?
0
9550
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10495
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10269
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.