【问题标题】:Uploaded file not save in folder上传的文件未保存在文件夹中
【发布时间】:2017-11-07 20:12:54
【问题描述】:

我正在使用 XAMPP 在我的 Windows 笔记本电脑上测试此代码,没有问题。但是当我在我的 mac book 上运行时,我要上传的文件没有保存在上传文件夹中。对我有什么解决方案吗? 这是我的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>

【问题讨论】:

  • 你检查过上传目录的权限并确保它可以被PHP写吗?
  • 你的意思是在 php.ini 中?我已经检查过了。
  • 没有。我的意思是实际的文件系统。
  • file_uploads=开启
  • 先生,我该如何检查?

标签: php pdo phpmyadmin


【解决方案1】:

您在本节中的代码似乎有问题:

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

【讨论】:

  • 没有。只是我要上传的文件没有保存在@Cameron Roe 文件夹中
  • 其实我已经尝试了很多方法。我的 Windows 笔记本电脑可以将其保存在文件夹中,但我的 macbook 不能。
  • 如果它仍然没有保存并且您将代码更新为我告诉您的内容,那么您可能遇到了文件权限错误。您应该阅读有关 Mac 文件系统、bash 和终端的教程,以了解如何检查和设置上传目录的文件权限。您还需要确保该目录存在于您的 Mac 上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-21
  • 2013-02-14
  • 2021-10-24
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多