Php 移动\u上载的\u文件不上载图像

Php 移动\u上载的\u文件不上载图像,php,mysql,image-uploading,Php,Mysql,Image Uploading,我正在尝试将一个文件上载到images文件夹,并将目录路径插入mysqldb 这是我的HTML表单 <form enctype="multipart/form-data" method="post" action="newfacility.php"> <fieldset> <legend>New Facility</legend> ... <label f

我正在尝试将一个文件上载到images文件夹,并将目录路径插入
mysql
db

这是我的
HTML
表单

<form enctype="multipart/form-data" method="post" action="newfacility.php">
        <fieldset>
            <legend>New Facility</legend>
            ...
            <label for="photo">Facility Photo:</label>                     
            <input type="file" id="facilityphoto" name="facilityphoto" /><br />
            <label for="province">Photo Description:</label>
            <input type="text" id="photodesc" name="photodesc" /><br />
            ....
            <input type="submit" value="Create" name="submit" />
        </fieldset>
    </form>
所以我现在一直遇到的错误是:
echo“对不起,上传设施照片时出错。”

我不明白我在这里做错了什么,导致图像没有上传到我的
images/
目录中

更换这些线路:

if (move_uploaded_file($_FILES["facilityphoto"]["tmp_name"], $facilityPhoto)){
   echo "The file ".( $_FILES["facilityphoto"]["name"]). " has been uploaded.";
}
有了这些:

if (move_uploaded_file($_FILES["facilityphoto"]["tmp_name"], 'images/'. $_FILES["facilityphoto"]["name"])){
   echo "The file ".( $_FILES["facilityphoto"]["name"]). " has been uploaded.";
}

我将提供一个只解决文件上传问题的答案,所有的数据库内容都将从答案中删除,因为这与问题无关

// returns true only if the file was written to $to, 
// the value of $status_msg will be a user friendly string
// representing the outcome.
function save_facility_photo($from, $to, &$status_msg) {
    // Check if file already exists
    if (file_exists($to)) {
        $status_msg = "Sorry, facility photo already exists.";
        return false;
    } 
    if (move_uploaded_file($from, $to)) {
        $status_msg = "The file ".basename($to)." has been uploaded.";
        return true;
    }
    $status_msg = "Sorry, there was an error uploading the facility photo.";
    return false;
}

if (isset($_POST['submit'])) {
    define('MM_UPLOADPATH', 'images/');

    $facilityPhoto = MM_UPLOADPATH . basename($_FILES["facilityphoto"]["name"]);

    if ($_FILES['facilityphoto']['error'] == UPLOAD_ERR_OK) {
        $status_msg = '';
        $from = $_FILES["facilityphoto"]["tmp_name"];
        $saved = save_facility_photo($from, $facilityPhoto, $status_msg);
    }
    else {
        // handle upload error
    }
    // continue with code
}
以下是我认为在您的脚本中发生的事情的解释

newfacility.php
的顶部,
require_once('upload_image.php')被调用。现在让我们通过
upload_image.php
进行步骤,注意
$facilityPhoto
尚未定义

// this is very likely true
if(isset($_FILES["facilityphoto"])) { 
    // $facilityPhoto is undefined so file_exists(NULL) will return false
    if (file_exists($facilityPhoto)) { }
    // the image upload was probably successful, so we jump to the else branch
    if($_FILES['facilityphoto']['error'] !==0) { 

    } 
    else {
        // $facilityPhoto is undefined move_uploaded_file('p/a/t/h', NULL) 
        // will return false, so we jump to the else branch
        if (move_uploaded_file($_FILES["facilityphoto"]["tmp_name"], $facilityPhoto)) {

        } 
        else {
            // resulting in this error
            echo "Sorry, there was an error uploading the facility photo.";
       }
    }
}

$facilityPhoto
的值为“images/imagename.jpeg”。所以我不明白为什么我要用硬编码目录替换我的变量。只是因为,我测试了这个,结果还是一样的(和预期的一样)。我不确定这是否回答了OP的问题。它只是检查代码看它是否还没有上传,但并不是为了确保在我看来它是上传的,您甚至没有检查文件上载是否成功…您将实际上载部分与将已上载的文件从临时目录移动到其他位置相混淆…我正在谈论的是:如何调用
newfacility.php
?它是否包含在
upload_image.php
中?
MM_UPLOADPATH
的值是多少?@CBroe Ohhh-hmm,那么我在这里上传了什么吗?我应该如何准确地检查文件的上载?在$\u文件中的错误数组我将此设置用于将图像实际上载到文件夹中,但由于某些原因,无法复制结果,我需要第二次..(我使用的是不同的图片)
// returns true only if the file was written to $to, 
// the value of $status_msg will be a user friendly string
// representing the outcome.
function save_facility_photo($from, $to, &$status_msg) {
    // Check if file already exists
    if (file_exists($to)) {
        $status_msg = "Sorry, facility photo already exists.";
        return false;
    } 
    if (move_uploaded_file($from, $to)) {
        $status_msg = "The file ".basename($to)." has been uploaded.";
        return true;
    }
    $status_msg = "Sorry, there was an error uploading the facility photo.";
    return false;
}

if (isset($_POST['submit'])) {
    define('MM_UPLOADPATH', 'images/');

    $facilityPhoto = MM_UPLOADPATH . basename($_FILES["facilityphoto"]["name"]);

    if ($_FILES['facilityphoto']['error'] == UPLOAD_ERR_OK) {
        $status_msg = '';
        $from = $_FILES["facilityphoto"]["tmp_name"];
        $saved = save_facility_photo($from, $facilityPhoto, $status_msg);
    }
    else {
        // handle upload error
    }
    // continue with code
}
// this is very likely true
if(isset($_FILES["facilityphoto"])) { 
    // $facilityPhoto is undefined so file_exists(NULL) will return false
    if (file_exists($facilityPhoto)) { }
    // the image upload was probably successful, so we jump to the else branch
    if($_FILES['facilityphoto']['error'] !==0) { 

    } 
    else {
        // $facilityPhoto is undefined move_uploaded_file('p/a/t/h', NULL) 
        // will return false, so we jump to the else branch
        if (move_uploaded_file($_FILES["facilityphoto"]["tmp_name"], $facilityPhoto)) {

        } 
        else {
            // resulting in this error
            echo "Sorry, there was an error uploading the facility photo.";
       }
    }
}