Hi this is scorpionz
I am uploading an excel sheet file using PHP
Now the file code is working fine for gif files but not working if i provive xls extension

Here is the php main code

<?php
	// include 'config.php'
	include 'uploadcode.php';
	
	if(isset($_POST['upload_file']))
	{
		$user_file			=	$_FILES["car_info_file"]["name"];
		upload_files($_FILES["car_info_file"]["name"]);
		
	}
	
?>

<body>
<form name="uploadfile" action="" method="post" enctype="multipart/form-data">
	<div class="upload-box">
		<div style='border-left-width:0px;border-right-width:0px;border-top-width:0px;border-bottom-width:1px;border-color:#C0C0C0;border-style: solid; background-color:#669900;text-align:center; width:'>
			<a href="#">Upload File </a></div>
		</div>
		<div>
			<table class="bodyBg" bgcolor="#333333">
				<tr>
					<td width="94" align><span class="style1">Upload Your Display </span></td>
		      		<td width="246"><input type="file" name="car_info_file" id="car_info_file" style="height: 19px; width:100px; font-size:87%;"/></td>
				</tr>
				<tr>
			 	 	<td colspan="2" align><input type="submit" name="upload_file" value="Upload" /></td>
	  			</tr>
			</table>
		</div>
	</div>
</form>
</body>

The above is the main file and in PHP block i am calling a PHP function with name upload_files.... and below is that php function code

