user2480085
user2480085

Reputation: 33

PHP: Image not uploading to folder

Hey guys I think i need a fresh set of eyes on this. I have an insert script that inserts data to the database along with the URL of an image that gets uploaded to a folder called uploads. The problem is that while all the information goes into the database the image never gets uploaded to the folder. Does anyone know why?

<?php
$date=$_POST['date'];
$title=$_POST['title'];
$body=$_POST['body'];
$month=$_POST['month'];
$file = $_FILES['file'];
$name = $file['name'];
$path = "uploads/" . basename($name);

$sql="INSERT INTO content (date, title, body, month, pic_id) VALUES ('$date','$title', '$body', '$month', '" . mysql_real_escape_string($path) . "')";
$result=mysql_query($sql);

if($result && move_uploaded_file($file['tmp_name'], $path) ){
echo 'Query has been inputted into the database';
}

else{
echo 'An error occured';
}
?>

My html looks like this:

    <h1>Post A Blog </h1></br>
    <form name="personal_information" method="post" enctype="multipart/form-data" action="insert.php">    
            <label>
                <span>Date:<br></span><input id="date" type="date" name="date" />
            </label><br><br>
            <label>
                <span>Title:<br></span><input id="title" type="text" name="title" />
            </label><br><br>
            <label>
                <span>Body:<br></span><textarea id="body" type="text" name="body"/></textarea>
            </label><br><br>
            <label>
            <span>Month:<br></span><input id="month" type="text" name="month" />
            </label><br><br>
            <br>

Upload file:</br>
<input type="file" name="file" id="fileupload"></br></br>
    <input type="submit" name="submit" value="Submit" id="submit"/>

Upvotes: 0

Views: 1932

Answers (2)

Sunil Kumar
Sunil Kumar

Reputation: 1391

move_uploaded_file() - Moves an uploaded file to a new location.

is_uploaded_file() - Tells whether the file was uploaded via HTTP POST

This will work for you:

if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
   echo "File ". $_FILES['userfile']['name'] ." uploaded successfully.\n";
}

More detail and example

Upvotes: 0

wardpeet
wardpeet

Reputation: 211

I'm on my phone so can't give you an example but I'm pretty sure that you have to use the fullpath as destination.

try __DIR__ . '/'. $path or use document_root

Upvotes: 0

Related Questions