BRBT
BRBT

Reputation: 1487

Uploading image not working as expected

I have a form where a user fills out multiple input fields and they can also upload an image. I recently added another input field where the user can upload an additional image.

<label for="photo">Facility Roof Plan:</label>                     
<input type="file" id="facilityroofplan" name="facilityroofplan" />

When the user submits my form it should upload this image, as well as store a directory path into a db. The information is being saved into my db properly without any issues, however when I check to see if the image was uploaded it is not there.

$directoryPath =  "../images/" . $selectedAssocAccount . "/" . $facilityID;    
//create the directory
mkdir($directoryPath, 0775);

//facility roof plan
if(!empty($_FILES["facilityroofplan"]["name"])){

    //directory path for the facility photo to reside in
    $facilityRoofPlan = "../images/". $selectedAssocAccount ."/" . $facilityID . "/" . basename($_FILES["facilityroofplan"]["name"]);                                   
    if($_FILES['facilityroofplan']['error'] == UPLOAD_ERR_OK) {
        $status_msg = '';
        $from = $_FILES["facilityroofplan"]["tmp_name"];
        $saved = save_facility_roof_plan($from, $facilityPhoto, $status_msg);
    } else{
        echo "Error uploading facility image.";             
    }

    //insert into photo table
    $photoQuery = "INSERT INTO facility_roof_plan (facility_id, roof_plan) VALUES ('$facilityID', '$facilityRoofPlan')";
    mysqli_query($dbc, $photoQuery)or die(mysqli_error($dbc)); 
}

And this is what my save_facility_roof_plan function looks like:

function save_facility_roof_plan($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 a photo.";
    return false;
}

I have done this in several other places and I have no issues uploading any images.

where am I going wrong here?

Upvotes: 1

Views: 32

Answers (1)

drew010
drew010

Reputation: 69967

In your code, you have the line

$saved = save_facility_roof_plan($from, $facilityPhoto, $status_msg);

But there is no variable $facilityPhoto anywhere in what you posted. My guess is that should be changed to $facilityRoofPlan since you set that path but never use it.

Then the $saved variable is never checked for errors which might have shown you why it isn't working.

Try:

$facilityRoofPlan = "../images/". $selectedAssocAccount ."/" . $facilityID . "/" . basename($_FILES["facilityroofplan"]["name"]);                                   
if($_FILES['facilityroofplan']['error'] == UPLOAD_ERR_OK) {
    $status_msg = '';
    $from = $_FILES["facilityroofplan"]["tmp_name"];
    $saved = save_facility_roof_plan($from, $facilityRoofPlan, $status_msg);
    if (!$saved) {
        echo "Error saving roof plan image: {$status_msg}";
    }
} else{
    echo "Error uploading facility image.";             
}

Upvotes: 2

Related Questions