使用PHP的所有文件,如名称,大小和类型都显然是正确的,但文件无法移动到真正的文件夹

I am a little bit confuse and have a headache to find the upload file problem. I have the PHP script which is contained a simple code of uploading file. Below is my script:

$file_path = "doc_student/";
$image = $final_save_dir . $_FILES['uploadFile']['name'];

if($_FILES['uploadFile']['error'] > 0):
echo "Error: " . $_FILES['uploadFile']['error']. "<br>";
else:
echo "Upload: " .$_FILES['uploadFile']['name'] . "<br>";
echo "Type: " .$_FILES['uploadFile']['type'] . "<br>";
echo "Size: " .($_FILES['uploadFile']['size'] / 1024) . "kb<br>";
echo "Stored in: " .move_uploaded_file($_FILES['uploadFile']['tmp_name'], $file_path . $_FILES['uploadFile']['name']) . "<br>";
endif;

Everything's worked fine except the file that I upload is not move to the $file_path = "doc_student/".

one of my friend told me to give a proper permission to allow my files upload to the folder as I given below but how to give a permission in Ubuntu?

use chown or chmod, chmod -R 777 doc_sudent is easy but not safe. chown -R owner.group doc_sudent, owner and group should be the run user and group of php

There are two mistakes you are doing in your code.

  1. $image = $final_save_dir . $_FILES['uploadFile']['name']; should be like this $image = $file_path . basename($_FILES['uploadFile']['name']); which is the filename with the proper path where you want to upload the file.

  2. move_uploaded_file($_FILES['uploadFile']['tmp_name'], $file_path . $_FILES['uploadFile']['name']) should be like this move_uploaded_file($_FILES['uploadFile']['tmp_name'], $image). The second argument of move_uploaded_file should be the filename with the path where you want to upload the file. And this function returns boolean value(either true or false ).

So you may use following method(if you want): // Check $_FILES['uploadFile']['error'] value. switch ($_FILES['uploadFile']['error']) { case UPLOAD_ERR_OK: break; case UPLOAD_ERR_NO_FILE: throw new RuntimeException('No file sent.'); case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: throw new RuntimeException('Exceeded filesize limit.'); default: throw new RuntimeException('Unknown errors.'); }

// You should also check filesize here.
if ($_FILES['uploadFile']['size'] > 1000000) {
    throw new RuntimeException('Exceeded filesize limit.');
}

$file_path = "doc_student/";
$image = $file_path . basename($_FILES['uploadFile']['name']);

 if (!move_uploaded_file($_FILES['uploadFile']['tmp_name'], $image)) {
    throw new RuntimeException('Failed to move uploaded file.');
}

echo "File is uploaded successfully.<br>";
echo "Upload: " .$_FILES['uploadFile']['name'] . "<br>";
echo "Type: " .$_FILES['uploadFile']['type'] . "<br>";
echo "Size: " .($_FILES['uploadFile']['size'] / 1024) . "kb<br>";

Here is the official reference: http://in3.php.net/manual/en/features.file-upload.php

For changing the permissions on ubuntu use command chmod 777 <folder name> Change the folder name in the above command to the folder you are uploading with full path.