$f="../mts/sites/default/files/test.doc";
//in this way i am able to find line count,but i am not getting how i can give folder path for line counting..if i give path as $safe_filename
and $target_path.$safe_filename
it's coming file content not opening. Please check the code and help me out thanks in advance
<?php
$nid = 1;
$teaser = false;
// Load node
$node = node_load(array('nid' => $nid));
// Prepare its output
if (node_hook($node, 'view')) {
node_invoke($node, 'view', $teaser, false);
}
else {
$node = node_prepare($node, $teaser);
}
// Allow modules to change content before viewing.
node_invoke_nodeapi($node, 'view', $teaser, false);
// Print
print $teaser ? $node->teaser : $node->body;
$target_path = "../mts/sites/default/files/ourfiles/";
//$myfile = basename( $_FILES['uploadedfile']['name']);
$safe_filename = preg_replace(
array("/\s+/", "/[^-\.\w]+/"),
array("_", ""),
trim($_FILES['uploadedfile']['name']));
$target_path = $target_path.$safe_filename;
if(file_exists($target_path))
{
echo "<script language=\"javascript\">";
echo "window.alert ('File al开发者_JAVA百科ready exist');";
echo "//--></script>";
}
elseif(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "<script language=\"javascript\">";
echo "window.alert ('File uploaded succesfully');";
echo "//--></script>";
/*
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
*/
}
$con = mysql_connect("localhost","mts","mts");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Create table
mysql_select_db("mts", $con);
$f="../mts/sites/default/files/test.doc";//in this way i am able to find line count,but i am not getting how i can give folder path for line counting
// count words
$numWords = str_word_count($str)/11;
echo "This file have ". $numWords . " words";
echo "This file have ". $numWords . " lines";
mysql_query("INSERT INTO mt_upload (FileName,linecount, FilePath,DateTime)
VALUES ('".$safe_filename."','".$numWords."', '".$target_path.$safe_filename."',NOW())");
// Execute query
mysql_query($sql,$con);
mysql_close($con);
?>
emphasized text
This is not a very safe way of doing things. If two users submitted files with the same filename, they would overwrite each other. This limits the user to a unique filename, which after a while of running a system becomes a problem. Not to mention that the user would be quite puzzled if he ever got such a message.
A much better solution is to generate your own filename for every file. You could make a long random string, or maybe take the mysql_insert_id()
for the inserted record in mt_upload table. Then you would also not need to mess around with regexps, and the whole thing would be much simpler. In the mt_upload table you can keep the old filename if you want to show it to the user or something.
If that's the exact code, then the problem is that you call str_word_count()
on $str
but don't appear to set $str
anywhere.
Function to count lines in all the files residing in a certain directory
function linecount_dir($dir) {
$ig = array('.','..');
$d = dir($dir);
while (false !== ($entry = $d->read())) {
if (!in_array($entry,$ig)) {
$total += linecount($d->path . "/$entry");
}
}
$d->close();
return $total;
}
Function to count lines
define(BLOCK_SIZE,131072);
function linecount($filename) {
$f = fopen($filename,'r');
while (!feof($f)) {
$d = fread($f,BLOCK_SIZE);
$rcount += substr_count($d, "\n");
$ncount += substr_count($d, "\r");
}
fclose($f);
return (($rcount >= $ncount) ? $rcount : $ncount);
}
Applied to your code
$f="../mts/sites/default/files/test.doc";//in this way i am able to find line count,but i am not getting how i can give folder path for line counting
// count words
$numWords = str_word_count($str)/11;
// count lines
$numLines = linecount($f);
echo "This file have ". $numWords . " words";
echo "This file have ". $numLines . " lines";
$f="../mts/sites/default/files/".$safe_filename;
精彩评论