4

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>

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I am getting this error: Warning: move_uploaded_file(uploads/test1.xlsx): failed to open stream:
move_uploaded_file should receive 2 parameters, look again at my example. you missed the tmp_name of the source
In this ($_FILES["filepath"]["tmp_name"], $target_file); what is ["filepath"] ?
the "filepath" is from your example. its the name of the input file field
1
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>

Comments

0

U did not write code to upload file.

 move_uploaded_file()

2 Comments

Can you please elaborate it little?
u r saying that ur file is not uploading.Currently ur file is in temp directory. Youy have to use move_uploaded_file to move file to target directory.here is its full reference php.net/manual/en/function.move-uploaded-file.php
0

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)

1 Comment

That I am doing now, but this error I am getting: Warning: move_uploaded_file(uploads/test1.xlsx): failed to open stream, test.xlsx is in D:/ how system will come to know that it has to take it particularly from D: only and if some one has it in E: then ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.