Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts

    How to create single install file from multiple sources

    Hi all,

    I've written a PHP/Javascript app where all the code is in one file. It's quite a small program and, basically, to handle different functions etc the code calls itself with a function code in the querystring. The start of the code then looks at this function code (if it exists) and decides which function to call etc... There is also a config.php file.

    It works really well.

    Now, I want to distribute this to users who can just about upload a file via FTP so, ideally, i just want to give them one file to upload.

    So far, I have written a PHP install program that has the code for the main program literally tacked on to the end of it.

    When the "install.php" runs it asks for a new password, encrypes it and then creates a config.php file (with the encrypted password and key in it) and then it copies itself line by line (from the point where the install section stops of course) to another file called main.php. Basically there is a comment line in the install.php that says "cut here" - The program searches for it and then starts copying data after that point. I have done it this way to make it easier to add stuff to the install program.

    When it is finished, main.php is run. Main.php checks to see if 'install.php' exists and, if so, deletes it.

    It all works like a charm - I have a single install.php file that creates main.php and config.php.

    Now, I want to include a small graphic header and footer that will be displayed via the main.php program. I guess they could be called header.jpg and footer.jpg.

    Is there any way of incorporating these jpg's as data in the 'install.php' file so that it can create the graphic .jpg files at install time in order to still keep the install.php as one file?

    If this was BASIC, I would, basically (lol), store the graphic in DATA statements and then use the READ statement to read the bytes and write them to a file.

    Any help would be most appreciated please. I think it could possibly be done with something similar to the following :

    Code:
    $data=array(21, 112, 144, 255, etc, etc, etc,);
    foreach($data as $imagebyte){
       write $imagebyte to file 
    }
    But there must be a more elegant way to do it?
    Last edited by spman; 11-15-2011 at 06:50 PM. Reason: EDIT: Code Tags

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    You can pretty much do the same thing with PHP. In your install.php file you will just need to insert the file_get_contents() results from whatever image(s) you want to include into the appropriate variable(s) and then create your new image file(s) and write the stored contents into the image file(s) on installation (using file_put_contents()).

    It will bloat your install file, but no more than your overall download packet would have been anyway if it was a .tar or .zip archive.


    EDIT:

    Pseudo-code:

    First, you need to create your install.php file, so we will make a file called get_contents.php:
    PHP Code:
    <?php
    print file_get_contents('path/to/image_01.jpg');
    ?>
    Then visit the page in-browser and copy the content results. Paste them into your install file something like this:

    install.php:
    PHP Code:
    <?php

    //blah blah blah

    //image 1
    $images[0]['name']='image_01.jpg';
    $images[0]['contents']='YOUR PASTED FILE CONTENTS GO HERE';

    //images 2 and beyond done the same way here...
    $images[1]['name']='image_02.jpg';
    $images[1]['contents']='ANOTHER BLOCK OF PASTED FILE CONTENTS GO HERE';

    //blah blah blah

    //now to create your image files on the server and load their contents at installation time:
    foreach($images as $key=>$value){
        
    file_put_contents "/path/to/desired/folder/".$value['name'] , $value['contents'] );
    }

    //blah blah blah

    //end of script

    ?>
    That's the basic idea, anyway.
    Last edited by Rowsdower!; 11-15-2011 at 07:01 PM. Reason: added some code...
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    spman (11-15-2011)

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Rowsdower! View Post
    You can pretty much do the same thing with PHP. In your install.php file you will just need to insert the file_get_contents() results from whatever image(s) you want to include into the appropriate variable(s) and then create your new image file(s) and write the stored contents into the image file(s) on installation (using file_put_contents()).

    It will bloat your install file, but no more than your overall download packet would have been anyway if it was a .tar or .zip archive.
    Thanks for the reply - much appreciated.

    So, I can get how I can read the jpg into a string but how would I actually encode the contents of that string into the install.php file?

    Basically - lets say the install program is as follows :

    Code:
    <?PHP
    $path = dirname(__FILE__) . '/' ;
    if (file_exists($path . 'main.php')) {
    	unlink ($path . 'main.php');
    }
    
    $oldfile=fopen($path . 'install.php','r');
    $newfile=fopen($path . 'main.php','w');
    $cuthere=0;
    		
    while(!feof($oldfile)) { 
         $fdata = fgets($oldfile);
         if (substr($fdata,0,14) == "<!--cuthere-->") {
    		$cuthere=1;
    		$fdata=fgets($oldfile);
    		$fdata=fgets($oldfile);
    	}
    	if ($cuthere==1) {
    		fputs($newfile,$fdata);
    	}
    }
    		
    fclose ($oldfile);
    fclose ($newfile);
    unlink ($path . 'install.php');
    header("Location: main.php");
    break;
    }
    ?>
    <html>
    <head><title>Install Program</title>
    </head>
    <style 
    <body>
    Installing...
    </body>
    <!--cuthere-->
    </html>
    <?php 
    echo "This is the main program"
    ?>
    How would I incorporate the data for the jpg into that program rather than providing it as a separate file?

    I think I am missing something here but not sure what LOL!

  • #4
    New Coder
    Join Date
    Nov 2011
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    LOL! You were editing your post at the same time as I created mine - Spotted the code now so thank you very much for that

  • #5
    New Coder
    Join Date
    Nov 2011
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Ok - I have tried it and I can't get it to work at the mo.

    Here is what I have done so far - Create a simple gradient header jpg and called it header.jpg.

    Wrote the following code to print it :

    Code:
    <?php
    print file_get_contents('header.jpg');
    ?>
    Run this and I get it printed to the Firefox browser window. Select it and copy it and then edit another file called writeheader.php in notepad. Paste it into the string so that it looks like this :

    Code:
    <?php
    
    $image='JFIFHHC     C   
    
    Z?i<( 4BD( 4BD( 4BD( 4BD( 4BD( 
    
    4BD( 4BD( 4BD( 4BD( 4BD( 4BD( 4BD( 4BD( 4BD( 4BD( 4BD( 4BD( 4BD( 
    
    4BD( 4BD( 4BD( 4BD( 4BD( 4D)JBD( 4BD( 4BD( 4BD( 4BD( 4BD( 4BD( 4BD( 
    
    4BD( 4BD( 4BD( 4BD( 4BD( 4BD( 4BD( 4BD( 4BD( 4BD( 4BD( 4BD( 4BD( 
    
    4BD( 4BD( 4BDFٴ,4BD,4BD,4BD,4BD,4BD,4BD,4BD,4BD,4BD,
    
    4BD,4BD,4BD,4BD,4BD,4BD,4BD,4BD,4BD,4BD,4BD,4BD,4BD,
    
    4BD,4D-NfYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!
    
    fYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!fY
    
    #LRh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!
    
    FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh"4ϥ!FQh!FQ
    
    h!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQ
    
    h!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQ!LRh!FQh!FQh!FQh!
    
    FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!
    
    FQh!FQh!FQh!FQh!FQh!FQh!FQh"6ͥ!fYh!fYh!fYh!fYh!fYh!fY
    
    h!fYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!
    
    fYh!fYh!fYh!fYh!fYh!fY!lRh!fYh!fYh!fYh!fYh!fYh!fYh!
    
    fYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!fYh!fY
    
    h!fYh!fYh!fYh!fYh"4ͥ!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQ
    
    h!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQ
    
    h!FQh!FQh!FQ#LRh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!
    
    FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!FQh!
    
    FQh"h; ';
    
    file_put_contents ( "head2.jpg" , $image);
    
    
    ?>
    Run this program and it outputs a new file called head2.jpg BUT when I try to load it in the browser I get a message telling me it can't display it as there are errors in the image.

    Any ideas please?

  • #6
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Sorry, I haven't actually done this before so I missed a crucial step in my pseudo-code. You need to base64 encode your image, store it that way in your install.php script, and then base64 decode the result and use that to populate the image file.

    To get the base64 encoded image contents:
    PHP Code:
    print base64_encode(file_get_contents('path/to/image_01.jpg')); 
    And then to make an image out of them once again:
    PHP Code:
    foreach($images as $key=>$value){ 
        
    file_put_contents "/path/to/desired/folder/".$value['name'] , base64_decode($value['contents']) ); 

    I think that will do the job...
    Last edited by Rowsdower!; 11-15-2011 at 09:10 PM.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #7
    New Coder
    Join Date
    Nov 2011
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Rowsdower! View Post
    Sorry, I haven't actually done this before so I missed a crucial step in my pseudo-code. You need to base64 encode your image, store it that way in your install.php script, and then base64 decode the result and use that to populate the image file.

    To get the base64 encoded image contents:
    PHP Code:
    print base64_encode(file_get_contents('path/to/image_01.jpg')); 
    And then to make an image out of them once again:
    PHP Code:
    foreach($images as $key=>$value){ 
        
    file_put_contents "/path/to/desired/folder/".$value['name'] , base64_decode($value['contents']) ); 

    I think that will do the job...
    That works perfectly! Once again, Thank you


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •