我正在将一个rsa私钥(.pem文件)上传到我的网站并使用fopen来读取内容,但最近我一遍又一遍地得到同样的错误而没有多大意义.
<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";
}
?>
该网站不断返回错误:
Warning: fopen(mykey.pem)
[function.fopen]: failed to open
stream: No such file or directory in
…. on line 38
(这是fopen行).它进入fopen状态的事实向我证明它确实上传但是使用fopen(也尝试使用file_get_contents($_ FILES [‘uploaded_file’] [‘name’]);以及失败)返回错误.
什么da puh!
最后的说明:
无论出于何种原因,fopen()不喜欢读取OpenSSL证书并返回错误:
openssl_private_decrypt(): supplied
resource is not a valid OpenSSL
X.509/key resource
如果你使用file_get_contents它会工作.
解决方法:
我相信你正试图打开一个错误的文件(名字).由于我在代码中的任何地方都没有看到move_uploaded_file(),您可能应该从$_FILES数组中读取tmp_name而不是name:
$fh = fopen($_FILES['uploaded_file']['tmp_name'], 'r');