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

时间:2017-06-06 17:31:23

标签: php pdo phpmyadmin

我使用XAMPP在我的Windows笔记本电脑上测试这些代码,没有问题。但是当我在我的Mac书上运行时,我想上传的文件没有保存在上传文件夹中。对我有什么解决方案? 这是我的PHP代码

<?php
$conn=new PDO('mysql:host=localhost; dbname=xxx', 'root', '') or die(mysql_error());
if(isset($_POST['submit'])!=""){


    $title = $_POST['title'];// file title
    $date = $_POST['date'];// file date
    $ref = $_POST['ref'];// file ref
    $ftype = $_POST['ftype'];// file ref
    $from_to = $_POST['from_to'];// person incharge
    $details = $_POST['details'];// details
    $location = $_POST['location'];// file location
    $status = $_POST['status'];// file status   

  $name=$_FILES['photo']['name'];
  $size=$_FILES['photo']['size'];
  $type=$_FILES['photo']['type'];
  $temp=$_FILES['photo']['tmp_name'];
  move_uploaded_file($_FILES,"upload/".$name);
$query=$conn->query("INSERT INTO upload(title, date, ref, ftype, from_to, details, location, status, name)VALUES('$title', '$date', '$ref', '$ftype', '$from_to', '$details', '$location', '$status', '$name')");
if($query){
header("location:view.php");
    }
else{
die(mysql_error());
    }
}
?>

这是我的表格

<form enctype="multipart/form-data" action="" name="form" method="post">

        <table class="table table-bordered table-responsive">

    <tr>
        <td><label class="control-label">Reference Number</label></td>
        <td><input class="form-control" type="text" name="ref" placeholder="Reference Number" required /></td>
    </tr>

    <tr>
        <td><label class="control-label">File Name</label></td>
        <td><input class="form-control" type="text" name="title" placeholder="File Name" required /></td>
    </tr>

    <tr>
        <td><label class="control-label">Type</label></td>
        <td><p><select name="ftype" required >
            <option value="Correspondence">Correspondence</option>
</select></p></td>
    </tr>

    <tr>
        <td><label class="control-label">Date</label></td>
        <td><input class="form-control" type="date" name="date" placeholder="Date" required /></td>
    </tr>
    <tr>

        <td><label class="control-label">Person Incharge</label></td>
        <td><input class="form-control" type="text" name="from_to" placeholder="Person Incharge" required /></td>
    </tr>

        <td><label class="control-label">Details</label></td>
        <td><textarea class="form-control" name="details" placeholder="Details" required ></textarea> </td>
    </tr>

        <td><label class="control-label">Location</label></td>
        <td><p><select name="location" required >
            <option value="Admin">Admin</option>

</select></p></td>
    </tr>

        <td><label class="control-label">Status</label></td>
        <td><p><select name="status" required >
            <option value="Active">Active</option>
</select></p></td>
    </tr>

    <tr>
        <td colspan="2"><p>Attach File<input type="file" name="photo" id="photo" required />
                    <input type="submit" name="submit" id="submit" value="Submit" />

        </td>
    </tr>

    </table>
    </form>

1 个答案:

答案 0 :(得分:0)

您似乎在本节中遇到了代码问题:

$name=$_FILES['photo']['name'];
$size=$_FILES['photo']['size'];
$type=$_FILES['photo']['type'];
$temp=$_FILES['photo']['tmp_name'];
move_uploaded_file($_FILES,"upload/".$name);

move_uploaded_file为其第一个参数取一个字符串,而不是一个关联数组。您正在传递整个$_FILES超全局。相反,您需要指定要移动的文件。您已经在文件tmp_name中设置了一个变量,这就是您传递给move_uploaded_file的内容。试试这个更新的代码。

$name=$_FILES['photo']['name'];
$size=$_FILES['photo']['size'];
$type=$_FILES['photo']['type'];
$temp=$_FILES['photo']['tmp_name'];
move_uploaded_file($temp,"upload/".$name);