Jump to content

File Transfer/GZip


mtylerb

Recommended Posts

Hey guys (and gals),

 

I'm trying to call files from one of my servers to another.  They are all image files and I'm mostly doing this as a learning experiment.  Right now I have it so that it is reading the contents of the files, but the .gz file it creates only has one file and the image in it is about the first 10 pixels deep, and then nothing.  So, assuming $contents is the fread of the file and $imagesize is just that, how would I put multiple files into a .gz compressed archive?

 

I'm currently trying:

 

		$archive = gzopen($_SERVER['DOCUMENT_ROOT'].'/grab/files/'.$this->year.'-'.$this->month.'-'.$this->day.'.gz','wb9');
	foreach($this->images as $url)
	{
		$parsed = parse_url($url);
		$host = $parsed["host"];
		$fp = @fsockopen($host, 80, $errno, $errstr, 20);
		if(!$fp)return false;
		else {
			@fputs($fp, "HEAD $url HTTP/1.1\r\n");
			@fputs($fp, "HOST: $host\r\n");
			@fputs($fp, "Connection: close\r\n\r\n");
			$headers = "";
			while(!@feof($fp))$headers .= @fgets ($fp, 128);
		}
		@fclose ($fp);
		$return = false;
		$arr_headers = explode("\n", $headers);
		foreach($arr_headers as $header) {
			$s = "Content-Length: ";
			if(substr(strtolower ($header), 0, strlen($s)) == strtolower($s)) {
				$return = trim(substr($header, strlen($s)));
				break;
			}
		}
		$imgsize = $return;
		$fp = fopen($url,'r');
		$contents = fread($fp, $imgsize);
		echo $imgsize."\r\n";
		gzwrite($archive,$contents);
	}
	gzclose($archive);

 

with no success.  Any ideas?  Thanks in advance!

 

EDIT: That echo near the end is just for debugging.

Link to comment
Share on other sites

The current code looks like:

 

	function createArchive()
{
	$archive = gzopen($_SERVER['DOCUMENT_ROOT'].'/grab/files/'.$this->year.'-'.$this->month.'-'.$this->day.'.gz','wb9');
	$x=0;
	foreach($this->images as $url)
	{
		$x++;
		$parsed = parse_url($url);
		$host = $parsed["host"];
		$fp = @fsockopen($host, 80, $errno, $errstr, 20);
		if(!$fp)return false;
		else {
			@fputs($fp, "HEAD $url HTTP/1.1\r\n");
			@fputs($fp, "HOST: $host\r\n");
			@fputs($fp, "Connection: close\r\n\r\n");
			$headers = "";
			while(!@feof($fp))$headers .= @fgets ($fp, 128);
		}
		@fclose ($fp);
		$return = false;
		$arr_headers = explode("\n", $headers);
		foreach($arr_headers as $header) {
			$s = "Content-Length: ";
			if(substr(strtolower ($header), 0, strlen($s)) == strtolower($s)) {
				$return = trim(substr($header, strlen($s)));
				break;
			}
		}
		$imgsize = $return;
		$fp = explode('/',$url);
		copy($url, $_SERVER['DOCUMENT_ROOT'].'/grab/files/'.$fp[count($fp)-1]);
		//unlink($_SERVER['DOCUMENT_ROOT'].'/grab/files/'.$fp[count($fp)-1]);
	}
	gzclose($archive);
	//unlink($_SERVER['DOCUMENT_ROOT'].'/grab/files/'.$this->year.'-'.$this->month.'-'.$this->day.'.gz');
}

 

Probably should have posted that before.  The unlinks are just there so I can uncomment them and delete the relevant files.  Is anyone able to help?

Link to comment
Share on other sites

http://php.net/manual/en/refs.compression.php

 

 

You can probably find something other than tar/gzip to use.

 

 

But, if you can't, you could use one of the many PHP/Tar classes out there.  Googling "PHP tar" came up with a bunch.

 

I may have misread what you were trying to do earlier, actually.  Gzip could suit your cause.  What exactly are you trying to do?

Link to comment
Share on other sites

I want to take a list of image files that I have transferred to a local server and compress them into one file.  So:

 

image1.jpg

image2.jpg

image3.jpg

image4.jpg

 

into

 

images.gz or images.tar.gz

 

Whatever is easier.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.