help one form two submit button
Results 1 to 6 of 6

Thread: help one form two submit button

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    27

    help one form two submit button

    Hi,

    I am currently trying to do an upload and download. In the download there will be a compile feature.
    The idea is the file that I upload have the same name as the compiled file that will be downloaded.
    Right now, there is three button in the browser : browse button, upload button and download button.
    If I browse the file first .There will be some text on the browse button. If I press upload, the file is uploaded to my local server and some text that show the file is generated.
    If after this, I press compile, nothing happen.
    If I browse the file. Then the text appear. Then press download instead of upload. There will be exe file that is compiled.
    How should I arrange the code, so that the sequence will be I browse, upload, compile not the current one where I browse upload and browse compile?

    Below is my code:
    PHP Code:
    echo"<form action='' method=POST enctype=multipart/form-data>";
    echo
    "<input type=file name=uploadedfile>";
    echo
    "<input type=submit name=upload value=Upload>";
    echo
    "<input type=submit value=Download name=download>";
    echo
    "</form>";

    //START OF UPLOAD
    //check if upload is selected
    if(isset($_POST['upload']))
    {
        
    $ext="";
        
    //Check the availability of the file. empty evaluate to true if empty. check if it is the recent uploaded file. validate filename
        
    if((!empty($_FILES["uploadedfile"])) && ($_FILES['uploadedfile']['error'] == 0) && (is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) && (preg_match("/^[A-Za-z0-9].*\.[a-z]{0,3}$/",$_FILES['uploadedfile']['tmp_name'])))
        {
            
    //basename return component name. strtolower change to lowercase.
        
    $filename     strtolower    (basename($_FILES['uploadedfile']['name']));
            
    //substr return some string after dot
        
    $ext         substr    ($filenamestrrpos($filename'.') + 1);
            
            
    //check if file extension is c and less than 100000kb
        
    if (($ext=="c") && ($_FILES["uploadedfile"]["size"] < 100000) && ($_FILES["uploadedfile"]["type"] == "text/plain"))
        {
                
    //add the dot
                
    $ext=".".$ext;
                
    //file directory
                
    $newname=dirname(__FILE__).'/upload/'.$_FILES["uploadedfile"]["name"];
                
                
    //move_uploaded_file is php function for directory
                
    if ((move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$newname)))
                {
                    print(
    '<table>');
            print(
    '<tr><td width=100px>File Name</td><td width=100px>File Size</td><td width=100px>Last Modified</td></tr>');
            print(
    '<tr><td>'$_FILES['uploadedfile']['name'] . '</td><td>'round($_FILES['uploadedfile']['size']/1024,4) . ' Kb</td><td>'.date("d F Y H:i:s."filemtime($newname)).'</td></tr>');
            print(
    '</table>'); 
                }
                else
                {
                print(
    'error');
                }
        }
        else 
        {
        print(
    'Error: Only .c files <500Kb');
        }    
        } 
        else 
        {
            
    $filename=NULL;
        }
    }
    else if((isset(
    $_POST['download'])))
    {
        
    $source='upload\\'.$_FILES['uploadedfile']['name'];
        echo 
    $source;
        
    $destination 'upload\\' pathinfo($_FILES['uploadedfile']['name'], PATHINFO_FILENAME) . '.exe'
        echo 
    $destination;
        
    $output system('"C:\Program Files\CodeBlocks\MinGW\bin\gcc.exe" '.$source.' -O3 -o '.$destination);
        echo 
    $output;
             
        
    $outputfilename     strtolower(basename($destination));
        
    $extension         substr    ($outputfilenamestrrpos($outputfilename'.') + 1);
        if (
    $extension=="exe")
        {
        
    //check file exist
            
    if (file_exists($destination))
            {
            
    //header=send a raw http header will do a download
            
    header('Content-Description: File Transfer');
            
    //tell browser type of file downloaded
            
    header('Content-Type: application/octet-stream');
            
    //attachment = tell browser that going to link a file
            //the name will be the same as $file.
            
    header('Content-Disposition: attachment; filename='.basename($outputfilename));
            
    //to prevent cache either by proxy or client browser
            
    header('Content-Transfer-Encoding: binary');
            
    header('Expires: 0');
            
    //compared it with the original server
            
    header('Cache-Control: must-revalidate');
            
    //to make IE accept download
            
    header('Pragma: public');
            
    // header('Content-Length: ' . filesize($file));
            
    header('X-Sendfile :'.$outputfilename);
            
    //erase output buffer
            
    ob_clean();
            
    //flush output buffer
            
    flush();
            
    //output a file
            
    readfile($destination);
            exit;
            }
        }
        else
        {}
    }
    else
    {
        
    //error
    }
    //END OF UPLOAD 
    Thanks in advance

  2. #2
    Senior Member
    Join Date
    Aug 2008
    Location
    London, UK
    Posts
    753
    The wya you're trying to download, the server doesn't know about the file you've just uploaded, it's stateless. Why don't you merge the steps, so that it uploads the file, and then compiles it and offers a link or something to download it? Or, once the file has been uploaded, offer a link which is really a link to the script that manages the compiling. I'd do this if you expect more uploads only than uploads & compiles.
    Ashley Sheridan
    www.ashleysheridan.co.uk

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    27
    I have tried to merge the steps and it works. any file that I upload will be compiled and downloaded in one submit.
    However, I want to split this step into two submit button.
    Is it possible?
    I am new to php.

  4. #4
    Senior Member
    Join Date
    Aug 2008
    Location
    London, UK
    Posts
    753
    You'll need to pass the name of the file you want to compile then somehow when the second submit is pressed. I think you're having a little trouble understanding the server/client process.

    The first step is this:
    1. You request the page
    2. Server sends back the HTML for the page with that form in


    The second step is this:
    1. You browse your system for the file to upload
    2. You hit submit which sends the form and file to the server
    3. The server saves the file you uploaded and sends back the page with the form in again and a saved message


    The third step is this:
    1. You click the compile button in the form which sends a request to the server
    2. The server receives an empty form and wonders what file you want compiled because you didn't tell it


    You see, HTTP is stateless, which means that the server can happily forget about you after it sends each page you view. You need to let the server know what file you want to compile. You could do this in one of a few ways:
    • When the server saves the upload from the first time and sends back the form, add a hidden input element to it with the file
    • Send back a link instead of the form (why do you need to show the form once the file is saved?) which just points to the compile script with the id of the file you uploaded
    • Instead, change the two buttons on the original form. Make one a save, and the other save & compile
    Ashley Sheridan
    www.ashleysheridan.co.uk

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    27
    Thanks.
    I have successfully implemented your third suggestion.
    save with save and compile.
    In the expense of adding another browse button for that feature.

    My current implementation
    one "File to Upload" browse button
    Then one "Upload File" submit button
    for the uploading feature

    one "File to Compile" browse button
    Then one "Download button" submit button
    for the downloading feature

    In the downloading feature, I basically upload then compile then download.
    Just adding the code that I already implement in the "Upload File" submit button with compiling and downloading.

    Is my understanding correct?

  6. #6
    Senior Member
    Join Date
    Aug 2008
    Location
    London, UK
    Posts
    753
    Yes, and if it allows you to do what you wanted then at least my understanding of your problem was correct to a point too!
    Ashley Sheridan
    www.ashleysheridan.co.uk

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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