0

I am building a upload page that will allow me to upload the picture and the record will be stored in MySQL. For now, I am able to save the record. However, I am having the problem in storing the picture in my actual file and the picture is not storing.

The code is below:

  <?php
           include "config.php";

            if (isset($_FILES['file'])) {
                $name = $_FILES['file']['name'];
                $size = $_FILES['file']['size'];
                $type = $_FILES['file']['type'];
                $tmp = $_FILES['file']['tmp_name'];
                $file = 'uploads'. $_FILES['file']['name'];
                $upload = move_uploaded_file($tmp, $file);
                if ($upload) {
                    $add = $db->prepare("insert into upload values('',?)");
                    $add->bindParam(1, $name);
                    if ($add->execute()) {
                        ?>
                        <div class="alert alert-success alert-dismissible" role="alert">
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                            <strong>Success!</strong> File upload successful to database.
                        </div>
                        <?php
                    } else {
                        ?>
                    <div class="alert alert-danger alert-dismissible" role="alert">
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <strong>Failed!</strong> File upload unsuccessful to database.
                    </div>
                    <?php
                    }
                } else {
                    ?>
                    <div class="alert alert-warning alert-dismissible" role="alert">
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <strong>Sorry!</strong> File upload unsuccessful .
                    </div>
                    <?php
                }
            }
            ?>

This is my file: enter image description here

After I echo out $file & $tmp: enter image description here

5
  • Was there an error? Or the picture is misplaced? Shouldn't there be a slash between the directory name and file name in 'uploads'. $_FILES['file']['name'];? Commented Feb 5, 2017 at 16:23
  • Yeah, I realised this mistake, however, it is still unable to store the image to the "uploads" file after I changed the script. Commented Feb 5, 2017 at 16:37
  • you went and edited your question using code provided from an answer given without it being marked as an additional edit. I have rolled the question back to its original state; please don't do that, unless you make an additional copy of the new code you are using under the original and marked as something like: "I tried this from the answer given, but it did not work". Commented Feb 5, 2017 at 19:24
  • You also need to include the complete <form> for this along with the input for the file. There could be something in there that isn't right. Commented Feb 5, 2017 at 19:25
  • Read the manual on uploading files also php.net/manual/en/features.file-upload.post-method.php it will help you. Right now, this $file = 'uploads'. $_FILES['file']['name'];' interprets to and for example: uploadsIMAGE.jpg rather than uploads/IMAGE.jpg using $file = 'uploads/'. $_FILES['file']['name']; - also make sure the path is correct and that the folder has proper permissions to write to it. Commented Feb 5, 2017 at 19:30

2 Answers 2

1

Change this

$file = 'uploads'. $_FILES['file']['name'];

to this

$file = '../uploads/'. $_FILES['file']['name'];

Your actual codes searches for upload folder into uploadTesting folder where your code is located.

Sign up to request clarification or add additional context in comments.

1 Comment

yeah, I changed it to according to your script. I am not sure why the picture is still unable to save the the "uploads" file. I have updated my code on top.
0

You need to provide the relative path to your upload directory from your code. For ex. If upload directory is one folder out of where your file is.

'../uploads/'.$fileName

Also, make sure that you have the write permission for that directory.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.