anish
anish

Reputation: 43

How to upload excel file to php server from <input type="file">

I want to upload excel file using to php and wanted to perform some file operation in that excel file. I am not able to upload the file , If any one have some Idea please help , in the server side how to get the path from where the file has been uploaded?

$target_dir = 'uploads/'; is not working for me. and My file is in D: Please help.

PHP CODE:

 <?php    
if(isset($_POST['SubmitButton'])){ //check if form was submitted

$target_dir = 'uploads/';
$target_file = $target_dir . basename($_FILES["filepath"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

require_once dirname(__FILE__) . '/Includes/Classes/PHPExcel/IOFactory.php';

$inputFileType = PHPExcel_IOFactory::identify($target_file);

$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($target_file);

$i=2;
$val=array();
$count=0;
for($i=2;$i<34;$i++)
{
$val[$count++]=$objPHPExcel->getActiveSheet()->getCell('C'.$i)->getValue();
}
//echo'<pre>';print_r($val);
}    
?>

HTML CODE:

<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file"  name="filepath" id="filepath"/></td><td><input type="submit" name="SubmitButton"/>
</body>

Upvotes: 4

Views: 40476

Answers (4)

Amranur Rahman
Amranur Rahman

Reputation: 1123

if(isset($_POST['SubmitButton'])){
  try {       //attached file formate
    $statement = $db->prepare("SHOW TABLE STATUS LIKE 'table_name'");
    $statement->execute();
    $result = $statement->fetchAll();
    foreach($result as $row)
        $new_id = $row[10]; //10 fixed

    $up_filename=$_FILES["filepath"]["name"];
    $file_basename = substr($up_filename, 0, strripos($up_filename, '.')); // strip extention
    $file_ext = substr($up_filename, strripos($up_filename, '.')); // strip name
    $f2 = $new_id . $file_ext;

    move_uploaded_file($_FILES["filepath"]["tmp_name"],"uploads/" . $f2);

   // Client's info Insert MySQl 
    $statement = $db->prepare("INSERT INTO table_name (files) VALUES (?)");
    $statement->execute(array($f2));

    $success_message = "Excel file upload successfully!";
}
catch(Exception $e) {
        $error_message = $e->getMessage();
}
}

Form code:

 <form action="" method="post" enctype="multipart/form-data"> <input
 type="file"  name="filepath" id="filepath"/></td><td><input
 type="submit" name="SubmitButton"/>
 </form>

Upvotes: 1

Jeremy Board
Jeremy Board

Reputation: 181

You have to move your file from the temporary upload directory to the directory you want it in with the move_uploaded_file() function.

There are 2 arguments you need to put for the move_uploaded_file() function - the temporary file you just uploaded (ex. $_FILES["file"]["tmp_name"]), and then the directory you want to move it to... So it would end up looking something like this: move_uploaded_file($_FILES["file"]["tmp_name"], $path_of_new_file)

Upvotes: 0

Roey Haim
Roey Haim

Reputation: 73

You first need to upload the file before the read line:

$target_dir = 'uploads/';
$target_file = $target_dir . basename($_FILES["filepath"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

move_uploaded_file($_FILES["filepath"]["tmp_name"], $target_file);

// rest of your code...

now you should be able to continue working on the file. before this, the file never have been in your uploads folder.

Upvotes: 1

Munish Chechi
Munish Chechi

Reputation: 135

U did not write code to upload file.

 move_uploaded_file()

Upvotes: 0

Related Questions