简体   繁体   English

Ajax Php图像上传和保存

[英]Ajax Php Image Upload and Save

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. 此外,我也想在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. 但是,当我运行代码时,当我尝试上传图像时,文本继续显示“ Image Uploading ...”,并且从未完成实际的上传。 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. 更新:每次我尝试上传图像时,Ajax POST都会出错。 In fact, the POST request might not even reach my upload.php file. 实际上,POST请求甚至可能无法到达我的upload.php文件。 I trie d to alert myself when the request actually reaches upload.php but nothing ever prints out. 当请求实际到达upload.php时,我试图提醒自己,但是什么也没有打印出来。 What may be the potential causes of this? 可能是什么原因造成的?

UPDATE #2: I have included a picture of my file layout. 更新#2:我包括了我的文件布局的图片。 I have the HTML and Javascript in privatecreate.blade.php and the Javascript in upload.php. 我在privatecreate.blade.php中有HTML和Javascript,在upload.php中有Javascript。 I want to save the images in uploads folder. 我想将图像保存在上载文件夹中。

我的文件夹系统图片

Update #3: I printed out the ajax error and it is "No Input File Specified" 更新#3:我打印出ajax错误,它是“未指定输入文件”

Please bear with my everyone. 请和我所有人一起忍受。 This is my absolute first time working with php and sql and I am trying my hardest to learn. 这是我绝对第一次使用php和sql,我正在努力学习。

HTML: 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: 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):

<?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" />';
}
?>
<?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" />';
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM