简体   繁体   English

无法将图片上传到服务器

[英]Unable to upload image to server

I have a file type and the button named "upload Image", where I select the file and upload the image. 我有一个文件类型和一个名为“上传图像”的按钮,在这里我选择文件并上传图像。 on clicking the upload image button image should be saved in the uploads folder. 单击上传图像按钮时,图像应保存在上传文件夹中。

On running in the localhost it works fine but fails to upload in the server. 在本地主机上运行时,它可以正常运行,但无法在服务器中上传。

This is my code: 这是我的代码:

$(document).ready(function() {
$('#upload').on('click', function() {
    var file_data = $('#fileToUpload').prop('files')[0];   
    var form_data = new FormData();                  
    form_data.append('file', file_data); 
    $.ajax({
                url: "http://mpp.ABCDEF.co.in/API/sendingInterface.php?fn= uploadImage", // point to server-side PHP script 
                dataType: 'text',  // what to expect back from the PHP script, if anything
                cache: false,
                contentType: false,
                processData: false,
                data: form_data,                         
                type: 'post',
                success: function(data) {
           $('#response').val(data);
        },
        error: function() {
        alert('Error');
        }
     });
});
}); 

Function of sendingInterface.php: sendInterface.php的功能:

if($_GET['fn'] =="uploadImage")
{
    $target_dir = $obj1->images_folder();
    $target_file = $target_dir . basename($_FILES['file']['name']);
    header ('Content-type: application/json');
    if (move_uploaded_file($_FILES['file']['tmp_name'], $target_file)) {
        http_response_code(200);
        $encoded = json_encode("{ success }");
        exit($encoded);
    } 
    else {
        http_response_code(200);
        $encoded = json_encode("{ failure }");    
        exit($encoded);
    }
}

Where $obj1->images_folder() is having the folder name in database as ../../MPP/images/uploads/ 其中$ obj1-> images_folder()在数据库中的文件夹名称为../../MPP/images/uploads/

On running in server I am getting the output as a alert box with message "Error". 在服务器上运行时,我得到的输出是带有消息“错误”的警报框。 How can I run it in server? 如何在服务器上运行它?

Try to manually set the full path to the upload folder - for an example: $target_dir="http://mpp.ABCDEF.co.in/API/MPP/images/uploads/" But you can't be sure with this relative path where the folder is. 尝试手动设置上传文件夹的完整路径-例如:$ target_dir =“ http://mpp.ABCDEF.co.in/API/MPP/images/uploads/”但您不能确定这一点文件夹所在的相对路径。 You have to find it by ftp-client or file manager - I don't know what is your hosting tools. 您必须通过ftp客户端或文件管理器找到它-我不知道您的托管工具是什么。 At all it is not good idea to use relative paths, because probably the config path and your base points are different. 毕竟,使用相对路径并不是一个好主意,因为可能配置路径和您的基点不同。

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

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