【问题标题】:File Upload Error using php使用php文件上传错误
【发布时间】:2014-03-10 18:18:55
【问题描述】:

我已经编写了一些代码来使用 php 在 MySQL 数据库中上传文件。 以下是我上传文件的代码。

$data['report_type']     = $this->input->post('report_type');
        $data['document_type']   = $this->input->post('document_type');
        $data['prescription_id'] = $this->input->post('prescription_id');
        $data['description']     = $this->input->post('description');
        $data['timestamp']       = strtotime(date('Y-m-d') . ' ' . date('H:i:s'));
        $data['laboratorist_id'] = $this->session->userdata('laboratorist_id');
        move_uploaded_file($_FILES["userfile"]["tmp_name"], "uploads/diagnosis_report/" . $_FILES["userfile"]["name"]);
        $data['file_name'] = $_FILES["userfile"]["name"];

        $this->db->insert('diagnosis_report', $data);
        $this->session->set_flashdata('flash_message', get_phrase('diagnosis_report_created'));
        redirect(base_url() . 'index.php?laboratorist/manage_prescription/edit/' . $this->input->post('prescription_id'), 'refresh'); 

每当我尝试上传任何文件时。它给出了一个错误

列“文件名”不能为空

插入diagnosis_report (report_type, document_type, prescription_id, description, timestamp, laboratorist_id, file_name) 值 (0, 0, 0, 0, 0, 139220) 1',空)

我的代码有什么问题?

【问题讨论】:

  • INSERT INTO diagnosis_report (report_type, document_type, prescription_id, description, timestamp, laboratorist_id, file_name) VALUES (0, 0, 0, 0, 1392206996, '1', '')
  • 你在使用codeigniter框架吗
  • 是的@Jenz!我正在使用 Codeigniter 框架!

标签: php mysql image file-upload upload


【解决方案1】:

SQL 错误:

列“文件名”不能为空

指的是插入语句中的最后一列。当file_name 列的列定义中有NOT NULL 时,您正尝试将其设置为NULL

两种解决方案:

第一,在您的数据库表上运行 alter table 命令以允许 NULL 值。

ALTER TABLE diagnosis_report MODIFY COLUMN file_name [datatype] DEFAULT NULL [etc]

如果它是 CMS 的一部分或您不维护的软件包,通常不推荐使用。如果是独立代码,这个解决方案是可用的。

或者二,修改你的PHP不要将file_name设置为NULL,而只是一个空字符串:

INSERT INTO diagnosis_report (report_type, document_type, prescription_id, description, timestamp, laboratorist_id, file_name) VALUES (0, 0, 0, 0, 1392206996, '1', '')

【讨论】:

  • 我试过你的代码,但它只是显示该文件已成功上传,但实际上并未存储在数据库中。
  • 你能确认文件正在保存using move_uploaded_file吗?如果不是这种情况,您可能需要进行一些调试以检查文件是否存在于$_FILES 中。如果不是,则可能是表单或服务器问题。
【解决方案2】:
$data['file_name']

返回 NULL 值,因为用户上传的文件存储在 $_FILES 变量中,而不是在 $_POST 中。

使用codeigniter的上传库:

http://ellislab.com/codeigniter/user_guide/libraries/file_uploading.html

【讨论】:

  • 我做了如下 - move_uploaded_file($_POST["userfile"]["tmp_name"], "uploads/diagnosis_report/" . $_POST["userfile"]["name"]); $data['file_name'] = $_FILES["userfile"]["name"];但仍然无法存储文件
【解决方案3】:

从您附加的代码段中删除这两行:-

move_uploaded_file($_FILES["userfile"]["tmp_name"], "uploads/diagnosis_report/" . $_FILES["userfile"]["name"]);
$data['file_name'] = $_FILES["userfile"]["name"];

不是上面两行,而是添加下面的代码行 -

$move='/uploads/diagnosis_report/';
$uploadfile=$_FILES["userfile"]["name"];
$path=$move.basename($uploadfile);
move_uploaded_file($_FILES["userfile"]["tmp_name"], SITE_ROOT.$path);
$data['file_name'] = basename($uploadfile);

现在你必须再做一件事 转到 \application\views\laboratorist。打开 ma​​nage_prescription.php ,你会看到下面的代码段

<!------ADD A DIAGNOSTIC REPORT TO THIS PRESCRIPTION-->
                    <div class="box">
                        <div class="box-header"><span class="title"><?php echo get_phrase('add_diagnosis_report');?></span></div>
                        <div class="box-content">
                        <?php echo form_open('laboratorist/manage_prescription/create_diagnosis_report' , array('class' => 'form-horizontal validatable'));?>

现在将相应的 form_open 更改为 form_open_multipart

 <!------ADD A DIAGNOSTIC REPORT TO THIS PRESCRIPTION-->
                    <div class="box">
                        <div class="box-header"><span class="title"><?php echo get_phrase('add_diagnosis_report');?></span></div>
                        <div class="box-content">
                        <?php echo form_open_multipart('laboratorist/manage_prescription/create_diagnosis_report' , array('class' => 'form-horizontal validatable'));?>

               I hope now it will run successfully. Please let me know if it doesn't work. Thanks.

【讨论】:

    猜你喜欢
    • 2015-04-08
    • 2010-10-19
    • 1970-01-01
    • 2012-06-18
    • 2010-09-05
    • 2012-05-04
    • 1970-01-01
    相关资源
    最近更新 更多