dsbezji539113152 2015-04-22 05:35
浏览 17

无法将图像上传到服务器

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:

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/

On running in server I am getting the output as a alert box with message "Error". How can I run it in server?

  • 写回答

1条回答 默认 最新

  • doutingyou2198 2015-04-22 05:48
    关注

    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. You have to find it by ftp-client or file manager - I don't know what is your hosting tools. At all it is not good idea to use relative paths, because probably the config path and your base points are different.

    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染