PHP文件上传老是报错
我正在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您正在尝试打开错误的文件(名称)。由于我在代码中没有看到 move_uploaded_file() ,因此您可能应该从 $_FILES 数组中读取 tmp_name 而不是 name :
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:
尝试
try