简体   繁体   English

将图像上传到钛合金服务器中时无法读取php文件

[英]Can't read php file when upload image to server in titanium

I have tried to get the image from gallery and upload the selected image to server using webservices in titanium. 我试图从图库中获取图像,然后使用钛中的Web服务将所选图像上传到服务器。

I have used below code. 我用下面的代码。 But am getting the debug error : HTTP error And also it shows the alert box like "There was an error during the connection" 但是正在收到调试错误:HTTP错误并且它还会显示警告框,例如“连接期间发生错误”

This code is working fine in my development server.But it is not working in my client server. 这段代码在我的开发服务器上可以正常工作,但是在我的客户端服务器上不能工作。 What's the reason ? 什么原因 ? why my code is not working in my client server ? 为什么我的代码在我的客户端服务器上不起作用?

The file upload is working fine when upload the file from android device.But it's not working while upload a file from iphone device.Can you please give me a idea to resolve this issue ? 从android设备上载文件时,文件上传工作正常。但是从iphone设备上载文件时,文件上传不起作用。能否给我一个解决此问题的想法?

Why am getting this error on my console window. 为什么在我的控制台窗口上出现此错误。

 function AUF_ADD_File_FolderData () { 
  Titanium.Media.openPhotoGallery({
  success:function(event) {
      var request = Ti.Network.createHTTPClient({ 
               onload : function(e) {
        Ti.API.info(this.responseText);
        Ti.API.info("image pathe"+" "+event.media);
   if(this.responseText == "Successfully file is created"){
             var managefolders =Alloy.createController('manage_folders').getView();
       managefolders.open();  
         }
         else{
             alert(this.responseText); 
         }
    }, 
              onerror: function(e){ 
                  Ti.API.debug(e.error); 
                  alert("There was an error during the connection"); 
              }, 
              timeout:20000, 
                 });    
                   var uploadabc = event.media.imageAsResized(400 , 400);
                       request.open("POST",url+"client/manager/at_manager_create_common_file.php"); 

                 var params = ({"manager_id": manager_id,"file": uploadabc,}); 
               // var params = ({"manager_id": manager_id,"file": event.media,});   
              request.send(params); 

},

    cancel:function() {
        // called when user cancels taking a picture
    },
    error:function(error) {
        // called when there's an error
        var a = Titanium.UI.createAlertDialog({title:'Camera'});
        if (error.code == Titanium.Media.NO_CAMERA) {
            a.setMessage('Please run this test on device');
        } else {
            a.setMessage('Unexpected error: ' + error.code);
        }
        a.show();
    },
    saveToPhotoGallery:false,
    // allowEditing and mediaTypes are iOS-only settings
    allowEditing:true,
    mediaTypes:[Ti.Media.MEDIA_TYPE_VIDEO,Ti.Media.MEDIA_TYPE_PHOTO]
});
}

EDIT: 编辑:

this is php file : 这是php文件:

    <?php  

$request            = base64_decode($_POST['jsondata']);

$data               = json_decode($request,true);
$manager_id         = $data['manager_id'];
$file_name          = $data['file_name'];
$source             = base64_decode($data['source']);

include "db_connect.php";
// connecting to db
$db = new DB_CONNECT();

$result     = mysql_query("SELECT * from at_common_files WHERE user_id = '$manager_id'  and file_name = '$file_name'");
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
    $response='{"Error":"1","Message":"Filename already existed"}';
    echo $response;
} else {
    $upload_dir = 'common_files/'.$manager_id."_".$file_name;

    file_put_contents($upload_dir,$source);
    $qugery     = mysql_query("insert into at_common_files (user_id,file_name) values ($manager_id, '$file_name') ");
    $response   = '{"Error":"0","Message":"Successfully file is created"}';
    echo $response;
}

?>

EDIT: 编辑:

As am getting the below error : 由于正在出现以下错误:

: [DEBUG] HTTP error : [INFO] IN ERROR {"type":"error","source":{"cache":false},"code":404,"error":"HTTP error","success":false} :[DEBUG] HTTP错误 :[INFO] IN错误{“类型”:“错误”,“源”:{“缓存”:false},“代码”:404,“错误”:“ HTTP错误”,“成功“:假}

