我在 IIS 上设置了 php,并尝试在 CakePHP 中实现文件上传。
我已遵循文件上传的必要准则,但每次上传时,我都会不断收到以下带有uploadError
. 即使temp_name
是空的,也可能有什么问题。
最初认为 IIS 没有对临时文件夹的写入权限,但即使在授予IIS_IUSRS
和IUSR
array(5) {
["name"]=>
string(14) "DSC02474_2.JPG"
["type"]=>
string(0) ""
["tmp_name"]=>
string(0) ""
["error"]=>
int(1)
["size"]=>
int(0)
}
形式
<?php
echo $this->Form->create('User', array('class'=>'form-horizontal', 'action'=>'uploadProfilePicture', 'id'=>'change-picture', 'type'=>'file'));
echo $this->Form->input('id', array('value'=>$authUser['id']));
echo $this->Form->file('picture');
?>
<input type="submit" value="Upload" class="btn btn-info" name="submit">
<?php echo $this->Form->end(); ?>
模型处理程序
public function uploadFile( $check ) {
$uploadData = array_shift($check);
if ( $uploadData['size'] == 0 || $uploadData['error'] !== 0) {
return false;
}
$uploadFolder = APP.'uploads'.DS."profile-pictures";
$fileName = time()."_".$uploadData['name'];
$uploadPath = $uploadFolder . DS . $fileName;
if( !file_exists($uploadFolder) ){
mkdir($uploadFolder);
}
if (move_uploaded_file($uploadData['tmp_name'], $uploadPath)) {
$this->set('picture', $fileName);
return true;
}
return false;
}