我有一個問題。我試圖使用PHP將文件上傳到文件夾中,但無法完成。我在下面解釋我的代碼。無法使用PHP將文件上傳到文件夾中
user.php的:
$target_dir = "admin/uploads/";
$target_file = $target_dir . basename($_FILES['file']['name']);
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
$uploadOk = 1;
$check = getimagesize($_FILES['file']['tmp_name']);
header('Content-Type: application/json');
if ($check !== false) {
$uploadOk = 1;
} else {
$uploadOk = 0;
}
if (file_exists($target_file)) {
$uploadOk = 0;
}
if ($uploadOk == 0) {
} else {
if (move_uploaded_file($_FILES['file']['tmp_name'], $target_file)) {
$result['msg'] = "Image has uploaded successfully.";
$result['num'] = 1;
$result['img'] =$_FILES['file']['name'];
} else {
$result['msg'] = "Sorry, Your Image could not uploaded to the directory.";
$result['num'] = 0;
}
}
這裏我得到消息Sorry, Your Image could not uploaded to the directory.
。在這裏我得到的輸入$_FILES
是像下面。
$_FILES=array('file'=>array('name' => 'IMG-20161121-WA0000.jpg','type' => ' application/octet-stream','tmp_name' => '/tmp/phpSb6a53', 'error' => 0, 'size' => 119198));
我也有文件夾寫權限。我的目錄結構如下所示。
root folder
->admin
=>uploads//(images need to saved)
-> API
=>V1
->user.php(//here is my file upload code)
在這種情況下,總是我無法將文件上傳到文件夾。請幫我解決這個問題。
可能是你的目錄沒有讀寫權限。將文件夾權限更改爲077 –
檢查目錄的權限。 – Samay
php error_log說什麼?該目錄不僅需要可寫,而且還由網絡服務器運行的同一用戶擁有(例如apache/nginx) - 假設這是一臺Linux服務器? – flauntster