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]
echo"";
echo"";
echo"";
echo"";
echo"";
//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
[/php]
Thanks in advance