homepage Welcome to WebmasterWorld Guest from 54.163.70.249
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Need help writing/echoing file size to document.
Need help writing/echoing file size to document.
fredfletcher




msg:4419169
 8:20 pm on Feb 18, 2012 (gmt 0)

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 77.25 MB
echo "\n";
echo formatBytes($filesize2,0); //returns 81,000,000 B
echo "\n";
echo formatBytes($filesize2,1); //returns 79,102 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.....";
}


?>

 

fredfletcher




msg:4419214
 12:05 am on Feb 19, 2012 (gmt 0)

to add, the server it's on, is using PHP4, nothing I can do about that for now.

enigma1




msg:4419295
 11:22 am on Feb 19, 2012 (gmt 0)

You don't need to do pow in this case you could do a right bit-shift. Calculate the result of the shift operation if it's higher than the next unit in the array and store the index. Something like

while($b >= 0x400){
$c++;
$b >>= 0x400;
$b++;
}

At the end of the loop the var $c will hold the index into the unit array which you can reference to append the appropriate string.

And what's the problem with PHP4?

enigma1




msg:4419317
 2:46 pm on Feb 19, 2012 (gmt 0)

Actually the shift operation should be 10 bits not 1024. Should read
$b >>= 10;

fredfletcher




msg:4419371
 7:26 pm on Feb 19, 2012 (gmt 0)

Thanks (enigma1), I replaced the code with the following, and is working nicely:

if( $filesize2 < 1024 )
$filesize2 = round(($filesize2 / 1024),0) . ' Bytes';

elseif( $filesize2 >= 1024 && $filesize2 < 104857 )
$filesize2 = round(($filesize2),0) . ' Bytes';

elseif( $filesize2 >= 104857 && $filesize2 < 1048576 )
$filesize2 = round(($filesize2 / 1024),2) . ' KB';

elseif( $filesize2 >= 1048576 )
$filesize2 = round(($filesize2 / 1048576),2) . ' MB';

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved