-2

I have some code that lets you upload an image to a file and then displays it on the page once uploaded. The only problem is that the image wont display?

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
//echo $target_file;
$uploadOk = 1;
//$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);




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






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





// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {




    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}



/* Displaying Image*/
//$image=$_FILES["fileToUpload"]["name"]; 
//      $img="uploads/".$image;

echo "<br>";

                        //echo'<br><img src="'.$img.'">';
                        //echo "<br><img src=\"upload/$img\">";


echo '<br><img src="$target_file">';

Here is the part where i have the image being displayed:

 /* Displaying Image*/

    echo "<br>";

    echo '<br><img src="$target_file">';
Smeaj
  • 1
  • 1
  • Tell us, when you view HTML source, what does `echo '
    ';` show you? and for the others that are commented out?
    – Funk Forty Niner Mar 15 '17 at 13:47
  • You're upload to a directory, not to a file! ;) A image, a word document, a csv, pdf... that are all files. So you're not upload a image to a file, you're uploading a image to a directory.. or, for any document: You're upload a file to a directory. Just for next time ;) – Twinfriends Mar 15 '17 at 13:56

1 Answers1

1

Try:

echo '<br><img src="'.$target_file.'">';

or

echo "<br><img src='$target_file'>";
gmc
  • 3,910
  • 2
  • 31
  • 44