Re: Stuck trying to upload and grab file name
| From: | Jim Giner | Date: | Tue, 24 Jul 2012 12:44:46 +0000 |
| Subject: | Re: Stuck trying to upload and grab file name | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-48340@lists.php.net to get a copy of this message | ||
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.