【问题标题】:How do I combine 2 .php file into one single function如何将 2 个 .php 文件合并为一个函数
【发布时间】:2011-09-06 17:55:05
【问题描述】:

我要感谢大家帮助我让我的 sendmail.php 和 fileupload.php 文件作为单独的函数正常工作。现在我试图将它们组合到一个文件中,以便使用它们的表单将在 SUBMIT 上执行这两个功能。

这是我目前拥有的:

    <?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( "QUOTES@DOMAIN.com", "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并不陌生,但任何想法/帮助将不胜感激。

【问题讨论】:

  • 定义“不工作”。你得到什么错误?
  • 对不起@ceejayoz。我没有错误。表单中的信息按预期格式发送到我的电子邮件,但文件未上传到我的服务器。没有显示错误。所以看起来文件“有效”但没有遵循函数的 file_upload 部分。我对 php 不太熟悉,所以我什至不知道从哪里开始进行故障排除。该功能似乎在到达文件重命名和上传部分之前就停止了。

标签: php file-upload sendmail


【解决方案1】:

您的表单显然发布了提交值,对吗?否则会跳过文件上传功能。

<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 ))              

【讨论】:

  • 是的,我的表单确实有一个提交输入。您可以在此处查看示例简短表单:niagarathistle.com/upload/form_upload.html 我注释掉了如果某些字段留空则会引发错误的 php,因此我不必重新创建整个表单。 file_uploads 目录仍然在原来的位置。我尝试添加您建议的花括号 {},但这只会引发语法错误。原始的 file_upload.php 文件没有这些大括号。有什么想法吗?
  • 有人对为什么没有按预期上传和重命名文件有任何进一步的想法吗?
  • 如果我有空的话。我将再次查看您的链接和代码。没有承诺。 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-16
  • 1970-01-01
相关资源
最近更新 更多