Solved

php script failing with converted word perfect file (converted to word doc)

Posted on 2008-10-21
5
219 Views
Last Modified: 2012-05-05
For some reason my upload script is failing with a particular file, a Word Perfect file that was converted to a doc file (97-2003). It throws off the whole system for some reason. I wish I could attach it but the information is private. The only guess that I have is that somehow the metadata is bad. However, I looked at it in windows explorer and it everything looks fine. My guess is that it has something to do with this line:

        //Attempt to move the uploaded file to it's new place
        copy($_FILES['File']['tmp_name'],"$folder".$_FILES['File']['name']);
      
I opened the file in Word and then resaved it. After that, it worked fine. Is there some way to catch this? Has anyone ever experienced this before?

Thanks in advance for your help!
0
Comment
Question by:sherlock5
  • 4
5 Comments
 
LVL 17

Expert Comment

by:psimation
Comment Utility
what is the filename of the doc before you try to upload?

0
 

Author Comment

by:sherlock5
Comment Utility
it has the .doc extension.
0
 

Author Comment

by:sherlock5
Comment Utility
i will have to post my script tomorrow... i just tried a simple script(http://www.phpeasystep.com/phptu/1.html) and i was able to upload the file... so something is wrong with my code. it is weird though that it worked for all of the files except for this one. i will post my code tomorrow... thanks for responding psimation.
0
 

Author Comment

by:sherlock5
Comment Utility
sorry for the amount of time it took to post my code...

once again, for some reason i'm having trouble uploading a doc file that was saved in wordperfect. this script works for everything else but seems to fail with this one file. i also tried this script and i was able to upload the doc file (http://www.phpeasystep.com/phptu/1.html). i figure that the problem is more with my code... which is very messy, i know, and i'm willing to listen to how i can improve it.

the problem that occurs when i load the wordperfect/word doc file is that all of the folders in the files folder are listed like this:

id (path/to/files//id)  4.00KB   remove
.
.
.

... and all of the hidden variables in the form disappear after the post or upload.

so, if anyone can help me, that would be swell!
<?

########### Connection to database ##################################################

connection goes here...

#####################################################################################
 
 

##### file functions #####

function fileDelete($filepath,$filename) {

$success = FALSE;

if (file_exists($filepath.$filename)&&$filename!=""&&$filename!="n/a") {

unlink ($filepath.$filename);

$success = TRUE;

}

return $success;

}
 
 

function countFiles($strDirName)

{

if ($hndDir = @opendir($strDirName))

{

$intCount = 0;

while (false !== ($strFilename = @readdir($hndDir)))

{

if ($strFilename != "." && $strFilename != "..")

{

$intCount++;

}

}

@closedir($hndDir);

}

else

{

$intCount = -1;

}
 

return $intCount;

}
 

function size($size, $short = false) {

  if ($short) {

    $unit = array(

      'B', 'KB', 'MB', 'GB', 'TB',

      'PB', 'EB', 'ZB', 'YB'

     );

  } else {

    $unit = array(

      'bytes', 'kilobyte', 'megabyte',

      'gigabyte', 'terabyte', 'petabyte',

      'exabyte', 'zettabyte', 'yottabyte'

     );

  }
 

  $base = floor(log($size, 1024));

  return round($size / pow(1024, $base), 2).' '.$unit[$base];

}
 
 

if($_REQUEST["id"]) { 

$id = $_REQUEST["id"]; 
 

} else { 

$id = $_REQUEST["ID"];

}
 

if($id) {

// check to see if paper has been published

$s7 = "SELECT T FROM W WHERE ID=$id ORDER BY R DESC LIMIT 1";

$r7 = @mysql_query($s7);

$c7 = @mysql_fetch_row($r7);

$live = "$c7[0]";

}
 

if($_REQUEST["live"] == "live") {

echo "<font color=red>We are sorry but you can no longer edit this...</font>";

} else {
 

?>

<h1>Form</h1>
 

<p><a href="form.php?Submit=Submit&Code=<?=$_REQUEST["Code"];?>&Email=<?=$_REQUEST["Email"]?>"\">Go back.</a></p>

<form name="form" method="post" enctype="multipart/form-data" action="<? echo $_SERVER['PHP_SELF'];?>">

	<table border="0" cellspacing="2" cellpadding="2">

	<tr>

	<th>Attach Files</th>

	</tr>

	<tr><td><p>Upload files here...</p> Fields with a <span class="error">*</span> are required.<br></td>

	</tr>

	<tr class="even">

	<td>Upload File:<span class="important"> *</span></td>

	</tr>

	<tr class="even">

	<td><input type="file" name="File" size="50"><input type="hidden" name="ID" value="<?=$id;?>"></td>

	</tr>

	<tr>

	<td><p><input name="Upload" type="submit" class="formbutton" value=" Upload "></p></td>

	</tr>

<?
 

	  $r = $_REQUEST["delete"];

	  if ($_FILES['File']['name'] == "" && ($r[0] == "A" || $r == "") && isset($_REQUEST["Upload"])) {

	  echo "<span class=\"error\"><img src=\"error.gif\" />You did not submit a file!</span>";

	  } elseif(!preg_match("/(doc)/i",$_FILES['File']['name']) && ($r[0] == "A" || $r == "") && isset($_REQUEST["Upload"])) {

	  echo "<span class=\"error\"><img src=\"error.gif\" />The file you have submitted is not a doc!</span>";

	  } elseif ($_FILES['File']['size'] > 31457280 && ($r[0] == "A" || $r == "") && isset($_REQUEST["Upload"])) {

	  echo "<span class=\"error\"><img src=\"error.gif\" />The file is larger than 30MB...</span";

	  }
 

if (preg_match("/(doc)/i",$_FILES['File']['name']) && $_FILES['File']['size'] < 31457280 && isset($_REQUEST["Upload"]) && $_FILES['File']['size'] > 0) {
 
 
 

$cf = countFiles("../path/to/files/$id/");
 

if( $cf < 31 && $_REQUEST["Upload"]) { 

$folder = $_SERVER['DOCUMENT_ROOT']."/path/to/files/$id/";
 

if(!(is_dir($folder))) {

mkdir($folder,0777);

}
 

      //Check if the file with the same name is already exists on the server

      if (!file_exists($folder.$_FILES['File']['name'])) {

        //Attempt to move the uploaded file to it's new place

        copy($_FILES['File']['tmp_name'],"$folder".$_FILES['File']['name']);

		} else {

		echo "<span class=\"error\"><img src=\"error.gif\" />The file you are uploading already exists!</span>";

		}

		

} else {

echo "<span class=\"error\"><img src=\"error.gif\" />You have reached the limit of uploads...</span>";

}
 

}
 

if ($_REQUEST["DeleteFiles"]) {

// remove files

$x = $_REQUEST["delete"];

for ($i=0; $i < count($x); $i++) {

		$filepath = $_SERVER['DOCUMENT_ROOT']."/path/to/files/$id/";

		fileDelete($filepath,$x[$i]); 

			}

}
 
 
 

echo "<tr>\n\r<td colspan=\"2\">\n\r";

echo "<h3>Uploaded Files</h3>\n\r<code>";
 

				   $path = $_SERVER['DOCUMENT_ROOT']."/path/to/files/$id/";

				   $dh = @opendir($path);

				   

				   while (false !== ($file=@readdir($dh)))

				   {

				   if (substr($file,0,1)!="." && !stristr($file, "filter"))

				   $files[]=array(filemtime($path.$file),filesize($path.$file),$file);

				   }

				   @closedir($dh);

				   if ($files)

				   {

				   rsort($files);

				   

				   foreach ($files as $file)

				   echo "<a href=\"../path/to/files/$id/$file[2]\" target=\"_blank\">$file[2]</a> &nbsp;<em>".number_format(($file[1]/1024),2)."KB</em> &nbsp;<input name=\"delete[]\" type=\"checkbox\" value=\"$file[2]\"> <sup><span class=\"error\">remove</span></sup><br>\n\r";

				   }

				

		echo "\n\r<br><input name=\"DeleteFiles\" type=\"submit\" class=\"formbutton\" value=\" Remove \"></code>\n\r</td></tr>";

		echo "<tr>\n\r<td align=\"center\" colspan=\"2\">\n\r";

		echo "</td></tr></table>";

		echo "<input name=\"Emails\" type=\"hidden\" value=\"$_REQUEST[Email]\">";

		echo "<input name=\"Code\" type=\"hidden\" value=\"$_REQUEST[Code]\">";

		echo "<input name=\"live\" type=\"hidden\" value=\"$live\">";

		echo "</form>";

}
 

		?>

Open in new window

0
 

Accepted Solution

by:
sherlock5 earned 0 total points
Comment Utility
i fixed this a while ago... turns out i just needed this in an htaccess file:

php_value post_max_size 30M
php_value upload_max_filesize 30M
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP Startup Error 12 29
Trouble parsing soap xml result 3 26
Page showing diff display 4 19
wordpress issue 2 15
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now