简体   繁体   English

如何在PHP中上传两个文件

[英]How To Upload two files in PHP

I'm trying to upload two file in one submit button using the following code: 我正在尝试使用以下代码在一个提交按钮中上传两个文件:

<label>Logo Image *</label>
<input type="file" name="ufile[]"/>
<label>Banner Image *</label>
<input type="file" name="ufile[]"/>

PHP PHP

$logo = $_FILES['ufile']['name'][0];
$block_img = $_FILES['ufile']['name'][1];

 if ($_FILES['ufile']['name']["error"] > 0) {
    echo "error<br>";
   }
  else {
    if (file_exists("small-image/" .  $_FILES['ufile']['name'][0])){
        echo $_FILES['ufile']['name'][1] . "File already exists in server. ";
    }
    else {
        move_uploaded_file($_FILES['ufile']['name'][0], "small-image/" . $_FILES['ufile']['name'][0]);
        move_uploaded_file($_FILES['ufile']['name'][1], "small-image/" . $_FILES['ufile']['name'][1]);
    }
 }

$sql_query = "UPDATE header_img SET logo_img = '$logo', block_img = '$block_img' WHERE banner_id = 1";

My database is updating correctly but the file is not uploaded. 我的数据库正在正确更新,但是文件未上传。 Yes there is a 777 directory call 'small-image'. 是的,有一个777目录呼叫“小图像”。

Any idea? 任何想法? Thanks. 谢谢。

When you use move_uploaded_file , you want to use $_FILES['ufile']['tmp_name'] , that's where the file is currently located. 使用move_uploaded_file ,要使用$_FILES['ufile']['tmp_name'] ,这是文件当前所在的位置。

move_uploaded_file($_FILES['ufile']['tmp_name'][0], "small-image/" . $_FILES['ufile']['name'][0]);
move_uploaded_file($_FILES['ufile']['tmp_name'][1], "small-image/" . $_FILES['ufile']['name'][1]);

Check the example in the docs: http://php.net/manual/en/function.move-uploaded-file.php 检查文档中的示例: http : //php.net/manual/en/function.move-uploaded-file.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM