Link to home
Start Free TrialLog in
Avatar of sherlock5
sherlock5

asked on

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

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!
Avatar of psimation
psimation
Flag of South Africa image

what is the filename of the doc before you try to upload?

Avatar of sherlock5
sherlock5

ASKER

it has the .doc extension.
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.
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

ASKER CERTIFIED SOLUTION
Avatar of sherlock5
sherlock5

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial