PHP MYSQL Lua上传图片

时间:2018-02-21 17:37:47

标签: php mysql lua corona

我正在尝试使用corona sdk(Lua)和PHP将个人资料图片上传到我的MYSQL数据库,但没有发生任何事情。当我单击按钮选择它工作的图像。当我选择图像时,我直接进入profile.lua场景,没有任何反应。我没有收到任何错误,所以我不知道发生了什么。有人可以帮我解决我的代码吗?

upload.lua:

local MultipartFormData = require("class_MultipartFormData")
local multipart = MultipartFormData.new()
local path=system.pathForFile( "image.jpg", system.TemporaryDirectory )
multipart:addFile("Image", path, "image/jpg", "image.jpg")

local params = {}        
params.body = multipart:getBody()
params.headers = multipart:getHeaders() -- Headers not valid until getBody() is called.

network.request("http://hash.x10host.com/cgi-bin/hash/upload.php", "POST", listener, params)

local image 

local mime = require "mime"

local bkgd = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
bkgd:setFillColor( 0, 0, 0 )

local myRoundedRect = display.newRoundedRect(10, 50, 80, 50, 12)
myRoundedRect.strokeWidth = 3
myRoundedRect:setFillColor(140, 140, 140)
myRoundedRect:setStrokeColor(180, 180, 180)

local sessionComplete = function(event) 
        image = event.target

        print( "Camera ", ( image and "returned an image" ) or "session was cancelled" )
        print( "event name: " .. event.name )
        print( "target: " .. tostring( image ) )

        if image then
                -- center image on screen

                image.x = display.contentWidth/2
                image.y = 59
                local w = image.width
                local h = image.height
                image.xScale = 0.3
                image.yScale = 0.3
                print( "w,h = ".. w .."," .. h )
        end
end

local listener = function( event )
        if media.hasSource( media.Camera ) then
                media.show( media.Camera, sessionComplete )
        else
                native.showAlert("Corona", "Camera not found.")
        end
        return true
end
myRoundedRect:addEventListener( "tap", listener )


local myRoundedRect1 = display.newRoundedRect(10, 400, 150, 50, 12)
myRoundedRect1.strokeWidth = 3
myRoundedRect1:setFillColor(140, 140, 140)
myRoundedRect1:setStrokeColor(180, 180, 180)

local Name = "Imagename"


function uploadBinary ( filename, url, onComplete )

       -- local path = system.pathForFile( filename )
       -- local fileHandle = io.open( path, "rb" ) 
       -- if fileHandle then 

       if image then

                     local params = {
                                    body = "image_file=" .. mime.b64(tostring( image )) .. "&image_filename="..Name
                        }


           --    io.close( fileHandle )

                local function networkListener ( event )
                        if (onComplete) then
                                        onComplete(event);
                        end
                        return true;
                end

                network.request( url, "POST", networkListener,  params)
        end
end

local function networkListener( event )
        if ( event.isError ) then
                print( "Network error!")
        else
              --  print ( "RESPONSE: " .. event.response)
              print ("Working")
        end
end

local function Upload ()

uploadBinary ( image, "http://www.test1.bugs3.com/Corona.php", networkListener)
end 




myRoundedRect1:addEventListener( "tap", Upload )

upload.php的:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

session_start();

$con = mysqli_connect("localhost", "hashx10h_brandon", "bigman23", "hashx10h_hash");

$username = $_SESSION['username'];

if(isset($_FILES['image'])){
  $errors= array();
  $file_name = $_FILES['image']['name'];
  $file_size = $_FILES['image']['size'];
  $file_tmp = $_FILES['image']['tmp_name'];
  $file_type = $_FILES['image']['type'];
  $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

  $extensions= array("jpeg","jpg","png");

  if(in_array($file_ext,$extensions)=== false){
     $errors[]="extension not allowed, please choose a JPEG or PNG file.";
  }

  if($file_size > 2097152) {
     $errors[]='File size must be 2 MB';
  }

  if(empty($errors)==true) {
     move_uploaded_file($file_tmp,"uploads/".$file_name);

    $store=mysqli_query($conn,"UPDATE users SET userPic='$userPic' WHERE username='$username'");
    mysqli_query($conn,$store);
     echo "Success";
  }else{
     print_r($errors);
     echo"it failed";
  }
 }

1 个答案:

答案 0 :(得分:0)

此:

multipart:addFile("Image", path, "image/jpg", "image.jpg")

需要这样:

multipart:addFile("image", path, "image/jpg", "image.jpg")

然后它应该工作。