将图像上传到服务器时,无法读取php文件

时间:2014-12-29 06:11:11

标签: php ios mysql file-upload titanium

我尝试从图库中获取图片,并使用钛金属网络服务将所选图片上传到服务器。

我使用了下面的代码。但是我收到了调试错误:HTTP错误并且它还显示警告框,例如"连接期间出现错误"

此代码在我的开发服务器中正常运行。但它无法在我的客户端服务器中运行。原因是什么?为什么我的代码无法在我的客户端服务器上运行?

从Android设备上传文件时文件上传工作正常。但是从iphone设备上传文件时它无法正常工作。您能否给我一个解决此问题的建议?

为什么我的控制台窗口出现此错误。

 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]
});
}

修改

这是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;
}

?>

修改

我收到以下错误:

:[DEBUG] HTTP错误 :[信息]错误{&#34;输入&#34;:&#34;错误&#34;,&#34;来源&#34;:{&#34;缓存&#34;:false}, &#34;代码&#34;:404,&#34;错误&#34;:&#34; HTTP错误&#34;,&#34;成功&#34;:false}

如果我已经调用相同的url并单独传递manager_id,那么结果很好。如果我已经传递了manager_id和文件,这次只会得到Http错误。我无法找到确切的问题。因为相同的钛代码和PHP代码(开发服务器)工作正常,图像上传到开发服务器文件夹。但我已将相同的php文件移动到我的客户端server.now它无法正常工作。同样的网络服务网址在浏览器中工作正常,而android.it只能在iphone中工作。那么我究竟无法找到问题所在?能不能给我一个解决方案。

编辑: 请参考以下链接:

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

我面临着一个完全相同的问题。请你给我一个解决方案。

2 个答案:

答案 0 :(得分:0)

我发现了很多像这样的问题(例如The 'Passive' connection '<appname>' access to protected services is denied)。

答案总是:
&#34;这个错误被称为&#34; Red Herring&#34;。这是一个令人误解的线索。 HID不是影响您的应用的真正错误。应该有其他消息可以指示发生了什么。&#34; 所以看看是否有其他错误massege描述了你的问题。

例如,尝试在sql语句中转义您正在使用的文件名:

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

答案 1 :(得分:0)

确保您的设备已连接到互联网,然后按以下方式尝试:

钛:

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

有关详情,请查看以下链接:http://code.tutsplus.com/tutorials/titanium-mobile-build-an-image-uploader--mobile-8860

如果它像这样工作,你将不得不再次添加你的逻辑(resizing和manager_id)