1
votes

I want to upload an image and save it in the uploads folder without refreshing the page. Furthermore, I also want to display the uploaded image on the screen in a div. However, when I run the code, when I try to upload an image, the text continues to say "Image Uploading..." and never finishes to actually upload. Therefore, the image never gets displayed on the page. Additionally, I am having trouble saving my image in the uploads folder, so can someone point me in the right direction? Thank you!

UPDATE: The Ajax POST gets an error each time I try to to upload an image. In fact, the POST request might not even reach my upload.php file. I trie d to alert myself when the request actually reaches upload.php but nothing ever prints out. What may be the potential causes of this?

UPDATE #2: I have included a picture of my file layout. I have the HTML and Javascript in privatecreate.blade.php and the Javascript in upload.php. I want to save the images in uploads folder.

Picture of my Folder System

Update #3: I printed out the ajax error and it is "No Input File Specified"

Please bear with my everyone. This is my absolute first time working with php and sql and I am trying my hardest to learn.

HTML:

 <div class="container" style="width:700px;">
                                        <br />
                                        <label>Select Image</label>
                                        <input type="file" name="file" id="file" />
                                        <br />
                                        <span id="uploaded_image"></span>
                                    </div>

Javascript:

 $(document).ready(function(){
    $(document).on('change', '#file', function(){
        var name = document.getElementById("file").files[0].name;
        var form_data = new FormData();
        var ext = name.split('.').pop().toLowerCase();

        if(jQuery.inArray(ext, ['gif','png','jpg','jpeg']) == -1)
        {
            alert("Invalid Image File");
        }
        else{
            var oFReader = new FileReader();
            oFReader.readAsDataURL(document.getElementById("file").files[0]);
            var f = document.getElementById("file").files[0];
            var fsize = f.size||f.fileSize;
            if(fsize > 2000000)
            {
                alert("Image File Size is very big");
            }
            else
            {
                form_data.append("file", document.getElementById('file').files[0]);
                $.ajax({
                    url:"upload.php",
                    method:"POST",
                    data: form_data,
                    contentType: false,
                    cache: false,
                    processData: false,
                    beforeSend:function(){
                        $('#uploaded_image').html("<label class='text-success'>Image Uploading...</label>");
                    },
                    success:function(data)
                    {
                        $('#uploaded_image').html(data);
                    }
                    ,error: function(ts)
                    {
                        alert("error:" + ts.responseText);
                    }
                });
            }
        }
    });
});

PHP (upload.php):

<?php
//upload.php
$message = "Running Upload.php";
echo "<script type='text/javascript'>alert('$message');</script>";
if($_FILES["file"]["name"] != '')
{
    $test = explode('.', $_FILES["file"]["name"]);
    $ext = end($test);
    $name = rand(100, 999) . '.' . $ext;
    $location = 'uploads/' . $name;
    move_uploaded_file($_FILES["file"]["tmp_name"], $location);
    echo '<img src="'.$location.'" height="150" width="225" class="img-thumbnail" />';
}
?>
1
Why you have a . (dot) before /upload - if you want to go one level up you have to use two dots. - Ylber Veliu
Consider this resource youtube.com/watch?v=ut-NcYgFRKI I think this will solve your problem - Ylber Veliu
That's the resource I used to program my image uploader, but mine is not functioning correctly...However, I have been looking at this piece of code for a long time and cannot find where I went wrong. - Jennifer Zhou
Here you have the source code, get it and compare it with yours. webslesson.info/2017/02/… - Ylber Veliu
Add an error callback, check the console, do some kind of debugging - Musa

1 Answers

0
votes
<?php
//Change your location path
if($_FILES["file"]["name"] != '')
{
    $test = explode('.', $_FILES["file"]["name"]);
    $ext = end($test);
    $name = rand(100, 999) . '.' . $ext;
    $location = 'upload/' . $name; // change here & enjoy
    move_uploaded_file($_FILES["file"]["tmp_name"], $location);
    echo '<img src="'.$location.'" height="150" width="225" class="img-thumbnail" />';
}
?>