I am handling imap in php via socket. Everything works perfect. I build mail swift object, then convert it to string and send to imap with append command, it works, I get OK [APPENDUID 1 4497] (Success), but if I attach file greater than about 3kb - imap server responds me nothing, and the mail doesn't append! What is the problem? May be I should make append something like part by part? I mean cut mail body for several parts and make several fwrite()?
These are several parts of my code. But I think problem is not in the code, but in some common imap stuff:
/**
* #param string $command
* #param string $successPattern
* #param bool $withCounter
* #return false|string
*/
protected function sendCommand($command, $successPattern = '', $withCounter = true)
{
$counter = $withCounter ? "{$this->commandHash}{$this->commandCounter} " : "";
$successPattern = !$successPattern ? $counter . 'OK' : $successPattern;
fwrite($this->stream, "{$counter}{$command}\r\n");
$this->commandCounter++;
$previousLine = '';
$buf = '';
$time = time();
while ((time() - $time) < $this->timeOut) {
$newLine = fread($this->stream, 4096);
if(!strlen($newLine)) continue;
$buf .= $newLine;
file_put_contents("/LOG2.txt", $newLine, FILE_APPEND);
if (strripos($previousLine.$newLine, $successPattern) !== FALSE){
$this->responseContainer->setLastResponseText($buf);
return $buf;
}
if (strripos($previousLine.$newLine, $this->commandHash . ($this->commandCounter - 1) . ' NO') !== FALSE
|| strripos($previousLine.$newLine, $this->commandHash . ($this->commandCounter - 1) . ' BAD') !== FALSE){
$this->responseContainer->setLastErrorText($buf);
return false;
}
$previousLine = $newLine;
}
var_dump(" Time out");
$this->responseContainer->setLastErrorText("{$command} {$counter} Time out");
return false;
}
/**
* #param $mailString
* #param string $folder
* #return false|int
*/
public function append($mailString, $folder = "INBOX")
{
if($this->sendCommand("APPEND {$folder} {".strlen($mailString)."}", "go ahead")){
$response = $this->sendCommand($mailString, '', false);
return $this->imapParser->parseAppendResult($response);
}
return false;
}
$message = new \Swift_Message();
$message->setSubject($mail->subject);
$message->setFrom($mail->from);
$message->setTo($mail->to);
$message->addPart($mail->body, 'text/html');
foreach($mail->files as $file){
$attachment = \Swift_Attachment::fromPath($file->getPath());
$attachment->setFilename($file->name.".".$file->type);
$message->attach($attachment);
}
$appendUid = $this->imapService->getCommander()->append($message->toString());
Creating stream:
/**
* #param string $host
* #param integer $port
* #return bool
*/
protected function createStream($host, $port)
{
//#todo proxy authentication
if($this->stream = #stream_socket_client("ssl://{$host}:{$port}", $errno, $errstr, $this->timeOut, STREAM_CLIENT_CONNECT, $this->context)) {
stream_set_timeout($this->stream, $this->timeOut);
return $this->stream;
}
$this->responseContainer->setLastErrorText("Failed connection to imap. Without proxy. " . $errstr);
return false;
}
Related
I'm trying to serve large audio files from google cloud storage with seeking support.
I have difficulties understanding php fopen and google stream wrapper working together.
When fopen is called it immediately calls stream_open from google StreamWrapper class.
However im unable to pass options to it through fopen context. I would like to set bitwise option 0b10000 as its STREAM_MUST_SEEK option. $flags parameter is always 0.
https://www.php.net/manual/en/streamwrapper.stream-open
Documentation shows there are atleast two options you can set, but it doesnt tell where you can set them.
Without $flag set to 0b10000 im getting:
PHP Warning: stream_copy_to_stream(): Failed to seek to position 85721088 in the stream in /home/project/src/Classes/StreamResponse.php on line 296
If i set $flags to 0b10000 it works and supports seeking.
$opts = array(
'gs' => array('key' => 'value')
);
$context = stream_context_create($opts);
$out = fopen('php://output', 'wb');
$file = fopen($this->file->getPathname(), 'rb', false, $context);
stream_copy_to_stream($file, $out, $this->maxlen, $this->offset);
fclose($out);
fclose($file);
/**
* Callback handler for when a stream is opened. For reads, we need to
* download the file to see if it can be opened.
*
* #param string $path The path of the resource to open
* #param string $mode The fopen mode. Currently only supports ('r', 'rb', 'rt', 'w', 'wb', 'wt')
* #param int $flags Bitwise options STREAM_USE_PATH|STREAM_REPORT_ERRORS|STREAM_MUST_SEEK
* #param string $openedPath Will be set to the path on success if STREAM_USE_PATH option is set
* #return bool
*/
public function stream_open($path, $mode, $flags, &$openedPath)
{
$client = $this->openPath($path);
// strip off 'b' or 't' from the mode
$mode = rtrim($mode, 'bt');
$options = [];
if ($this->context) {
$contextOptions = stream_context_get_options($this->context);
if (array_key_exists($this->protocol, $contextOptions)) {
$options = $contextOptions[$this->protocol] ?: [];
}
}
if ($mode == 'w') {
$this->stream = new WriteStream(null, $options);
$this->stream->setUploader(
$this->bucket->getStreamableUploader(
$this->stream,
$options + ['name' => $this->file]
)
);
} elseif ($mode == 'r') {
try {
// Lazy read from the source
$options['restOptions']['stream'] = true;
$this->stream = new ReadStream(
$this->bucket->object($this->file)->downloadAsStream($options)
);
// Wrap the response in a caching stream to make it seekable
if (!$this->stream->isSeekable() && ($flags & STREAM_MUST_SEEK)) {
$this->stream = new CachingStream($this->stream);
}
} catch (ServiceException $ex) {
return $this->returnError($ex->getMessage(), $flags);
}
} else {
return $this->returnError('Unknown stream_open mode.', $flags);
}
if ($flags & STREAM_USE_PATH) {
$openedPath = $path;
}
return true;
}
For Windows Chrome (and probably many other browsers), this code works for serving an mp3 in an audio element:
/**
*
* #param string $filename
* #return \Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory
*/
public function getMp3($filename) {
$fileContents = Storage::disk(\App\Helpers\CoachingCallsHelper::DISK)->get($filename);
$fileSize = Storage::disk(\App\Helpers\CoachingCallsHelper::DISK)->size($filename);
$shortlen = $fileSize - 1;
$headers = [
'Accept-Ranges' => 'bytes',
'Content-Range' => 'bytes 0-' . $shortlen . '/' . $fileSize,
'Content-Type' => "audio/mpeg"
];
Log::debug('$headers=' . json_encode($headers));
$response = response($fileContents, 200, $headers);
return $response;
}
But when I use an iPhone to browse to the same page, the mp3 file does not show the total duration, and when I play it, it says "Live broadcast".
I've tried to follow suggestions from various answers of this question (HTML5 <audio> Safari live broadcast vs not) and other articles I've read, but none seem to have an effect.
No matter how I change the headers, the mp3 seems to function as desired on Windows and does not work on iOS.
How can I debug what I'm doing wrong?
Here is HTML:
<audio controls preload="auto">
<source src="{{$coachingCall->getMp3Url()}}" type="audio/mpeg"/>
<p>Your browser doesnt support embedded HTML5 audio. Here is a link to the audio instead.</p>
</audio>
MP3 files don't have timestamps, and therefore no inherent length that can be known ahead of time. Chrome is just guessing, based on the bitrate at the beginning of the file and the byte size of the file. It doesn't really know.
Some players don't bother guessing.
Also, all browsers on iOS are Safari under the hood, thanks to some incredibly restrictive policies by Apple. Therefore, Chrome on iOS is really just a wrapper for a Safari web view.
Whoa, that was a very difficult problem to solve. (It took me days.)
And I learned that it wasn't just iOS that was having problems: Safari on Mac hadn't been working either.
Now I think everything works on every browser I've tested.
I'm really glad I found this example to follow.
Here is my answer:
/**
*
* #param string $disk
* #param string $filename
* #return \Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\StreamedResponse
*/
public static function getMediaFile($disk, $filename) {
$rangeHeader = request()->header('Range');
$fileContents = Storage::disk($disk)->get($filename);
$fullFilePath = Storage::disk($disk)->path($filename); //https://stackoverflow.com/a/49532280/470749
$headers = ['Content-Type' => Storage::disk($disk)->mimeType($fullFilePath)];
if ($rangeHeader) {
return self::getResponseStream($disk, $fullFilePath, $fileContents, $rangeHeader, $headers);
} else {
$httpStatusCode = 200;
return response($fileContents, $httpStatusCode, $headers);
}
}
/**
*
* #param string $disk
* #param string $fullFilePath
* #param string $fileContents
* #param string $rangeRequestHeader
* #param array $responseHeaders
* #return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public static function getResponseStream($disk, $fullFilePath, $fileContents, $rangeRequestHeader, $responseHeaders) {
$stream = Storage::disk($disk)->readStream($fullFilePath);
$fileSize = strlen($fileContents);
$fileSizeMinusOneByte = $fileSize - 1; //because it is 0-indexed. https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.16
list($param, $rangeHeader) = explode('=', $rangeRequestHeader);
if (strtolower(trim($param)) !== 'bytes') {
abort(400, "Invalid byte range request"); //Note, this is not how https://stackoverflow.com/a/29997555/470749 did it
}
list($from, $to) = explode('-', $rangeHeader);
if ($from === '') {
$end = $fileSizeMinusOneByte;
$start = $end - intval($from);
} elseif ($to === '') {
$start = intval($from);
$end = $fileSizeMinusOneByte;
} else {
$start = intval($from);
$end = intval($to);
}
$length = $end - $start + 1;
$httpStatusCode = 206;
$responseHeaders['Content-Range'] = sprintf('bytes %d-%d/%d', $start, $end, $fileSize);
$responseStream = response()->stream(function() use ($stream, $start, $length) {
fseek($stream, $start, SEEK_SET);
echo fread($stream, $length);
fclose($stream);
}, $httpStatusCode, $responseHeaders);
return $responseStream;
}
I can't comment since I just made my account, so... complementing RYAN's
Just found out that you can save some loading time removing the
$fileContents = Storage::disk($disk)->get($filename);
And replacing it with
$fileSize = Storage::disk($disk)->size($filename);
Passing the size directly to the getResponseStream function, instead of downloading the whole content into a variable and then measuring the length.
Thank you Ryan, saved me a lot of precious time with the stinky safari.
So, here's the skinny.
I have an HTML form (of which there will be many depending on the Letter template) that passes the form data into a Letter Template via PHP. THIS...I have successfully done.
However, what I need is to pass the data INTO the template, THEN turn that template INTO a PDF.
Any suggestions? AND......GO
I've recently use pdftk (server) to do so: https://www.pdflabs.com/tools/pdftk-server/
First, install it on a webserver or locally.
Then, here's a PHP class I adapted from the web (copy it and name it PdfFormToPdftk.php):
<?php
class PdfFormToPdftk {
/*
* Path to raw PDF form
* #var string
*/
private $pdfurl;
/*
* Path to PDFKTK
* #var string
*/
private $pdftkpath;
/*
* Path to temp files
* #var string
*/
private $tmppath;
/*
* Errors
* #var string
*/
private $errors;
/*
* Last command done
* #var string
*/
private $lastcmd;
/*
* Form data
* #var array
*/
private $data;
/*
* Path to filled PDF form
* #var string
*/
private $output;
/*
* Flag for flattening the file
* #var string
*/
private $flatten;
public function __construct($pdfurl, $data, $tmppath, $pdftkpath = '/usr/bin/pdftk') {
$this->pdfurl = $pdfurl;
$this->data = $data;
$this->tmppath = $tmppath;
$this->pdftkpath = $pdftkpath;
}
private function tempfile() {
return tempnam($this->tmppath, gethostname());
}
public function fields($pretty = false) {
$tmp = $this->tempfile();
exec("{$this->pdftkpath} {$this->pdfurl} dump_data_fields > {$tmp}");
$con = file_get_contents($tmp);
unlink($tmp);
return $pretty == true ? nl2br($con) : $con;
}
private function makeFdf() {
$fdf = '%FDF-1.2
1 0 obj<</FDF<< /Fields[';
foreach ($this->data as $key => $value) {
$fdf .= '<</T(' . $key . ')/V(' . $value . ')>>';
}
$fdf .= "] >> >>
endobj
trailer
<</Root 1 0 R>>
%%EOF";
$fdf_file = $this->tempfile();
file_put_contents($fdf_file, $fdf);
return $fdf_file;
}
public function flatten() {
$this->flatten = ' flatten';
return $this;
}
private function generate() {
$fdf = $this->makeFdf();
$this->output = $this->tempfile();
$cmd = "{$this->pdftkpath} {$this->pdfurl} fill_form {$fdf} output {$this->output}{$this->flatten} 2>&1";
$this->lastcmd = $cmd;
exec($cmd, $outputAndErrors, $returnValue);
$this->errors = $outputAndErrors;
unlink($fdf);
}
public function save($path = null) {
if (is_null($path)) {
return $this;
}
if (!$this->output) {
$this->generate();
}
$dest = pathinfo($path, PATHINFO_DIRNAME);
if (!file_exists($dest)) {
mkdir($dest, 0775, true);
}
if (!copy($this->output, $path)) {
echo "failed to copy $path...\n";
}
unlink($this->output);
$this->output = $path;
return $this;
}
public function download() {
if (!$this->output) {
$this->generate();
}
$filepath = $this->output;
if (file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=' . uniqid(gethostname()) . '.pdf');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
readfile($filepath);
exit;
}
}
}
You could use it like this in some script:
<?php
require_once 'PdfFormToPdftk.php';
$datas = ['firstname' => 'Foo', 'lastname' => 'Bar'];
$output_file = 'yourfile.pdf';
$template_pdf_file = 'templatefile.pdf'; //where your virgin pdf template contains at least 'firstname' and 'lastname' as editable fields. You might want to use Adobe Acrobat Pro and save it as a Adobe Static PDF Form
$path_to_pdftk_server = '/opt/pdflabs/pdftk/bin/pdftk'; // type 'which pdftk' in your console to find yours
$pdf = new PdfFormToPdftk($template_pdf_file, $datas, '/', $path_to_pdftk_server);
$file = $pdf->save($output_file);
I'm trying to code a C# UDP server. It receives a specific ID from the client, and return the song associated with it. The client is a PHP webpage, and stocks the bytes received into a file. Right now I'm doing some tests, trying to simply start a fake lecture of the song (just a javascript alert) when the transfer is at 2048 bytes. But I have plenty of bugs... The PHP page seems to finish the transfer into the file BEFORE having received all the data... The server continue to send bytes but the file is complete, with the good weight and all...
I know I don't have a very good english, so if you don't undersood something, just ask !
Here is the C# code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Data.SQLite;
namespace cdCollector
{
public partial class Streaming : Form
{
private static List<IPAddress> clients_ = new List<IPAddress>();
public Streaming()
{
InitializeComponent();
listen();
}
public class ThreadClient
{
private static UdpClient socket_;
private static IPEndPoint ipepClient_;
private static int noChanson_;
private static SQLiteConnection connexion_;
public void setSocket(ref UdpClient socket) { socket_ = socket; }
public void setIpepClient(ref IPEndPoint ipepClient) { ipepClient_ = ipepClient; }
public void setNoChanson(int noChanson) { noChanson_ = noChanson; }
public void setConnexion(ref SQLiteConnection connexion) { connexion_ = connexion; }
public static void send()
{
try
{
while (Thread.CurrentThread.IsAlive)
{
Chanson uneChanson;
FileStream stream;
byte[] buffer = new byte[1024];
int read;
uneChanson = new Chanson(noChanson_);
uneChanson.load(ref connexion_);
stream = new FileStream("C:\\Users\\Julie\\Documents\\toune.flac", FileMode.Open, FileAccess.Read);
socket_.Send(Encoding.ASCII.GetBytes(stream.Length.ToString()), stream.Length.ToString().Length, ipepClient_);
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
socket_.Send(buffer, buffer.Length, ipepClient_);
Console.WriteLine("finished");
}
}
catch (ThreadAbortException tae)
{ }
catch (Exception)
{
Thread.CurrentThread.Abort();
}
}
}
public static void listen()
{
byte[] data = new byte[1024];
IPEndPoint ipepServer = new IPEndPoint(IPAddress.Any, 7575); // IP du serveur
IPEndPoint ipepClient = new IPEndPoint(IPAddress.Any, 0); // IP du client;
UdpClient socket = new UdpClient(ipepServer); // socket serveur
int noChanson;
SQLiteConnection connexion = new SQLiteConnection("Data Source=" + Application.StartupPath + "\\cdCollector.db");
SQLiteCommand command = new SQLiteCommand(connexion);
SQLiteDataReader dr;
Thread thread;
connexion.Open();
while (true)
{
try
{
Console.WriteLine("Waiting for a client...");
data = socket.Receive(ref ipepClient);
Console.WriteLine("Message received from {0}:", ipepClient.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length));
command.CommandText = "SELECT KeyLocale FROM AssocKeys WHERE NomTable = 'Chanson' AND KeyWeb = "
+ int.Parse(Encoding.ASCII.GetString(data, 0, data.Length));
dr = command.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
noChanson = dr.GetInt32(0);
dr.Close();
ThreadClient client = new ThreadClient();
client.setConnexion(ref connexion);
client.setIpepClient(ref ipepClient);
client.setNoChanson(noChanson);
client.setSocket(ref socket);
thread = new Thread(new ThreadStart(ThreadClient.send));
thread.Start();
}
else
socket.Send(Encoding.ASCII.GetBytes("Erreur: Chanson introuvable"), ("Erreur: Chanson introuvable").Length, ipepClient);
}
catch (SocketException se)
{
Console.WriteLine("Erreur Socket:" + se.Message);
}
catch (Exception ex)
{
Console.WriteLine("Erreur: " + ex.Message);
}
}
connexion.Close();
}
}
}
And the PHP code:
<?php
session_start();
$address="192.168.2.2";
$read = false;
$port = 7575;
$length = 0;
$started = false;
if (isset($port) and
($socket=socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) and
(socket_connect($socket, $address, $port)))
{
$text = "Connection successful on IP $address, port $port <br />";
$from = '';
$port = 0;
$length = 0;
socket_send( $socket, $_GET['no'], 1024, MSG_EOR );
socket_recvfrom( $socket, $buf, 1024, 12, $from, $port);
$lengthTotal = $buf;
echo "Taille prévue du fichier: " . $lengthTotal . "<br />";
if( file_exists( "temp" . $_SESSION['ID_Membre'] . ".flac" ) )
unlink("temp" . $_SESSION['ID_Membre'] . ".flac");
$file = fopen("temp" . $_SESSION['ID_Membre'] . ".flac", 'a');
$buf = null;
while( $length < $lengthTotal )
{
$length += socket_recvfrom( $socket, $buf, 1024, 12, $from, $port );
if( $length > 2048 && !$started )
{
?>
<script type="text/javascript">
<!--
alert("Lecture...");
//->
</script>
<?php
$started = true;
}
fputs($file, $buf);
flush();
}
echo "<br />" . $length . "<br />";
fclose($file);
}
else
$text="Unable to connect<pre>".socket_strerror(socket_last_error())."</pre>";
echo $text;
?>
Thanks a lot !
UDP is an inherently unreliable transport. You will need to implement acknowledgements, timeouts, retransmissions and sequence numbers on top of UDP in order to guarantee transmission of all of your data in the expected order, unless your client application can live with dropped packets. I would advise you to consider using TCP sockets instead if you need guaranteed transmission of data between server and client and don't want to have to implement all of this stuff yourself (which might need to include client-side buffering to rearrange out-of-order datagrams). If you want reliability on top of UDP, I would advise you to read a good textbook on the subject (e.g. "Unix Network Programming" by W. Richard Stevens etc.).
Pointers on TCP:
You should take a look at System.Net.Sockets.TcpClient and System.Net.Sockets.TcpListener for the C# side of things and consult the PHP documentation for info on the PHP side of things.
Using TCP sockets isn't really that much different except you'll be using send and recv (or C#/PHP equivalents) instead of send_to and recv_from. Setting up the server side of things is a little more complicated since you need to bind and listen etc. but there are plenty of resources, e.g.:
http://www.switchonthecode.com/tutorials/csharp-tutorial-simple-threaded-tcp-server
Thanks for your help. I changed what you told me, except adding 'b' to the fopen mode because my web server is on Ubuntu. I still receive plenty of errors to tell me that the client connection had to close... It seems like PHP think the download is finished and exit the loop, so it closes the connection of the socket. Also, many minutes after the page have load, the server is still sending data... I never did streaming before so I have difficulties to see where the problem is ...
Here's the new PHP code:
<?php
session_start();
$address="192.168.2.2";
$read = false;
$port = 7575;
$length = 0;
$started = false;
if (isset($port) and
($socket=socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) and
(socket_connect($socket, $address, $port)))
{
$text = "Connection successful on IP $address, port $port <br />";
$from = '';
$port = 0;
$length = 0;
socket_send( $socket, $_GET['no'], 1024, MSG_EOR );
socket_recvfrom( $socket, $buf, 1024, MSG_WAITALL, $from, $port);
$lengthTotal = $buf;
echo "Taille prévue du fichier: " . $lengthTotal . "<br />";
if( file_exists( "temp" . $_SESSION['ID_Membre'] . ".flac" ) )
unlink("temp" . $_SESSION['ID_Membre'] . ".flac");
$file = fopen("temp" . $_SESSION['ID_Membre'] . ".flac", 'a');
$buf = null;
while( $length !== FALSE && $length < $lengthTotal )
{
$length += socket_recvfrom( $socket, $buf, 1024, 12, $from, $port );
if( $length > 2048 && !$started )
{
?>
<script type="text/javascript">
<!--
alert("Lecture...");
//->
</script>
<?php
$started = true;
}
if( $length == FALSE )
echo "ERREUR";
fputs($file, $buf, $length);
flush();
}
echo "<br />" . $length . "<br />";
fclose($file);
}
else
$text="Unable to connect<pre>".socket_strerror(socket_last_error())."</pre>";
echo $text;
?>
Some points:
1.- socket_recvfrom could return FALSE if there is any error, you can check the error with false === socket_recvfrom.
2.- If you are using a windows server add b to the open mode: $file = fopen("temp" . $_SESSION['ID_Membre'] . ".flac", 'ab'); (you are writing a binary file).
3.- Use as third argument of the fputs function the value returned by the socket_recvfrom function (if this value !== FALSE).
4.- You are using the value 12 (MSG_DONTROUTE | MSG_EOR), try to use 0 or MSG_WAITALL (of course socket_recvfrom is going to wait to receive 1024 bytes).
Your reception loop must be:
$reclen = 0;
while( ($reclen !== FALSE) && ($length < $lengthTotal) )
{
$reclen = socket_recvfrom( $socket, $buf, 1024, 12, $from, $port );
if ($reclen === FALSE)
{
echo "ERREUR";
break;
}
$length += $reclen;
if( $length > 2048 && !$started )
{
?>
<script type="text/javascript">
<!--
alert("Lecture...");
//->
</script>
<?php
$started = true;
}
fputs($file, $buf, $length);
flush();
}
The problem is that you are adding the value returned by socket_recvfrom to $length if the return value is FALSE is going to add 0 to $length, that is the reason why you have to add an additional variable ($reclength).
I have to download big file (1xx MB) using PHP.
How can i download this without wasting memory (RAM) for temporary file ?
When i use
$something=file_get_contents('http://somehost.example/file.zip');
file_put_contents($something,'myfile.zip');
I need to have so much memory that size of that file.
Maybe it's possible to download it using any other way ?
For example in parts (for example 1024b), write to disk, and download another part repeating until file will be fully downloaded ?
Copy the file one small chunk at a time
/**
* Copy remote file over HTTP one small chunk at a time.
*
* #param $infile The full URL to the remote file
* #param $outfile The path where to save the file
*/
function copyfile_chunked($infile, $outfile) {
$chunksize = 10 * (1024 * 1024); // 10 Megs
/**
* parse_url breaks a part a URL into it's parts, i.e. host, path,
* query string, etc.
*/
$parts = parse_url($infile);
$i_handle = fsockopen($parts['host'], 80, $errstr, $errcode, 5);
$o_handle = fopen($outfile, 'wb');
if ($i_handle == false || $o_handle == false) {
return false;
}
if (!empty($parts['query'])) {
$parts['path'] .= '?' . $parts['query'];
}
/**
* Send the request to the server for the file
*/
$request = "GET {$parts['path']} HTTP/1.1\r\n";
$request .= "Host: {$parts['host']}\r\n";
$request .= "User-Agent: Mozilla/5.0\r\n";
$request .= "Keep-Alive: 115\r\n";
$request .= "Connection: keep-alive\r\n\r\n";
fwrite($i_handle, $request);
/**
* Now read the headers from the remote server. We'll need
* to get the content length.
*/
$headers = array();
while(!feof($i_handle)) {
$line = fgets($i_handle);
if ($line == "\r\n") break;
$headers[] = $line;
}
/**
* Look for the Content-Length header, and get the size
* of the remote file.
*/
$length = 0;
foreach($headers as $header) {
if (stripos($header, 'Content-Length:') === 0) {
$length = (int)str_replace('Content-Length: ', '', $header);
break;
}
}
/**
* Start reading in the remote file, and writing it to the
* local file one chunk at a time.
*/
$cnt = 0;
while(!feof($i_handle)) {
$buf = '';
$buf = fread($i_handle, $chunksize);
$bytes = fwrite($o_handle, $buf);
if ($bytes == false) {
return false;
}
$cnt += $bytes;
/**
* We're done reading when we've reached the conent length
*/
if ($cnt >= $length) break;
}
fclose($i_handle);
fclose($o_handle);
return $cnt;
}
Adjust the $chunksize variable to your needs. This has only been mildly tested. It could easily break for a number of reasons.
Usage:
copyfile_chunked('http://somesite.com/somefile.jpg', '/local/path/somefile.jpg');
you can shell out to a wget using exec() this will result in the lowest memory usage.
<?php
exec("wget -o outputfilename.tar.gz http://pathtofile/file.tar.gz")
?>
You can also try using fopen() and fread() and fwrite(). That way you onlly download x bytes into memory at a time.