PHP文件上传老是报错

发布于 2024-09-30 15:00:12 字数 1652 浏览 8 评论 0原文

我正在将 rsa 私钥(.pem 文件)上传到我的网站,并使用 fopen 读取内容,但最近我一遍又一遍地收到相同的错误,但没有多大意义。

HTML

  <form enctype="multipart/form-data" action="upload.php" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
    Choose a file to upload: <input name="uploaded_file" type="file" />
    <input type="submit" value="Upload" />
  </form> 

Upload.php

<?php
session_start();

if( $_SERVER['SERVER_PORT'] == 80) {
    header('Location:https://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"]); 
    die();
} 



echo $_FILES['uploaded_file']['name'];

//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "pem") && ($_FILES["uploaded_file"]["type"] == "text/plain")) {

$fh = fopen($_FILES['uploaded_file']['name'], 'r');

//continue with code
  } else {
     echo "Error: Only pem files are accepted for upload";
  }
} else {
 echo "Error: No file uploaded";
}
?>

网站不断返回错误:

警告:fopen(mykey.pem) [function.fopen]:打开失败 流:没有这样的文件或目录 ....第 38 行

(即 fopen 行)。它进入 fopen 状态的事实向我证明它实际上确实上传但使用 fopen (也尝试使用 file_get_contents($_FILES['uploaded_file']['name']); 也未成功)返回错误。

什么达噗!

最后说明:

无论出于何种原因,fopen() 不喜欢读取 OpenSSL 证书并返回错误:

openssl_private_decrypt():提供 资源不是有效的 OpenSSL X.509/关键资源

如果您使用 file_get_contents 它可以工作。

I am uploading an rsa private key (.pem file) to my website and using fopen to read the contents but lately I keep getting the same error over and over without making much sense.

HTML

  <form enctype="multipart/form-data" action="upload.php" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
    Choose a file to upload: <input name="uploaded_file" type="file" />
    <input type="submit" value="Upload" />
  </form> 

Upload.php

<?php
session_start();

if( $_SERVER['SERVER_PORT'] == 80) {
    header('Location:https://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"]); 
    die();
} 



echo $_FILES['uploaded_file']['name'];

//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "pem") && ($_FILES["uploaded_file"]["type"] == "text/plain")) {

$fh = fopen($_FILES['uploaded_file']['name'], 'r');

//continue with code
  } else {
     echo "Error: Only pem files are accepted for upload";
  }
} else {
 echo "Error: No file uploaded";
}
?>

The site keeps returning the error:

Warning: fopen(mykey.pem)
[function.fopen]: failed to open
stream: No such file or directory in
.... on line 38

(which is fopen line). The fact it gets to the fopen state proves to me it does in fact upload but using fopen (also tried using file_get_contents($_FILES['uploaded_file']['name']); as well unsuccessfully) returns an error.

what da puh!

FINAL NOTES:

For whatever reason, fopen() does not like reading OpenSSL certificates and returns an eror:

openssl_private_decrypt(): supplied
resource is not a valid OpenSSL
X.509/key resource

if you use file_get_contents it works however.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只等公子 2024-10-07 15:00:12

我相信您正在尝试打开错误的文件(名称)。由于我在代码中没有看到 move_uploaded_file() ,因此您可能应该从 $_FILES 数组中读取 tmp_name 而不是 name

$fh = fopen($_FILES['uploaded_file']['tmp_name'], 'r');

I believe you're trying to open a wrong file(name). Since I don't see move_uploaded_file() anywhere in your code, you should probably be reading the tmp_name instead of name from the $_FILES array:

$fh = fopen($_FILES['uploaded_file']['tmp_name'], 'r');
捎一片雪花 2024-10-07 15:00:12

尝试

  $fh = fopen($_FILES['uploaded_file']['tmp_name'], 'r');

try

  $fh = fopen($_FILES['uploaded_file']['tmp_name'], 'r');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文