0

My code worked perfectly when all i was doing was upload files into the database. But when i tried to store it also in folder, i'm getting a warning:

Warning: move_uploaded_file(): Unable to move 'F:\xammp\xammp\tmp\php34C9.tmp' to 'uploads/' in F:\xammp\xammp\htdocs\custom-cms-praktis\main.php on line 47

I think I'm missing something important but i cant figure out what -_- here is my code:

<?php
$server = "localhost";
$user = "root";
$password = "";
$db = "cms_development";

$conn = new mysqli($server,$user,$password,$db);


if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
if (isset($_POST['username']) && isset($_POST['submit'])) {

    $fileName = $_FILES['file']['name'];
    $tmp_name = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileType = $_FILES['file']['type'];
    $author = $_POST['username'];
    $email = $_POST['email'];

    $fp = fopen($tmp_name, 'r');
    $content = fread($fp, fileSize($tmp_name));
    $content = addslashes($content);
    fclose($fp);

    if (!get_magic_quotes_gpc()) {
        $fileName = addslashes($fileName);
    }

    if (move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/")) {

        $query = "INSERT INTO upload (name, size, type, content, author, email ) ".
            "VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$author', '$email')";

        mysql_query($query) or die('Error, Bad Query');

    } else {

        echo "Sorry, there was an error uploading your file.";

    }

}
?>

    <form method="post" action="" enctype="multipart/form-data">
        <input type="text" name="username" value="" placeholder="Name">
        <input type="text" name="email" value="" placeholder="Email">
        <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
        <input type="file" name="file">
        <input type="submit" value="Submit" name="submit">
    </form>
</div>

Im working on local

4
  • Possible duplicate of PHP Warning: move_uploaded_file() unable to move
    – B001ᛦ
    Aug 31, 2016 at 7:42
  • are you sure about the directory /uploads. check this created or not or permission issue or may be code file and uploads/ in same directory. Aug 31, 2016 at 8:00
  • @YagnikDetroja , lol, the folder name was "upload" not "uploads". cant believe i missed something like that. that was a bit embarrassing, but thanks anyway
    – codeplus
    Sep 1, 2016 at 1:38
  • 1
    @codeplus ok great but you write the path as "uploads". if folder name upload then why you try to move your file in "uploads". check this in your code. if (move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/")) { Read your code carefully then laugh on other. Sep 1, 2016 at 4:46

1 Answer 1

-1

check if folder "uploads/" exists

if You don't know in what directory You are working use echo getcwd(); to print it

i think it should be in that dir: F:\xammp\xammp\htdocs\custom-cms-praktis\uploads

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.