PHP File upload image not working

Jaaayz

I am trying to upload a image in my db but it won't work.

here is the code to upload the image.

<?php
    $con = mysqli_connect("localhost", "root", "", "test");
    mysqli_select_db("test", $con);

    $username = $_POST['username'];
    $password = $_POST['password'];
    $dob = $_POST['dob'];
    $no = $_POST['no'];

    if(isset($_POST['submit'])) {

        if(getimagesize($_FILES['image']['tmp_name']) == FALSE) {
            echo "Please select an image.";
        }

        else {
            $target_dir = "uploads/";
            $target_file = $target_dir . basename($_FILES["image"]["name"]);
            $uploadOk = 1;
            $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

            // Check file size
            if ($_FILES["image"]["size"] > 500000) {
                echo "Sorry, your file is too large.";
                $uploadOk = 0;
            }

            // Check if file already exists
            if (file_exists($target_file)) {
                echo "<p>Sorry, file already exists.</p>";
                $uploadOk = 0;
            }

            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                echo "<p>Sorry, your file was not uploaded.</p>";
            }

            // if everything is ok, try to upload file
            else {

                if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
                    $name = basename( $_FILES["image"]["name"]);
                    $image = "uploads/".basename( $_FILES["image"]["name"]);
                    echo "<p>".$name. " has been uploaded.</p>";
                    header("location: image.php");

                    $qry = "INSERT INTO images (id, name, image, username, password, dob, no) VALUES (NULL, '".$name."', '".$image."', '".$username."', '".$password."', '".$dob."', '".$no."')";
                    $result = mysqli_query($con, $qry);

                    //$sql = "INSERT INTO images (username) VALUES ('$username')";
                    //$result = mysqli_query($con, $sql);

                } 

                else {
                    echo "<p>Sorry, there was an error uploading your file.</p>";
                }
            }
        }
     }
 ?>

it always said that sorry there was an error uploading the file.

and this is the error in the server log.

PHP Warning: move_uploaded_file(): Unable to move '/tmp/phpRGn6S7' to 'uploads/Screenshot from 2017-08-09 16-57-58.png' in /var/www/html/practice1/images2.php on line 45, referer: http://localhost/practice1/image.php

I am new to file uploading in PHP. would really appreciate if someone can help. Thanks in advance.

Meloman

On Windows and Linux, the move_uploaded_file() function doesn't work the same. On Linux, you need to put the full path like this :

$target_dir = "/var/www/html/practice1/uploads/";

Or seperate project_dir and upload dir like this :

$project_dir = '/var/www/html/practice1/';
$target_dir  = $project_dir . 'uploads/';

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

File upload not working | Php

From Dev

PHP file upload not working

From Dev

File Upload not Working in PHP

From Dev

PHP - File upload not working

From Dev

PHP image upload code not working

From Dev

ng-file-upload for image upload not working

From Dev

image file upload not working on wamp server

From Dev

Image Upload in PHP using AJAX Jquery not working

From Dev

Cloudinary Image Upload Using PHP Not Working

From Dev

Image Upload in PHP using AJAX Jquery not working

From Dev

PHP file upload not working for mp4

From Dev

PHP file upload suddenly stopped working

From Dev

PHP file upload not working. Getting error:

From Dev

php multiple file upload not working at server

From Dev

Upload single image file to FTP using PHP

From Dev

PHP Upload file from Image variable

From Dev

How to upload image file in alfresco using PHP

From Dev

Upload single image file to FTP using PHP

From Dev

PHP image upload and assignment of unique file name

From Dev

Cannot move uploaded file for image upload php

From Dev

Can't upload image file with php to sever

From Dev

PHP Image Upload Security - gd / imagick / move_upload_file

From Dev

PHP Image Upload Security - gd / imagick / move_upload_file

From Dev

PHP upload file turns animated gif into regular image after upload

From Dev

upload all fields with the image file upload optional PHP MySQLI

From Dev

File upload is not working in combine php form but working separately

From Dev

PHP File Upload Not Working after change to PHP settings

From Dev

PHP File Upload Not Working after change to PHP settings

From Dev

Upload image to server not working

Related Related

HotTag

Archive