1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need PHP Programmer To Setup Simple Work

Discussion in 'PHP & Perl' started by gimme4free, Dec 18, 2008.

  1. gimme4free

    gimme4free Executive VIP Jr. VIP Premium Member

    Joined:
    Oct 22, 2008
    Messages:
    1,907
    Likes Received:
    1,938
    Home Page:
    I need someone to create a page for me.

    On this page there will be a form such as, type in your website, then your second website, then link X, then link Y etc.

    These once submitted need to be put into a few different PHP files that are inside different folders and then ZIP the lot up and download instantly.

    This needs to work as if a lot of users are doing this at the same time so people dont get hold of others files.

    If anyone can do this please could you PM me with a price, it is quite a simple job in my opinion but I don't know how to do the ZIP up bit.
     
  2. drdankmendez

    drdankmendez Regular Member

    Joined:
    May 30, 2008
    Messages:
    218
    Likes Received:
    332
    Location:
    In front of my computer
    Im not really interested in taking this as a job but in any case I'd like to share the basic method to accomplish this as to help anyone who finds this thread, if noone has PMed you already about doing this, and the code samples below do not help you feel free to PM me.

    1. Create a php file (phpFilename.php) and initiate a session with a random ID using somthing like:

    Code:
    <?php
    session_start();
    $sess_id = rand(100000, 999999999);
    $_SESSION['rand_id'] = $sess_id;
    ?>
    
    2. Build the form into the file (phpFilename.php).

    Code:
    <form action="formAction.php" method="post">
    <table>
    	<tr>
    		<td>Website 1</td>
    		<td><input type="text" name="website1" /></td>
    	</tr>
    	<tr>
    		<td>Website 2</td>
    		<td><input type="text" name="website2" /></td>
    	</tr>
    	<tr>
    		<td>Link X</td>
    		<td><input type="text" name="linkX" /></td>
    	</tr>
    	<tr>
    		<td>Link Y</td>
    		<td><input type="text" name="linkY" /></td>
    	</tr>	
    	<tr>
    		<td colspan="2">
    			<input type="submit" value="Submit!" />
    		</td>
    	</tr>
    </table>
    </form>
    
    3. Create another php file (formAction.php) and add the functions to get the form POST
    Code:
    <?php
    session_start();
    $website1 = $_POST['website1'];
    $website2 = $_POST['website2'];
    $linkX = $_POST['linkX'];
    $linkY = $_POST['linkY'];
    ?>
    
    4. Now it gets a bit tricky. There are a ton of ways to accomplish writing the POST data to specific parts of the PHP script. One of the simplest is to include the whole php script into the (formAction.php) file as a PHP variable with the replacement part labeled, then replace the labels with the POST data and write the whole thing to a new file. Start with including the original script with labels.
    Code:
    <?php
    $original_script = "<?php";
    $original_script .= "echo 'Website 1 = {WEBSITE_1}';";
    $original_script .= "echo 'Website 2 = {WEBSITE_2}';";
    $original_script .= "echo 'Link X = {LINK_X}';";
    $original_script .= "echo 'Link Y = {LINK_Y}';";
    $original_script .= "?>";
    ?>
    
    5. Next add the replacement code to (formAction.php).
    Code:
    <?php
    $original_script = str_replace('{WEBSITE_1}', $website1, $original_script);
    $original_script = str_replace('{WEBSITE_2}', $website2, $original_script);
    $original_script = str_replace('{LINK_X}', $linkX, $original_script);
    $original_script = str_replace('{LINK_Y}', $linkY, $original_script);
    ?>
    
    6. Next in (formAction.php) use PHP's file functions to create a new file, and write the original script with the replacements added (the new file being made will be stored in a new directory named after the unique session ID, this way multiple people can use this script without comprimising thier data).
    Code:
    <?php
    mkdir('/tmp_dirs/'.$_SESSION['rand_id']);
    $dir_name = '/tmp_dirs/'.$_SESSION['rand_id'];
    $final_filename = 'outputFile.php'
    $final_file = fopen($dir_name.'/'.$final_filename, 'w');
    fwrite($final_file, $original_script); 
    fclose($final_file); 
    ?>
    
    7. Ok at this point the user has submitted thier data, a new php file has been created with thier data in a unique folder. Now we need to add this new file to a zip file along with whatever other files are needed. At this point if it hasn't gotten tricky enough for you, prepare yourself to use the ZIP functions of PHP (NOTE: this requires for ZZIPlib (PHP 4.0), or zlib (PHP 5.0) libraries to be installed with PHP) (formAction.php).
    Code:
    <?php
    $zip = new ZipArchive();
    $zip_name = '/zips/'.$_SESSION['rand_id'].'.zip';
    $filename1 = '/tmp_dirs/'.$_SESSION['rand_id'].'outputFile.php';
    $filename2 = '/otherDirectory/filename.php';
    $filename3 = '/otherDirectory/filename2.php';
    
    if ($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE) {
        exit("cannot open <$zip_name>n");
    }
    $zip->addFile($filename1,$filename1);
    $zip->addFile($filename2,$filename2);
    $zip->addFile($filename3,$filename3);
    $zip->close();
    ?> 
    
    8. Ok so the zip file has been created, now we need to prompt the user to download the file. This can be accomplished by redirecting the user to the zip file. (formAction.php)
    Code:
    <?php
    header("Location: ".$zip_name);
    ?>
    

    I would like to mention now that these samples are nothing more than samples, I have not tested this particular code, I have used similar functions in other projects but since this is untested I do not assure its function. Also there are many things that should be added to these samples such as the removal of files after use, checking to make sure files were created correctly, form validation, etc.
     
    • Thanks Thanks x 4
    Last edited: Dec 24, 2008
  3. gimme4free

    gimme4free Executive VIP Jr. VIP Premium Member

    Joined:
    Oct 22, 2008
    Messages:
    1,907
    Likes Received:
    1,938
    Home Page:
    Thanks a lot, will try this out