RE: [PHP-DB] Re: Stuck trying to upload and grab file name
| From: | Brad | Date: | Tue, 24 Jul 2012 15:18:16 +0000 |
| Subject: | RE: [PHP-DB] Re: Stuck trying to upload and grab file name | ||
| References: | 1 2 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-48342@lists.php.net to get a copy of this message | ||
Thank you very much my friend.
The significant difference between your code and mine is that my file
contents are going into a MySQL database and it is this db function that is
crapping out. The SQL (with variables replaced with static values) runs
clean -> http://pastie.org/4302489
But as soon as you throw in a variable/dynamic value, it just becomes a
mess.
Brad
-----Original Message-----
From: Jim Giner [mailto:jim.giner@albanyhandball.com]
Sent: Tuesday, July 24, 2012 8:45 AM
To: php-db@lists.php.net
Subject: [PHP-DB] Re: Stuck trying to upload and grab file name
At the risk of being unable to understand all the posts that are here
already, here is some std. code that I use when I'm uploading a file.
First - i capture the input field in a local var and all of its components
in other local vars to make my coding easier later on.
$inputfield = $_POST['infile']; // infile is the html name of the
type='file' field
$destfolder = (where I want the end result stored) $outfile = (the filename
of the end result) $result =
HandleUploadedFile($inputfield,$destfolder,$outfile);
(handle $result after return)
*******
The function I use:
function HandleUploadedFile($inputfield,$destfolder,$outfile)
{ // parm 1 is the name of the type=file field in the html; 2 & 3 are
path and output filename you want to store it in
global $msg,$ftype,$ferror,$fname,$fsize,$ftmpname;
$ftype = $_FILES[$inputfield]["type"];
$ferror = $_FILES[$inputfield]["error"];
$fname = $_FILES[$inputfield]["name"];
$fsize = $_FILES[$inputfield]["size"];
$ftmpname = $_FILES[$inputfield]["tmp_name"];
// code up acceptabe filetypes here
if ( $ftype == "text/plain")
{
if ($ferror > 0)
{
$msg .= ' Upload error - return code was '.$ferror;
return False;
}
else
{
if (file_exists($destfolder . $fname))
{
$msg .= " Duplicate filename - upload not done,
continuing.";
return True;
}
else
{
$msg .= " File has been uploaded to: " . $destfolder
.$outfile;
move_uploaded_file($ftmpname,$destfolder . $outfile);
return True;
}
}
}
else
{
$msg .= " Invalid file type for menu input upload";
$msg .= " Type is ".$ftype;
return False;
}
}
You of course want to name your end result like the original filename, so
you will have to make a change to that parameter, but this will work for
you.
--
PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit:
http://www.php.net/unsub.php