使用PHP上傳文件時出現一個問題。我可以成功上傳圖片類型文件,但無法上傳PDF /文檔類型文件。我在下面解釋我的代碼。使用PHP無法上傳文檔/ pdf文件
$target_dir = "../../admin/enquiry/";
$fileName = generateRandom() . '_' . $_FILES['attachment']['name'];
$target_file = $target_dir . basename($fileName);
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
$uploadOk = 1;
$check = getimagesize($_FILES['attachment']['tmp_name']);
header('Content-Type: application/json');
if ($check !== false) {
$result['msg'] = "check not false.";
// echo json_encode(array('status' => $check['mime']));
$uploadOk = 1;
} else {
$result['msg'] = "check false.";
// echo json_encode(array('status' => 'upload failed'));
$uploadOk = 0;
}
if (file_exists($target_file)) {
echo json_encode(array('status' => 'file already exists'));
$uploadOk = 0;
}
if ($uploadOk == 0) {
//
} else {
if (move_uploaded_file($_FILES['attachment']['tmp_name'], $target_file)) {
$result['msg'] = "File has uploaded successfully.";
$result['num'] = 1;
$result['img'] =$fileName;
}else{
$result['msg'] = "Sorry, Your File could not uploaded to the directory.";
$result['num'] = 0;
}
}
這裏我得到消息check false.
意味着有一些問題getimagesize
。我也可以傳遞文件大小爲138kb,但在圖片的情況下,它的上傳成功,但其他類型的文件無法上傳,但我需要上傳這些。請幫助我。
打印您的$ _FILES並檢查文件實際上是上傳或任何其他問題?的print_r($ _ FILES);出口; – Ima
@Ima:我已經打印並在那裏收到了這條消息。 – satya
@RuhulAmin:請檢查我對你的帖子的評論。我按你的方式做了,但同樣的問題。 – satya