PHP file_get_contents可以在本地运行,但不能在服务器上运行


PHP file_get_contents works on Local but not on server

我使用file_get_contents上传图像,我使用webcam.js捕获图像。它在我的本地服务器上工作,但当我把它移到在线服务器上时,它不工作,所以我试着搜索发生了什么,并把我带到allow_url_fopen,所以我检查我的cpanel,如果allow_url_fopen打开了,我导航PHP Selector | options,并证明它是on,所以我希望有人知道我下一步需要做什么。让它工作。

php:

<?php
//set random name for the image, used time() for uniqueness
$filename =  time() . '.jpg';
$filepath = '../images/profile/';
      $patient_id = $_SESSION['patient_current_id'];
      $sql  = "UPDATE patients SET ";
      $sql .= "image_path = '{$filename}' ";
      $sql .= "WHERE id = {$patient_id} ";
      $sql .= "LIMIT 1";
      $image = mysqli_query($con, $sql);
//read the raw POST data and save the file with file_put_contents()
$result = file_put_contents( $filepath.$filename, file_get_contents('php://input') );
if (!$result) {
    print "ERROR: Failed to write data to $filename, check permissions'n";
    exit();
}
echo $filepath.$filename;
?>

尝试使用cUrl而不是file_get_contents。

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 $data = curl_exec("http://www.example.com");
 curl_close($ch);
 $result = file_put_contents( $filepath.$filename, $data );
  • 尽管最有希望的原因可能是验证php.ini中allow_url_fopen是否设置为true
  • 它设置为TRUE,检查您的服务器管理员是否禁用了任何此类功能

或者,您也可以尝试使用CURL来获取文件的内容。