Jump to content

uploading csv files


ill-fated24

Recommended Posts

Hi everyone,

 

I got trouble uploading a csv file in php. Cant seem to figure out whats wrong, it always lead me to "nicetry" even though im sure im uploading a csv file.

 

upload_page

<?php
		
echo "<form action='upload_process.php' id='upload_form' method='post' enctype='multipart/form-data'>";
   echo "<table id='class1' align='center'>";
      echo "<tr>";
      echo "<th colspan=2>Upload</th>";
      echo "</tr>";
      echo "<tr>";
      echo "<td colspan=2><input type='file' name='file' id='file' size='50'/></td>";
      echo "</tr>";
      echo "<tr class='alt'>";
      echo "<td colspan=2><input type='submit' name='upload' value='upload' /></td>";
      echo "</tr>";
   echo "</table>";
echo "</form>";

?>

upload_process

$mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv');

if(in_array($_FILES['file']['type'],$mimes)){
   echo "csv file!";
} else {
   echo "nicetry";
}

any input would be very much appreciated.

 

thanks! :D

Link to comment
Share on other sites

Sorry for the double post. Im having trouble with my internet connection.

 

I've also tried the below code for upload_process.

Keeps throwing me this is not a csv file.

if (($_FILES["file"]["type"] == "application/vnd.ms-excel")) {
		if ($_FILES["file"]["error"] > 0) {
			echo "error uploading the file";
		}
		else {
			echo "hooray!";
		}
	}
	else {
		echo "this is not a csv file";
	}

Man this one problem got me searching for answers for like a day already.

Edited by ill-fated24
Link to comment
Share on other sites

It should be the file uploaded from the upload_page.

 

Barand's questions weren't about what the values should be, but were for you to look at what the values actually are. because your code is testing what the values are and your code is detecting that they aren't what you expect, knowing what they actually are would point to the cause of the problem.

 

stated another way, your code is trying to use data values that would only be present for a successful upload, before your code has even tested that the upload worked. check if the upload worked first, then try to test the data values from the upload. for a successful upload, $_FILES["file"] will be set and $_FILES["file"]['error'] will be equal to a zero.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.