Is there a way to have php echo multiple files?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dustinsmith08
Forum Newbie
Posts: 3
Joined: Wed Jan 27, 2010 12:27 pm

Is there a way to have php echo multiple files?

Post by dustinsmith08 »

I'm making a flash site that loads in several external reasuorces. It needs these resources loaded before it can run.

My first question: is it bad to make mutable http requests at once? I remember hearing that there is a performance penalty for downloading more then two files at once.

My second question: is there a way to have php echo mutable files with one request? Ideally I would have php organize these files in an array and I would then have my flash site pull the files out of the array once once loaded. I know about readfile() and fpassthru(), but these functions only echo the first file I tell the script to send. I know about the zip function, but im worried that this will tie up a lot of server resources. I would like to keep the site scalable and im worried that the php zip function will be too expensive. Unfortunately I cant zip the external resources and request a static zip file, because the site is customized to each user and will load a different set of resources for different users.

Thanks
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Is there a way to have php echo multiple files?

Post by AbraCadaver »

If you just want the files in an array then this will do it:

Code: Select all

$files[] = file_get_contents('path/to/file1');
$files[] = file_get_contents('path/to/file2');
If you want to echo both, then you could:

Code: Select all

echo implode($files);
You could also:

Code: Select all

$files = file_get_contents('path/to/file1');
$files .= file_get_contents('path/to/file2');
echo $files;
 
Not sure if this is what you're after.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
dustinsmith08
Forum Newbie
Posts: 3
Joined: Wed Jan 27, 2010 12:27 pm

worked great

Post by dustinsmith08 »

Thanks! That worked wonderfully. Tomorrow Ill post the flash & php code once i polish it a little, in case anyone else is trying to do the same thing.
dustinsmith08
Forum Newbie
Posts: 3
Joined: Wed Jan 27, 2010 12:27 pm

my solution

Post by dustinsmith08 »

Im posting the code I wrote just case someone else is trying to do the same thing I did. I needed a flash site do efficiently download several external resources. Generally I found that using this method is only slightly faster but seems to become increasingly more efficient with larger amounts of files.

following is the php code:

(saved as sendObject.php)

<?php
session_start();
if($_GET['typ'] == 1){
$rootFolder = ""; //the root where php gets all the files from
$fileList = explode(",", $_POST['list']);
//die("list count: " . count($fileList));

$files[] = file_get_contents($rootFolder . $fileList[0]);
$map = "".filesize($rootFolder . $fileList[0]);

//$testing = "-p0";
//
$filesCount = count($fileList);

for($j=1 ; $j<$filesCount ; $j++){
//$testing .= "," . $j;
// have error testing
$fileStize = filesize($rootFolder . $fileList[$j]);
if(!$fileStize) $map .= "-0"; // represents an error
else{
$files[] = file_get_contents($rootFolder . $fileList[$j]);
$map .= "-" . $fileStize;
}
}
$_SESSION['map'] = $map;
echo implode($files);
//echo $testing;
}else if($_GET['typ'] == 2){
echo $_SESSION['map'];
$_SESSION['map'] = "";
}
?>


following is the actionscript code:

var requestSite:String = "http://www.yoursite.com"

//to request files you include their name in the following array.
//if they are in a sub directory of the rootfolder(specified in php) you must include that also.
//currently the script will request three files - yourPicA.jpg, yourPicB.jpg, yourPicC.jpg
loadContent(new Array("yourPicA.jpg", "yourPicB.jpg", "yourPicC.jpg"))
var tim:int = 0

function loadContent(list:Array){
if(list.length == 0) return
var url:String = requestSite + "/sendObject.php?typ=1"
var request:URLRequest = new URLRequest(url);
var rv:URLVariables = new URLVariables();
rv.list = list.toString()
request.data = rv;
request.method = URLRequestMethod.POST;
//
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener(Event.COMPLETE, getMap);
urlLoader.addEventListener(ProgressEvent.PROGRESS, loadContentProgress);
tim = getTimer()
trace("timeA: "+tim)
urlLoader.load(request);
}//loadContent

function loadContentProgress(e:ProgressEvent){
trace("loaded: "+e.bytesLoaded)
}
var buildFile:ByteArray
//
function getMap(e:Event){
var timB:int = getTimer()
trace("timeB: "+timB)
trace("total time: "+(timB-tim))
buildFile = e.target.data
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, breakApartFiles);
//urlLoader.addEventListener(ProgressEvent.PROGRESS, loadContentProgress);
urlLoader.load(new URLRequest(requestSite + "/sendObject.php?typ=2"));
}

function breakApartFiles(e:Event){
//trace("buildFile.length:" +buildFile.length)
var breaks:Array = e.target.data.split("-")
if(breaks[breaks.length-1] == "") breaks.splice(breaks.length-1, 1) // for some reason the string will split at least once even if there is not '-' delimitator in the string
var startPos:int = 0
//trace("breaks:" +breaks)
for(var b:int = 0; b < breaks.length; ++b){
var lengthCount:int = breaks * 1
if(lengthCount <= 0) {
trace("There was an error with file number: "+(b+1)+". M0st likely the server could not find the file. Make sure the file address is correct")
continue //this file was was not loaded so we skip to the next one.
}
var newObject:ByteArray = new ByteArray()
//trace("startPos:" +startPos)
//trace("lengthCount:" +lengthCount)
newObject.writeBytes(buildFile, startPos, lengthCount)
var lod:Loader = new Loader()
lod.contentLoaderInfo.addEventListener(Event.COMPLETE,newObjectMade)
lod.loadBytes(newObject)
startPos += breaks * 1
}
}

function newObjectMade(e:Event){
addChild(e.target.content)
e.target.content.alpha = .5

}
Post Reply