简体   繁体   中英

file upload error using codeigniter

I am tryin to upload file in mysql database using php with codeigniter framework. Following my code. Its just saving first name of file in database but actual file in not storing at the given path.

My code is -

move_uploaded_file($_FILES["userfile"]["tmp_name"] , "uploads/diagnosis_report/".$_FILES["userfile"]["name"]);          

$data['file_name'] = $_POST["userfile"]["name"];

I believe:

$data['file_name'] = $_POST["userfile"]["name"];

Should be:

$data['file_name'] = $_FILES["userfile"]["name"];
// ---------------------^

$upload_path $_POST["userfile"]["name"] is just the file name. If you want to save it with the path you need to do something like this:

// For relative path
$data['file_name'] = "uploads/diagnosis_report/".$_FILES["userfile"]["name"];

// For absolute path
$data['file_name'] = dirname(__FILE__)."/uploads/diagnosis_report/".$_FILES["userfile"]["name"];

Honestly what I would do is set an upload path variable first like so:

$upload_path = dirname(__FILE__)."/uploads/diagnosis_report/";

Then you can use it over and over again like so:

move_uploaded_file($_FILES["userfile"]["tmp_name"] , $upload_path.$_FILES["userfile"]["name"]);          

$data['file_name'] = $upload_path.$_FILES["userfile"]["name"];

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM