Hello, I have tried my best in achieving this, and for some reason, it's not working.
What I am trying to do, is to write to file and echo on screen, a converted version of how many bytes/megabytes etc. a file has been uploaded, and echoed on screen upon success, instead of the standard e.g. 101355 bytes.
Here is my present code, and any help would be appreciated, thank you.
<?php
require('security.php');
//set where you want to store files
//in this example we keep file in folder upload
//$HTTP_POST_FILES['ufile']['name']; = upload file name
// DATE STAMP FUNCTION start
$today = mktime(0, 0, 0, date("m"), date("d"), date("y"));
// DATE STAMP FUNCTION end
$path1= "../folder_upload/".$HTTP_POST_FILES['ufile']['name'][0];
copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
echo "File Name: ".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>";
echo "File Size: ".$HTTP_POST_FILES['ufile']['size'][0]."<BR/>";
echo "File Type: ".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>";
echo "<P>";
///////////////////////////////////////////////////////
// Use this code to display the error or success.
$filesize1=$HTTP_POST_FILES['ufile']['name'][0];
$filesize2=$HTTP_POST_FILES['ufile']['size'][0];
if($filesize1 != 1)
{
echo "<br><b>All files have been successfully uploaded.</b>";
// filesize test
function formatBytes($b,$p = null) {
/**
*
* returns formatted number of bytes.
* two parameters: the bytes and the precision (optional).
* if no precision is set, function will determine clean
* result automatically.
*
**/
$units = array("B","kB","MB","GB","TB","PB","EB","ZB","YB");
$c=0;
if(!$p && $p !== 0) {
foreach($units as $k => $u) {
if(($b / pow(1024,$k)) >= 1) {
$r["bytes"] = $b / pow(1024,$k);
$r["units"] = $u;
$c++;
}
}
return number_format($r["bytes"],2) . " " . $r["units"];
} else {
return number_format($b / pow(1024,$p)) . " " . $units[$p];
}
}
echo "\n";
echo formatBytes($filesize2); //returns in MB
echo "\n";
echo formatBytes($filesize2,0); //returns in B
echo "\n";
echo formatBytes($filesize2,1); //returns in kB
// filesize test
// start writing to file
$lines = file('uploaded.txt');
$fopen = fopen("uploaded.txt", "w+");
fwrite($fopen, ("<P align=\"justify\" class=\"para\"><b>")."");
fwrite($fopen, $_POST["date1"]."");
fwrite($fopen, ("<br>")."");
fwrite($fopen, $_POST["header1"]."");
fwrite($fopen, ("</b><br>")."");
fwrite($fopen, $_POST["description1"]."");
fwrite($fopen, ("<br><br>")."");
//GOOD ONE
fwrite($fopen, ("<a href=\"$path1\"><img border=\"0\" src=\"/img/icon1.gif\" align=\"left\" hspace=\"4\" align=\"absmiddle\" width=\"16\" height=\"16\">")."");
fwrite($fopen, ("Posted file</a> - Document size: $filesize2 in bytes/kb/or megabytes etc")."\r\n");
//date stamp
fwrite($fopen, "\n- Posted on ".date("D m/d/Y", $today)."");
fwrite($fopen, (" (Automatically generated)")."");
// Horizontal line
fwrite($fopen, ("<HR style=\"border: 1px dotted #C0C0C0\" color=\"#263E72\" align=\"justify\">")."");
// 1 empty line below <HR>
fwrite($fopen, ("<br>")."");
foreach ($lines as $line) { fwrite( $fopen, "$line"); }
// find and replace function start
echo "\nSuccess writing to file.";
fclose($fopen);
// end writing to file
// find and replace function start
// open file to find \ and other weird characters
include 'find_and_replace.php' ;
}
else {
echo "ERROR.....";
}
?>