if i have call the same url and pass a manager_id alone , am getting the results fine.if i have passing the manager_id and file, this time only am getting the Http error. 如果我调用了相同的URL并单独传递了一个manager_id,则结果很好。如果我传递了manager_id和文件,则这一次仅是Http错误。 i can't find a exact issue.Because the same titanium code and php code (development server)is working fine and the image is uploading to development server folder. 我找不到确切的问题。因为相同的钛代码和php代码(开发服务器)工作正常,并且图像正在上传到开发服务器文件夹。 but i have moved the same php file to my client server.now it is not working . 但是我已经将相同的php文件移到我的客户端server.now上不能用。 also the same web service url is working fine in browser and android.it's not working in iphone only.so that exactly i can't find where is the issue ? 同样的Web服务URL在浏览器和android中也可以正常工作。它不仅在iphone中不工作,所以我找不到问题出在哪里? can you please give me a solutions. 你能给我一个解决方案吗?

EDIT : please refer the below link: 编辑:请参考以下链接:

http://developer.appcelerator.com/question/174462/image-not-uploading-from-iphone#comment-224007 http://developer.appcelerator.com/question/174462/image-not-uploading-from-iphone#comment-224007

I have facing a exact same issue.could you please give me a solution. 我遇到了完全相同的问题。请给我一个解决方案。

i have found many questions like this (eg The 'Passive' connection '<appname>' access to protected services is denied ). 我发现了很多类似的问题(例如,“被动”连接“ <appname>”对受保护服务的访问被拒绝 )。

the answer is always: 答案总是:
"This error is what's known as a "Red Herring". It's a clue that's misleading. The HID isn't a real error that affects your app. There should be other messages that may indicate what's going on." “这个错误就是所谓的“红鲱鱼”。这是一个令人误解的线索。HID并不是影响您应用程序的真正错误。应该有其他消息可能表明正在发生的情况。” so look if there is a other error massege which describes your problem. 因此,请查看是否还有其他错误说明可以描述您的问题。

for example try to escape the filename you are using within the sql statements: 例如,尝试转义您在sql语句中使用的文件名:

$file_name = mysql_real_escape_string($data['file_name']);

Make sure your device is connected to the internet and then try it like this: 确保您的设备已连接到互联网,然后尝试如下操作:

Titanium: 钛:

function AUF_ADD_File_FolderData () { 
    Titanium.Media.openPhotoGallery({
        success:function(event) {

            var xhr = Titanium.Network.createHTTPClient();

            xhr.onerror = function(e){
                Ti.API.info('IN ERROR ' + JSON.stringify(e));
                alert('Sorry, we could not upload your photo! Please try again.');
            };

            xhr.onload = function(){
                Ti.API.info(this.responseText);
                Ti.API.info("image pathe"+" "+event.media);
                if(this.responseText == "Successfully file is created"){
                    var managefolders =Alloy.createController('manage_folders').getView();
                    managefolders.open();  
                }else{
                    alert(this.responseText); 
                }            
            };

            xhr.open('POST', url+"client/manager/at_manager_create_common_file.php");

            xhr.send({
                media: event.media,
                manager_id: manager_id
            });     

        },
        cancel:function() {
            // called when user cancels taking a picture
        },
        error:function(error) {
            // called when there's an error
            var a = Titanium.UI.createAlertDialog({title:'Camera'});
            if (error.code == Titanium.Media.NO_CAMERA) {
                a.setMessage('Please run this test on device');
            } else {
                a.setMessage('Unexpected error: ' + error.code);
            }
            a.show();
        },
        saveToPhotoGallery:false,
        // allowEditing and mediaTypes are iOS-only settings
        allowEditing:true,
        mediaTypes:[Ti.Media.MEDIA_TYPE_VIDEO,Ti.Media.MEDIA_TYPE_PHOTO]*/
    });
}

PHP: PHP:

<?php
    //this function returns a random 5-char filename with the jpg extension
    function randomFileName()
    {
       $length = 5;
       $characters = 'abcdefghijklmnopqrstuvwxyz';
       $string = '';    
       for ($p = 0; $p < $length; $p++) {
          $string .= $characters[mt_rand(0, strlen($characters))];
       }
       return $string . '.jpg';
    }

    //create the random filename string and uploads target variables
    $randomString = randomFileName();
    $target = 'common_files/'.$randomString;  


    if(move_uploaded_file($_FILES['media']['tmp_name'], $target))
    {
         echo "success";
    }
    else
    {   
         echo "moving to target failed"; 
    }
?>

For more info check this link: http://code.tutsplus.com/tutorials/titanium-mobile-build-an-image-uploader--mobile-8860 有关更多信息,请检查此链接: http : //code.tutsplus.com/tutorials/titanium-mobile-build-an-image-uploader--mobile-8860

If it works like this you will have to add your logic again (resizing and manager_id) 如果这样工作,您将不得不再次添加逻辑(调整大小和manager_id)

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

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