簡體   English   中英

PHP Server從Phonegap和Jquery上傳圖像

[英]PHP Server Upload an image from Phonegap and Jquery

好的,我有一個用於上傳圖像的小應用程序(使用phonegap,Jquery)。 我通過AJAX上傳jquery處理。 圖像正在發送但我不確定如何在severside正確處理它。 任何想法都非常歡迎! 到目前為止,這是我的代碼:

PHP:

<?php 
    ////////THE PROBLEM AREA I THNK//////////
    if ($_REQUEST['image']) {

            // convert the image data from base64
            $imgData = base64_decode($_REQUEST['image']);

            // set the image paths
            $file = '/uploaded_files/' . md5(date('Ymdgisu')) . '.jpg';
            $url = 'http://creativetree.co/creativetreeAlpha' . $file; 

            // delete the image if it already exists
            if (file_exists($file)) { unlink($file); }

            // write the imgData to the file
            $fp = fopen($file, 'w');
            fwrite($fp, $imgData);
            fclose($fp);
    }

    echo "<h1>Page is online</h1>";
    echo "<p>Nothing special just a first go at phonegap! </p>";
    echo "<h2>Contents of uploaded_files Folder to show iphone upload</h2>";

    $dir = '/homepages/22/d397139588/htdocs/creativetreeAlpha/uploaded_files';

    if ($handle = opendir($dir)) {
        while (false !== ($file = readdir($handle))) { 
            if ($file != "." && $file != "..") { 
                echo "$file\n";
                echo "<br>";
            } 
        }
        closedir($handle); 
    }
   ?>

基本HTML:

<script>
function onBodyLoad() {
document.addEventListener("deviceready",onDeviceReady,false);
}
</script>
</head>
<body onload="onBodyLoad()">
<body>
        <h1>Upload an image</h1>

        <div id="upload"    
        <input type="button" class="send-image" value="camera" />
        <input type="button" class="send-image" value="library" />
        <img style="width:60px; height:60px" id="image" src="" />
        </div>

<div id="output"></div>
</body>
</html>

使用Javascript:

$(document).ready(function(){
  $(document).bind('deviceready', function(){

  function sendImage(src) {

    src = (src == 'library') ? Camera.PictureSourceType.PHOTOLIBRARY : Camera.PictureSourceType.CAMERA;
    navigator.camera.getPicture(success, fail, {quality: 45, sourceType: src});

                             function success(imageData) {
                             var url = 'http://creativetree.co/creativetreeAlpha/mobileImageUpload.php';
                             var params = {image: imageData};

                             // send the data
                             $.post(url, params, function(data) {
                                     alert('sent');
                                     // Display the selected image on send complete
                                     $('#image').attr('src', 'data:image/jpeg;base64,' + params['image']);     
                             });
                             }
}

  function fail(message) { alert(message); }

  $('.send-image').click(function () { sendImage($(this).val()); });

  });
});

使用fwrite檢查要在其中創建映像的目錄的寫入權限。

簡而言之,我需要在我的在線服務器文件夾上設置權限。

另外值得注意的是任何使用phonegap並訪問URL的新iphone開發者(顯然對於AJAX至關重要)。 必須在Xcode項目中設置URL(PhoneGap.plist包含用於存儲外部URL的數組)。

對於某些人來說這可能是顯而易見的,但即使是PhoneGap似乎也沒有很好的記錄(或者至少我沒有在瘋狂的谷歌搜索中看到)。 我研究過的wiki教程沒有提到這一點。

有關詳情,請參閱圖片

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM