我無法使用php將文件上載到目錄中。我在下面解釋我的代碼。無法使用PHP將圖像上傳到文件夾中
$_FILES=array('picture'=>array('name' => 'IMG-20161121-WA0000.jpg','type' => ' image/png','tmp_name' => '/tmp/phpSb6a53', 'error' => 0, 'size' => 119198));
$imageFieldName = 'picture';
$imagePath = "admin/uploads/";
uploadImage($_FILES,$imageFieldName,$imagePath,function($image){
print_r($image);exit;
}
public function uploadImage($files, $imageFieldName, $imageDirPath, $callback) {
$result = array();
;
$imageName = generateRandomNumber() . '_' . $_FILES[$imageFieldName]['name'];
// echo($_FILES[$imageFieldName]['tmp_name']);exit;
$target_dir = $imageDirPath;
$target_file = $target_dir . basename($imageName);
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
if (file_exists($target_file)) {
$result['msg'] = "Sorry, file already exists.";
$result['num'] = 0;
$callback($result);
$uploadOk = 0;
}
if ($_FILES[$imageFieldName]["size"] > 500000) {
// echo 'fileSize';exit;
$result['msg'] = "Sorry, file size is large.";
$result['num'] = 0;
$callback($result);
$uploadOk = 0;
}
if (($imageFileType != "jpg" && $imageFileType != "JPG") && $imageFileType != "png" && $imageFileType != "jpeg") {
$result['msg'] = "Sorry, only .jpg,.jpeg and .png files are allowed.";
$result['num'] = 0;
$callback($result);
$uploadOk = 0;
}
if ($uploadOk == 0) {
$result['msg'] = "Sorry, Your file could not uploaded.";
$result['num'] = 0;
$callback($result);
} else {
if (move_uploaded_file($_FILES[$imageFieldName]['tmp_name'], $target_file)) {
$result['msg'] = "Image has uploaded successfully.";
$result['num'] = 1;
$result['img'] = $imageName;
$callback($result);
} else {
$result['msg'] = "Sorry, Your Image could not uploaded to the directory.";
$result['num'] = 0;
$callback($result);
}
}
}
選擇文件後我得到
$_FILES=array('name' => 'IMG-20161121-WA0000.jpg','type' => ' image/png','tmp_name' => '/tmp/phpSb6a53', 'error' => 0, 'size' => 119198);
輸入。
但我收到消息
`陣列([MSG] =>對不起,你的圖像不能上傳到 目錄。[NUM] => 0)
而print_r的。這裏我需要將圖像上傳到文件夾中。請幫幫我。
檢查目標目錄是否可寫 –
@RomaRush:是的,它是可寫的。 – subhra
我有這樣的目錄路徑。 'API/normaluser.php'在這個文件中我使用fileupload代碼,我想上傳'admin/uploads /'文件夾。'API和admin'文件夾位於一個根文件夾和'admin'文件夾'upload'文件夾內部目前我需要上傳圖片。 – subhra