2011-09-06 74 views
-2

我想感謝大家幫助我將sendmail.php和fileupload.php文件作爲單獨的函數正常工作。現在我正在嘗試將它們合併到一個文件中,以便使用它們的表單將在SUBMIT上執行這兩個函數。如何將2 .php文件合併爲一個函數

這是我目前:

<?php 
$project = $_REQUEST['project'] ; 
$project_other = $_REQUEST['project_other'] ; 
$quantity = $_REQUEST['quantity'] ;  
$pages = $_REQUEST['pages'] ; 
$color = $_REQUEST['color'] ; 
$color_other = $_REQUEST['color_other'] ; 
$size = $_REQUEST['size'] ; 
$page_layout = $_REQUEST['page_layout'] ; 
$stock = $_REQUEST['stock'] ; 
$stock_other = $_REQUEST['stock_other'] ; 
$paper_finish = $_REQUEST['paper_finish'] ; 
$paper_finish_other = $_REQUEST['paper_finish_other'] ; 
$typeset = $_REQUEST['typeset'] ; 
$timeframe = $_REQUEST['timeframe'] ; 
$budget = $_REQUEST['budget'] ; 
$add_info = $_REQUEST['add_info'] ; 
$name = $_REQUEST['name'] ; 
$phone = $_REQUEST['phone'] ; 
$email = $_REQUEST['email'] ; 
$company = $_REQUEST['company'] ; 
$proj_name = $_REQUEST['proj_name'] ; 
$zip = $_REQUEST['zip'] ; 
$upload = $_REQUEST['upload'] ; 

if (!isset($_REQUEST['email'])) { 
    header("Location: ../pages/quote/quote.html"); 
} 
if (ereg("[\r\n]", $name) || ereg("[\r\n]", $email)) { 
    header("Location: ../pages/quote/quote_injection_error.html"); 
} 
elseif (empty($name) || empty($phone) || empty($email) || empty($company) || empty($proj_name) || empty($zip) || empty($project) || empty($quantity) || empty($color) || empty($size) || empty($timeframe) || empty($budget)) { 
    header("Location: ../pages/quote/quote_content_error.html"); 
} 
else { 
    mail("[email protected]", "Request for Quote: $project", 
     "$add_info\n 
     What kind of project is this? $project\n 
     Name: $name\n 
     Name of Project: $proj_name\n 
     Company: $company\n 
     Telephone: $phone\n 
     E-mail Address: $email\n 
     ZIP code: $zip\n 
     Is there a file attachment/upload? $upload\n 
     What do you need a quote on? $project : $project_other\n 
     What quantity do you require? $quantity\n 
     If applicable, how many pages is each document? $pages\n 
     Full color or black and white? $color : $color_other\n 
     What size do you want your print project to be? $size\n 
     What type of page layout do you need for your project? $page_layout\n 
     What paper stock do you require? $stock : $stock_other\n 
     What paper finish do you require? $paper_finish : $paper_finish_other\n 
     Are your documents typeset? $typeset\n 
     When do you need this project completed by? $timeframe\n 
     What is your budget for this project? $budget\n 
     Additional information to help COMPANY prepare our quote for you? $add_info", 
     "From: $name <$email>"); 
    header("Location: ../pages/quote/quote_thanks.html"); 
    } 
