PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Dec 14, 2019 8:06 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Fri Apr 13, 2012 3:04 am 
Offline
Forum Newbie

Joined: Wed Apr 04, 2012 8:54 pm
Posts: 22
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:
Syntax: [ Download ] [ Hide ]
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        ($filename, strrpos($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        ($outputfilename, strrpos($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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group