通过保存到csv文件的表单上传的附加文件的名称

时间:2014-02-04 11:54:48

标签: php

您好我有代码通过网络表单保存所有提交以保存到csv文件。我遇到的唯一问题是随查询上传的文件。因为它没有写文件名。

这是我的代码......

<?PHP 
class MySaveToCsv extends FM_ExtensionModule
{
function BeforeStartProcessing()
{
$customername = $_POST['CustomerName'];
$companyname = $_POST['CompanyName'];
$noemps = $_POST['NoEmps'];
$telephone = $_POST['TelephoneNo'];
$email = $_POST['EmailAddress'];
$addressline1 = $_POST['AddressLine1'];
$addressline2 = $_POST['AddressLine2'];
$addressline3 = $_POST['AddressLine3'];
$city = $_POST['City'];
$county = $_POST['County'];
$postcode = $_POST['PostCode'];
$enquiry = $_POST['Enquiry'];
$file = $_POST['upload'];
$source = $_POST['Source'];
$quotecode = $_POST['QuoteCode'];

if(count($_FILES)){
        $form_image_path = "http://" . $_SERVER['HTTP_HOST'] . "/forms/";
            switch($this->enquiryType){
                case "ContactUs" :
                $form_image_path .= "WebEnquiry/formdata/t_uploads_ContactUs/";

        }
        $i = 1;
        foreach($_FILES as $file){
            $dummy .= "Image Attachment No. " . $i . " : " . $file['name'] . "\r\n";
            $i++;
        }
    }


$fp = fopen('ContactUs.csv', a); 
if($customername){
$savestring = $customername . "|" . $companyname . "|" . $noemps . "|" . $telephone . "|" . $email . "|" . $addressline1 . "|" . $addressline2 . "|" . $addressline3 . "|" . $city . "|" . $county . "|" . $postcode . "|" . $enquiry . "|" . $dummy . "|" . $source . "|" . $quotecode . "\n";
fwrite($fp, $savestring);}
fclose($fp);
}
}
?>

更改一些代码后第二次提交但问题仍然存在

<?PHP 
class MySaveToCsv extends FM_ExtensionModule
{
function BeforeStartProcessing()
{
$customername = $_POST['CustomerName'];
$companyname = $_POST['CompanyName'];
$noemps = $_POST['NoEmps'];
$telephone = $_POST['TelephoneNo'];
$email = $_POST['EmailAddress'];
$addressline1 = $_POST['AddressLine1'];
$addressline2 = $_POST['AddressLine2'];
$addressline3 = $_POST['AddressLine3'];
$city = $_POST['City'];
$county = $_POST['County'];
$postcode = $_POST['PostCode'];
$enquiry = $_POST['Enquiry'];
$source = $_POST['Source'];
$quotecode = $_POST['QuoteCode'];
$file = $_POST['upload'];


if(count($_FILES)){

$filepath = $_SERVER['DOCUMENT_ROOT'] . "/forms/ContactUs/formdata/uploads_ContactUs/" . $_FILES["file"]["name"];
}
function reArrayFiles(&$file_post) {

$file_ary = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);

for ($i=0; $i<$file_count; $i++) {
    foreach ($file_keys as $key) {
        $file_ary[$i][$key] = $file_post[$key][$i];
    }
}


return $file_ary;
}
if ($_FILES['upload']) {
$file_ary = reArrayFiles($_FILES['ufile']);

foreach ($file_ary as $file) {
    $dummy .= 'File Name: ' . $file['name'];


   }

}


$fp = fopen('ContactUs.csv', a); 
if($customername){
    $today = date("j/m/Y H:i");
$savestring = $today . "|" . $customername . "|" . $companyname . "|" . $noemps . "|" . $telephone . "|" . $email . "|" . $addressline1 . "|" . $addressline2 . "|" . $addressline3 . "|" . $city . "|" . $county . "|" . $postcode . "|" . $enquiry . "|" . $dummy . "|" . $source . "|" . $quotecode . "\n";
fwrite($fp, $savestring);}
fclose($fp);
}
}
?>

1 个答案:

答案 0 :(得分:0)

该脚本中有很多可能出错的地方。您有错误的文件路径,或者PHP没有保存文件的权限,或者未设置$this->enquiryType,或者您的代码未按预期执行。

尝试更简单的方法以确保您可以编写文件:

$filepath = $_SERVER['DOCUMENT_ROOT'] . "/WebEnquiry/formdata/t_uploads_ContactUs/" . $_FILES["file"]["name"];

if (file_exists($filepath)) die($_FILES["file"]["name"] . ' - This file already exists.');
if (move_uploaded_file($_FILES["file"]["tmp_name"], $filepath)){
    //Successful. Maybe set a flag.
}
else die('File upload/save error.');

注释掉现有的上传代码,只是尝试将上传的文件保存到脚本中的目录中。 PHP需要对该目录的写权限。