如何使用以下代码在PHP上传文件?

时间:2015-03-12 09:06:40

标签: php

我想从我的本地服务器或系统上传文件,但它给我错误请帮助我 我的代码: -

$inimage = new StdClass();
$image= file_get_contents($_FILES['image']['tmp_name']); ;

$handle = fopen('$image', 'r');
$card_image = fread($handle, filesize($image));

fclose($handle);
$inimage->fileName = $image;    
$inimage->fileData = $card_image;
$params->OCRWSInputImage = $inimage;   

$settings = new StdClass();
$settings->ocrLanguages = array("ENGLISH");
$settings->outputDocumentFormat  = "TXT";
$settings->convertToBW = FALSE;
$settings->getOCRText = TRUE;
$settings->createOutputDocument = FALSE;
$settings->multiPageDoc = FALSE;
$settings->ocrWords = FALSE;
$params->OCRWSSetting = $settings;


try 
{
   $result = $client->OCRWebServiceRecognize($params);
} 
catch (SoapFault $fault) 
{
   print($client->__getLastRequest());
   print($client->__getLastRequestHeaders());
}

var_dump($result);
print("Done");
?>

BUt IT会出现以下错误: -

  

注意:未定义的索引:第18行的C:\ xampp1 \ htdocs \ google \ g.php中的图像

     

警告:file_get_contents():第18行的C:\ xampp1 \ htdocs \ google \ g.php中的文件名不能为空

     

警告:fopen($ image):无法打开流:第22行的C:\ xampp1 \ htdocs \ google \ g.php中没有此类文件或目录

     

警告:fread()期望参数1为资源,第23行的C:\ xampp1 \ htdocs \ google \ g.php中给出布尔值

     

警告:fclose()要求参数1为资源,第25行的C:\ xampp1 \ htdocs \ google \ g.php中给出布尔值   object(stdClass)#5(1){[" OCRWSResponse"] => object(stdClass)#6(3){[" ocrText"] => object(stdClass)#7(0){} [" errorMessage"] => string(25)"输入文件名不正确" [" ocrWSWords"] => object(stdClass)#8(0){}}} Don

1 个答案:

答案 0 :(得分:0)

您的HTML-Form-Data似乎没有名为“image”的文件。

此外,您以错误的方式使用file_get_contents。

//This reads the file and put its contents into the $imagevariable
$image= file_get_contents($_FILES['image']['tmp_name']); ;
//Here you are trying to open a file with a name that was stored in your uploaded file.
$handle = fopen('$image', 'r');
$card_image = fread($handle, filesize($image));

fclose($handle);

我建议你阅读file_upload上的W3C教程。

http://www.w3schools.com/php/php_file_upload.asp