if (isset($_POST['submit'])) { 

    // Configuration - Script Options 
    $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension 
    $file_basename = substr($filename, 0, strripos($filename, '.')); // Get file name minus extension 
    $file_ext = substr($filename, strripos($filename, '.')); // Get file extension 
    $filesize = $_FILES['file']['size']; // Get file size 
    $allowed_file_types = array('.jpg','.jpeg','.gif','.bmp','.png','.pdf','.doc','.docx','.psd'); // These will be the types of files that are allowed to pass the upload validation 
    $file_counter = 1; // used to increment filename if name already exists 
    $company = $_REQUEST['company']; 
    $project = $_REQUEST['proj_name']; 

    // File renaming and upload functionality 
    if (in_array($file_ext,$allowed_file_types) && ($filesize < 10000001)) { // Checks to make sure uploaded file(s) is an allowed file type AND within the allowable file size (currently 10MB) 

     // Rename File 
     $newfilename = $company . '_' . $proj_name . '_' . $file_basename; // Rename file as (CompanyName_FileName_DateStamp) 
     // Loop until an available file name is found 
     while (file_exists("file_uploads/" . $newfilename)) 
      $finalfilename = $newfilename . '_' . $file_counter++ . $file_ext; // This will be the File Name shown in the upload destination directory (currently the "file_uploads" directory) 
     if (file_exists("file_uploads/" . $finalfilename)) { 
      // file already exists error 
      echo "This file already exists. Please rename this file and upload again if necessary."; 
     } else { 
      move_uploaded_file($_FILES["file"]["tmp_name"], "file_uploads/" . $finalfilename); 
      echo "File uploaded successfully."; 
     } 
    } elseif (empty($file_basename)) { 
      // file selection error 
      echo "Please select a file to upload."; 
     } elseif ($filesize > 10000000) { 
      //file size error 
      echo "The file you are trying to upload is too large. Files must be no larger than 10MB."; 
     } else { 
      // file type error 
      echo "The file you attempted to upload is not allowed. You can only upload the following types of files: .jpg, .jpeg, .gif, .bmp, .png, .pdf, .doc, .docx, and .psd."; 
      unlink($_FILES["file"]["tmp_name"]); 
     } 
    } 
    /* 
    must add page links for error and success messages: 
    // redirect to upload success url 
    header("Location: http://www.example.com/thankyou.html"); 
    die(); 
    */ 

    ?> 

的「sendmail的」部分的作品,我得到的答案,我通過電子郵件發送給我說清楚,簡明的形式輸入。但是,由於我將「file_upload」文件添加到sendmail.php的底部,並且沒有任何更改(只是剪切並粘貼在最後關閉php標記?>之上),所以file_upload和重命名函數不起作用。

任何人都可以指出我在正確的方向如何讓這個工作在一個單一的文件?我不太熟悉PHP,但任何想法/援助將不勝感激。

+0

定義「不工作」。你會得到什麼錯誤? – ceejayoz

+0

對不起@ceejayoz。我沒有得到任何錯誤。表單中的信息按預期格式發送到我的電子郵件,但文件未上傳到我的服務器。沒有顯示錯誤。所以它看起來像文件「工作」,但沒有遵循函數的file_upload部分。我不太熟悉PHP,所以我甚至不知道從哪裏開始進行故障排除。它看起來像該函數甚至在它到達文件重命名和上傳部分之前停止。 – Craig

回答

0

你的表格顯然是提交一個提交值的權利?如果不是,則會跳過文件上傳功能。

<input type="hidden" name="submit" value="TRUE" /> 

因爲你把它放在腳本的file_upload部分的頂部。

if (isset($_POST['submit'])) 

其他的事情可能是您移動從一個目錄下的文件file_upload.php到另一個,現在當你調用

file_exists("file_uploads/" . $newfilename) 

目錄不能被發現和跳過功能。

最後,你是否遺漏了while循環的{}?你說你從另一個文件複製100%。所以即時通訊假設它是獨立運作的?

while (file_exists("file_uploads/" . $newfilename))    
+0

是的,我的表單確實有一個SUBMIT輸入。你可以在這裏查看示例簡寫:http://www.niagarathistle.com/upload/form_upload.html我註釋瞭如果某個字段空白的話會引發錯誤,所以我不必重新創建整個表單。 file_uploads目錄仍然與原來的位置相同。我嘗試添加你建議的花括號,但是這沒有做任何事情,只是拋出語法錯誤。原始的file_upload.php文件沒有這些大括號。任何想法? – Craig

+0

任何人有任何進一步的想法,爲什麼這不是按預期上傳和重命名文件? – Craig

+0

如果我可以免費得到一些時間。我會再看看你的鏈接和代碼。沒有保證。 ;) – Adam