上传的文件未保存在文件夹中

时间:2013-10-03 05:38:57

标签: php html

我创建了一个表单,其中有一个文件字段,文件上传并保存在名为“att”的文件夹中。 代码如下:

if($_FILES['file'] != '') 
 {
    //Settings 

    $allowed_extensions = array("jpg", "jpeg", "gif", "bmp", "pdf", "docx","xlsx");
    echo $upload_folder = './att/'; //<-- this folder must be writeable by the script


    $errors ='';

    //Get the uploaded file information
    echo $name_of_uploaded_file = basename($_FILES['file']['name']);

    //get the file extension of the file
    $type_of_uploaded_file = substr($name_of_uploaded_file, 
                            strrpos($name_of_uploaded_file, '.') + 1);

    $size_of_uploaded_file = $_FILES["file"]["size"];

此代码已运行但文件未显示在文件夹中。代码中有什么问题

3 个答案:

答案 0 :(得分:5)

请在代码上添加 move_uploaded_file()

像这样:

<?php
if($_FILES['file'] != '') 
 {
    //Settings 

    $allowed_extensions = array("jpg", "jpeg", "gif", "bmp", "pdf", "docx","xlsx");
    $upload_folder = './att/'; //<-- this folder must be writeable by the script


    $errors ='';

    //Get the uploaded file information
    $name_of_uploaded_file = basename($_FILES['file']['name']);

    //get the file extension of the file
    $type_of_uploaded_file = substr($name_of_uploaded_file, 
                            strrpos($name_of_uploaded_file, '.') + 1);

     $size_of_uploaded_file = $_FILES["file"]["size"];

     move_uploaded_file($name_of_uploaded_file, $upload_folder);     
  }

?>

确保 att 文件夹可以重写。

答案 1 :(得分:5)

您只是使用文件详细信息而不移动它。要移动,您应该使用move_uploaded_file \

eg: 

move_uploaded_file($_FILES['file']['tmp_name'], "PLACE WHERE YOU WANT TO MOVE")

答案 2 :(得分:4)

您必须使用move_uploaded_file功能。请参阅此示例:move_uploaded_file