doujiu3882 2015-03-23 16:04
浏览 52
已采纳

move_uploaded_file没有上传图片

I am trying to upload a file to an images folder and also insert the directory path into a mysql db.

Here is my HTML form

<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>

newfacility.php

require_once('../appvars.php');
require_once('upload_image.php');

//connect to db and test connection.
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);        
if (!$dbc) {
    die("Connection failed: " . mysqli_connect_error());
}

if (isset($_POST['submit'])) {
    // Grab the user data from the POST
    $facilityNumber = mysqli_real_escape_string($dbc, trim($_POST['facilitynumber']));
    ....
    ....

    //This is defined in appvars.php -- define('MM_UPLOADPATH', 'images/');

    //facility photo
    $facilityPhoto = MM_UPLOADPATH . basename($_FILES["facilityphoto"]["name"]);
    $facilityPhotoDesc = mysqli_real_escape_string($dbc, trim($_POST['photodesc']));

    // check if the faciliy info already exists.
    if (!empty($facilityNumber) && !empty($facilityName) && !empty($facilityAddress) && !empty($facilityCity)) {

        $query = "SELECT * FROM facility WHERE facility_number = '$facilityNumber' AND facility_name = '$facilityName' "
                . "AND facility_address = '$facilityAddress' AND facility_city = '$facilityCity'";

        $data = mysqli_query($dbc, $query);
        //if the facility is unique insert the data into the database
        if (mysqli_num_rows($data) == 0) {
            //insert into facility table
            $query = "INSERT INTO facility (facility_id, account_id, facility_number, facility_name, facility_address,"
                    . " facility_city, facility_province, facility_postal_code, facility_photo, facility_roof_plan,"
                    . " facility_roof_size, facility_roof_size_inspected, facility_last_inspected_date, facility_inspected_by)"
                    . " VALUES (NULL, '$selectedAssocAccount', '$facilityNumber', '$facilityName', '$facilityAddress', "
                    . "'$facilityCity', '$facilityProvince', '$facilityPostalCode', '$facilityRoofSize', "
                    . "'$facilityRoofSizeInspected', '$facilityDayInspected', '$facilityInspectedBy')";
            mysqli_query($dbc, $query);

            //query used to get the facility_id of the facility we had just entered -- I haven't tested this yet.
            $getFacilityID = "SELECT facility_id FROM facility WHERE facility_number = '$facilityNumber' AND facility_name = '$facilityName' "
                . "AND facility_address = '$facilityAddress' AND facility_city = '$facilityCity'";

            $facilityID = mysqli_query($dbc, $getFacilityID);

            //insert into photo table
            $photoQuery = "INSERT INTO photo (photo_id, facility_id, photo, photo_desc)"
                        . "VALUES (NULL, $facilityID, $facilityPhoto, $facilityPhotoDesc)";
            mysqli_query($dbc, $photoQuery);

            // Confirm success with the user
            echo '<p>You have succesfully created a new facility. '
                . 'Please go back to the <a href="/admin.php">admin panel</a>.</p>';

            //testing to see if I can view the image
            echo '<img class="profile" src="' . MM_UPLOADPATH . $facilityPhoto . '"/>';

            //close db connection
            mysqli_close($dbc);
            exit();
        }

And finally here is upload_image.php

if(isset($_FILES["facilityphoto"])) { 
    // Check if file already exists
    if (file_exists($facilityPhoto)) {
        echo "Sorry, facility photo already exists.";
    }
    if($_FILES['facilityphoto']['error'] !==0) { 
        echo "Error uploading facility photo image.";
    } else {
        if (move_uploaded_file($_FILES["facilityphoto"]["tmp_name"], $facilityPhoto)) {
            echo "The file ".( $_FILES["facilityphoto"]["name"]). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading the facility photo.";
       }
    }
}

So the error I keep hitting right now is: echo "Sorry, there was an error uploading the facility photo.";

I don't understand what I am doing wrong here that is resulting in the image not being uploaded into my images/ directory.

  • 写回答

2条回答 默认 最新

  • dss087358 2015-03-23 17:18
    关注

    I am going to provide an answer that addresses only the file upload problem, all the database stuff is striped from the answer as it is not relevant.

    // 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
    }
    

    The following is an explanation of what I think is happening in your scripts.

    At the top of newfacility.php, require_once('upload_image.php'); is called. Now lets step though upload_image.php noting that $facilityPhoto has not yet been defined

    // 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.";
           }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值