function upload_files($fil)
{

if((!empty($_FILES["car_info_file"])) && ($_FILES['car_info_file']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 350Kb
  $filename = basename($_FILES['car_info_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  //echo $ext;
  if (($ext == "xls") && ($_FILES["car_info_file"]["type"] == "text/xls" ) && ($_FILES["car_info_file"]["size"] < 2000000)) {
    //Determine the path to which we want to save this file
	 // $newname = 'C:\xampp\htdocs\online_shopping\uploaded_images\$filename';
	  $newname = 'C:\xampp\htdocs\hblcars\file_folder\\'.$filename;
     // echo $newname;
	  //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['car_info_file']['tmp_name'],$newname))) {
          // echo "It's done! The file has been saved and Uploaded as: ".$newname;
		   echo "<div style='background-color:#C0C0C0;color:#0099CC;font-weight:bold;text-align:center;'>It's done! The file has been saved and Uploaded</div>";
        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         echo "Error: File ".$_FILES["car_info_file"]["name"]." already exists";
      }
  } else {
     echo "Error: Only .XLS files under 200000Kb are accepted for upload";
  }
} else {
 echo "Error: No file uploaded";
}
}

I think the Problem is here
if (($ext == "xls") && ($_FILES["car_info_file"]["type"] == "text/xls" ) && ($_FILES["car_info_file"]["size"] < 2000000))
because i am getting this error: Error: Only .XLS files under 200000Kb are accepted for upload

Thanks and Regards
Scorpionz

The problem is,

if((!empty($_FILES["car_info_file"])) && ($_FILES ==

and all will not be accessible inside the function. Instead, pass $_FILES as parameter for the function.

upload_files($_FILES);

Then you can access it in your function like,
$fil and so on..

Btw, type is application/xls..

Thanks Naveen for Replying
But the things u told are not making any effect

in fact let me clear to you that if i use this line as image

if (($ext == "gif") && ($_FILES["car_info_file"]["type"] == "image/gif" ) && ($_FILES["car_info_file"]["size"] < 2000000))

It does work

but not working with this below

if (($ext == "xls") && ($_FILES["car_info_file"]["type"] == "application/xls" ) && ($_FILES["car_info_file"]["size"] < 2000000))

I use this

upload_files($_FILES);

But no benefit at all....

IF you have some better code send here in which it covers all type of files with Sizing issue i am also not comfortable with sizing issue

Thanks and Regards
Scorpionz

I got......
Error: Only .XLS files under 200000Kb are accepted for upload

Then I remove $_FILES["car_info_file"]["type"] == "text/xls" ) && ($_FILES["car_info_file"]["size"] < 2000000)
from if condition


now file is uploaded but I got error like....

File is already exists..... :-)

Hi,

I guess this might come late, but i just figured out what is wrong...

$_FILES["car_info_file"]["type"] == "application/xls"

should me changed by

$_FILES["car_info_file"]["type"] == "application/vnd.ms-excel"

I have been googling, and looks that is the problem, since a MS-excel document has a specific type of file.

Hope I've helped :)

Vaishali

You can also use ,

//Upload excel file:
$_FILES["car_info_file"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ...

plz give me coding for only excel file accept in php......

plz help me.....

Are you trying to upload files of the extention of only xls, xlsx or both?

how to set online path for above function..??

$newname = 'http://localhost/sirfpaisa-newone/admin//'.$filename;

instead of

$newname = 'D:\wamp-new\www\sirfpaisa-newone\admin\\'.$filename;

To anyone who had been wonderstruck about some files being easily uploaded and some not, it could be a size issue. I'm sharing this as I was stuck with my PHP code not uploading large files and I kept assuming it wasn't uploading any Excel files. So, if you are using PHP and you want to increase the file upload limit, go to the php.ini file and make the following modificatons:

upload_max_filesize = 2M

to be changed to

upload_max_filesize = 10M
post_max_size = 10M

or the size required. Then restart the Apache server and the upload will start magically working. Hope this will be of help to someone.

after this uploading i have to store the datas into database..how it possible?

Hai;

I think the below example will help you

<?php
if(isset($_FILES["txtFile"])) {
$allowedExts = array("doc", "docx", "txt", "pdf");
$extension = end(explode(".", $_FILES["txtFile"]["name"]));
$message = "";
if ((($_FILES["txtFile"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|| ($_FILES["txtFile"]["type"] == "application/pdf")
|| ($_FILES["txtFile"]["type"] == "application/msword")
|| ($_FILES["txtFile"]["type"] == "text/plain"))
&& ($_FILES["txtFile"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["txtFile"]["error"] > 0)
    {
    $message.= "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {   
    $tempFile = $_FILES['txtFile']['tmp_name'];
    $targetPath = 'admin/images/uploads/CV/';
    $newFileName =$_FILES['txtFile']['name'];
    $targetFile =  str_replace('//','/', $targetPath) . $newFileName;

    $cpy=1;
    do {


        $targetFile =  str_replace('//','/',$targetPath) ."Copy".$cpy. $_FILES['txtFile']['name'];
            $fileName="Copy".$cpy. $_FILES['txtFile']['name'];
            $cpy++;
        }while(is_file($targetFile));

        move_uploaded_file($tempFile,$targetFile);

        //here you can call your database and set up your insert query..
        //in this example I use an oop's for insert data
        $myObject->insertDataFunction(addslashes($_REQUEST['txtname']),addslashes($_REQUEST['txtemail']),($_REQUEST['txtPhone']),addslashes($_REQUEST['txaComments']),$fileName,$_REQUEST['id']);
        }
        ?>

okei thank u soo mchh..i'l work it and confirm..
by the way am having another doubt is that..how it is possible by accesing the database datas into excelsheet..
i need to download the excel sheet from the webpage itself.
plz help me

i ddnt get this :(

Member Avatar for diafol

See Bachov's link and use his code or check the link in the last post

Here is an example

<?php 

if(isset($_POST["submit"])) {

    $target_path = "uploads/";
    $target_path = $target_path . basename( $_FILES['fileToUpload']['name']); 

    if ($_FILES["fileToUpload"]["type"] == "application/xls" or $_FILES["fileToUpload"]["type"] == "application/vnd.ms-excel" or $_FILES["fileToUpload"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"){
        if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path)) {
            echo "The file ".  basename( $_FILES['fileToUpload']['name']). 
            " has been uploaded";
        } else{
            echo "There was an error uploading the file, please try again!";
        }
    }else{
        echo "This is not an excel file, please try again!";
    }
}
// nexxoz.com
?>
<!DOCTYPE html>
<html>
<body>

<form action="" method="post" enctype="multipart/form-data">
    <p>Select excel to upload:</p>
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>

What PHP code for uploading Excel sheet ???? good anwer,..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.