-1

I am trying to write php in order to insert a picture (or a file) in an html document. The code I have is the following, but I can't seem to manage to upload the picture. This is a part of form validation project. The code I have so far is the following.

<article>
    <figure>

<?php
if(isset($_POST['submit'])){

    $file = $_FILES['file'];
    $file_name = $_FILES['file']['name'];
    $file_type = $_FILES['file']['type'];
    $file_size = $_FILES['file']['size'];
    $file__temp_name = $_FILES['file']['tmp_name'];
    $file_error = $_FILES['file']['error'];

    $fileExt = explode('.', $file_name);
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array('jpg', 'jpeg', 'png');

    echo '<img src="uploads/'.$file_name.'"/>';

    if (in_array($fileActualExt, $allowed)){
        if ($file_error === 0){
            if ($file_size < 1000000){
                $file_name_new = uniqid('', true).".".$fileActualExt;
                $file_destination = 'uploads/'.$file_name_new;
                move_uploaded_file($file__temp_name, $file_destination);

            } else {
                echo "Your file is too big!";
            }
        } else {
            echo "There was an error uploading your file!";
        }
    } else {
        echo "You cannot upload files of this type";
    }
}
?>
    </figure>
</article>


<div id="form">
...
<form enctype="multipart/form-data" method="POST" action="http://localhost/form.php">

    <input type="file" name="file">
    <button type="submit" name="submit">Upload</button>
    <p style="font-size: small ; margin-bottom: 10px ; margin-top: 5px">Browse for a photo</p>

...

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
immalee
  • 43
  • 7

1 Answers1

0

If you do this

echo '<img src="uploads/'.$file_name.'"/>';

Before you have done this

move_uploaded_file($file__temp_name, $file_destination);

Then there will be NO IMAGE there to find!

So move this line down after the move_uploaded_file() and use the Correct file name in the src= attribute

echo '<img src="uploads/'.$file_name_new.'"/>';

Or use the variable you created with the full path in

echo "<img src='$file_destination'/>";
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • thank you, I was just looking at that, because it was only sth i tried. I had the "echo" statement after the uploaded file, but still I can't see it. Sorry for that. – immalee Jan 15 '20 at 16:35
  • move_uploaded_file($file__temp_name, $file_destination); echo ''; This is what i was using, but the file doesn't upload. :( – immalee Jan 15 '20 at 16:37
  • So see the answer again. You are not using the correct File Name because you created a file name with other unique characters in it – RiggsFolly Jan 15 '20 at 16:39
  • Thank you, I will see into it again. – immalee Jan 15 '20 at 16:41
  • Unfortunately I can't make it work. It won't even move to the folder "uploads" in the first place, let alone show in my final webpage. Thank you for trying to help me! – immalee Jan 15 '20 at 17:26