所以我有一个html表单,它是传递我的图片(实际名称),但这是我所能得到的。当我试图获取tmp_name或类似的任何东西时,它显示了一个错误。
另外,如果我的html表单没有声明enctype
,我只能获得文件的名称。
<form action="{{ urlFor('account.profile.post') }}" method="post" autocomplete="off">
<!-- OTHER TEXT INPUTS THAT OUTPUT PERFECTLY FINE -->
<div class="form-group">
<label for="propic">Profile picture</label>
<input type="file" id="propic" name="propic">
</div>
</form>
PHP if($app->request->params('propic') != null) {
$propic = $app->request->post('propic');
$target_dir = "profs/$img_salt/";
$target_file = $target_dir.$propic;
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
if (move_uploaded_file($propic["tmp_name"], $target_file)) {
echo "The file ". basename( $propic["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
您必须将表单的编码类型设置为multipart/form-data
:
<form action="{{ urlFor('account.profile.post') }}" method="post" autocomplete="off" enctype="multipart/form-data">
如果没有此编码类型,您将无法发送带有该表单的文件。
在你的php中,从文件中抓取文件而不是post(不确定以下代码是否在你正在使用的php框架中这样做):
$propic = $app->request->files('propic');