FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


File upload problem :'(





bladesage
Okay, now that I got that last MySQL thing working okay, another project is causing me trouble. I need to make a file upload script, which I did, but it keeps having more and more errors.

My page to upload looks like this (standard XHTML)
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Upload a template!</title>
<script type="text/javascript">

function checkfiles()
   {
   var fname = document.frm.filename.value;
   var extstarts = fname.indexOf('.');
   var lngth = fname.length;
   var extension = fname.substring(extstarts, lngth);
   if(extension != ".pdf" && extension != ".pub" && extension != ".ppt" && extension != ".doc" && extension != ".xls")
      {
      alert("This is not an allowed filetype!\n\n" + extension);
      return false;
      }
   if(document.frm.desc.value.length > 600)
      {
      alert("This description is too long!  They may only be up to 600 characters.");
      return false;
      }
   return true;
   }

</script>
</head>

<body>

<form name="frm" action="upload.php" enctype="multipart/form-data" onsubmit="return checkfiles()">

Your name:<br />
<input type="text" name="name" size="30" maxlength="30" /><br /><br />

A description of your template:<br />
<textarea name="desc" cols="120" rows="4"></textarea><br /><br />

File:<br />
<input type="file" name="filename" size="100" /><br /><br />

<input type="submit" value="Upload!!" />









</form>

</body>
</html>


The script to handle this looks like this
Code:
<?php


$fname = $_FILES['filename']['name'];
$fpath = $_FILES['filename']['tmp_name'];
$fsize = $_FILES['filename']['size'];
$name = $_POST['name'];
$desc = $_POST['desc'];

if($fsize > 1073741824)
   {
   die("File size is too big.  Only up to 1 Megabyte is allowed.");
   }

$link = mysql_connect("localhost", "teenzine_file", "pass");
mysql_select_db("teenzine_uploads") or die("ERROR<br /><br />$query1<br /><br />".mysql_error());

$query1 = "INSERT INTO uploads(name, filename, description)"."VALUES($name, $fname, $desc)";
mysql_query($query1);
$npath = mysql_insert_id();

mkdir($npath);

if(!is_uploaded_file($fpath))
   {
   die("Not uploaded file!");
   }

move_uploaded_file($fpath, $npath) or die("ERROR");

print $fpath;


?>


It keeps giving me the "Not uploaded file!" message Crying or Very sad.
MWANGI
Why don't you just use the copy function.I find it much better
Quote:
Code:
<?php
// copydirr.inc.php
/*
26.07.2005
Author: Anton Makarenko
   makarenkoa at ukrpost dot net
   webmaster at eufimb dot edu dot ua
*/
function copydirr($fromDir,$toDir,$chmod=0757,$verbose=false)
/*
   copies everything from directory $fromDir to directory $toDir
   and sets up files mode $chmod
*/
{
//* Check for some errors
$errors=array();
$messages=array();
if (!is_writable($toDir))
   $errors[]='target '.$toDir.' is not writable';
if (!is_dir($toDir))
   $errors[]='target '.$toDir.' is not a directory';
if (!is_dir($fromDir))
   $errors[]='source '.$fromDir.' is not a directory';
if (!empty($errors))
   {
   if ($verbose)
       foreach($errors as $err)
           echo '<strong>Error</strong>: '.$err.'<br />';
   return false;
   }
//*/
$exceptions=array('.','..');
//* Processing
$handle=opendir($fromDir);
while (false!==($item=readdir($handle)))
   if (!in_array($item,$exceptions))
       {
       //* cleanup for trailing slashes in directories destinations
       $from=str_replace('//','/',$fromDir.'/'.$item);
       $to=str_replace('//','/',$toDir.'/'.$item);
       //*/
       if (is_file($from))
           {
           if (@copy($from,$to))
               {
               chmod($to,$chmod);
               touch($to,filemtime($from)); // to track last modified time
               $messages[]='File copied from '.$from.' to '.$to;
               }
           else
               $errors[]='cannot copy file from '.$from.' to '.$to;
           }
       if (is_dir($from))
           {
           if (@mkdir($to))
               {
               chmod($to,$chmod);
               $messages[]='Directory created: '.$to;
               }
           else
               $errors[]='cannot create directory '.$to;
           copydirr($from,$to,$chmod,$verbose);
           }
       }
closedir($handle);
//*/
//* Output
if ($verbose)
   {
   foreach($errors as $err)
       echo '<strong>Error</strong>: '.$err.'<br />';
   foreach($messages as $msg)
       echo $msg.'<br />';
   }
//*/
return true;
}
/* sample usage:
WARNING:
if You set wrong $chmod then You'll not be able to access files and directories
in destination directory.
For example: once upon a time I've called the function with parameters:
copydir($fromDir,$toDir,true);
What happened? I've forgotten one parameter (chmod)
What happened next? Those files and directories became inaccessible for me
(they had mode 0001), so I had to ask sysadmin to delete them from root account
Be careful :-)
<?php
require('./copydirr.inc.php');
copydirr('./testSRC','D:/srv/Apache2/htdocs/testDEST',0777,true);
?>
*/
?>
Andrzej Nadziejko (andrzej at vao . pl)
27-Jun-2005 12:52
These functions are to copy and remove big directories:

/*
source files are in source directory
*/
function SetupFolder($dir_name)
{
   mkdir($dir_name,'0777');
   $folder = opendir('source');
   while($file = readdir($folder))
   {   
       if ($file == '.' || $file == '..') {
           continue;
       }
       if(is_dir('source/'.$file))
       {
           mkdir($dir_name.'/'.$file,0777);
           CopyFiles('source/'.$file,$dir_name.'/'.$file);
       }
       else
       {   
           copy('source/'.$file,$dir_name.'/'.$file);
       }
   }
   closedir($folder);
   return 1;
}
//copy many files
function CopyFiles($source,$dest)
{   
   $folder = opendir($source);
   while($file = readdir($folder))
   {
       if ($file == '.' || $file == '..') {
           continue;
       }
       
       if(is_dir($source.'/'.$file))
       {
           mkdir($dest.'/'.$file,0777);
           CopySourceFiles($source.'/'.$file,$dest.'/'.$file);
       }
       else
       {
           copy($source.'/'.$file,$dest.'/'.$file);
       }
       
   }
   closedir($folder);
   return 1;
}
//remove file, directories, subdirectories
function RemoveFiles($source)
{
   $folder = opendir($source);
   while($file = readdir($folder))
   {
       if ($file == '.' || $file == '..') {
           continue;
       }
       
       if(is_dir($source.'/'.$file))
       {
           RemoveFiles($source.'/'.$file);
       }
       else
       {
           unlink($source.'/'.$file);
       }
       
   }
   closedir($folder);
   rmdir($source);
   return 1;
}
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.