logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: Uploading Files using php
Author
kr33



Joined: 19 Sep 2006
Posts: 64
Location: South Africa

PostPosted: Fri 03 Nov '06 9:36    Post subject: Uploading Files using php Reply with quote

Hi

I'm trying to upload files so that I can eventually import them into my MySQL database.

I've created a basic upload page, eg of an upload section

Php/html: upload.php
Code:

<?php
   session_name("name");
   session_start();

   //Check if authentic user
  if ($_SESSION['authuser'] == 1)
 {
?>

<html>
<head>
  <title>Upload Page</title>
</head>

<body>
  <form action="filehandle.php"enctype="multipart/form-data"
                                                 method="post" name="uploadForm">
  <table width="270px" border="0" cellpadding="2" cellspacing="2"
                 align="center">
   <tr>
     <td> <input type="file" name="file_to_upload" /> </td>
   </tr>
  </table>
  </form>
</body>
</html>

<?php
} else {
      echo "User not allowed to upload any files!";
      exit();
}
?>


The above code is just segments of my actual code(for illustrative purposes)

Php: filehandle.php
Code:

<?php
   
    if ($_FILES['file_to_upload']['error'] > 0) {
         $err=$_FILES['file_to_upload']['error'];
           switch($err)
           {
                    //Error conditions
           }
    } else
       {
               //Display filename
        }
?>


Yet again, the above is just a code segment.

My question is...How do i correctly check that the type of file to be uploaded is the filetyp that i want and NOT any else?

I'm uploading only .CSV files.

Thanks

Quote:

Every man has to go through Hell...to reach his Paradise
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7288
Location: Germany, Next to Hamburg

PostPosted: Fri 03 Nov '06 11:42    Post subject: Reply with quote

Code:

/**
* Get the extension from file
* @param $file full filename
* @return mixed var
*/
function getExtension ($file){
$_file=trim("$file");
$getpos=strrpos($_file,".");
if(!$getpos)
      return "";

$calc = strlen($_file)-$getpos;
return substr($_file, $getpos+1, $calc);
}

$ext = getExtension($your_input_string);
if(eregi("csv", $ext)){
//do the upload
}
else
{
// :-P
}
Back to top
kr33



Joined: 19 Sep 2006
Posts: 64
Location: South Africa

PostPosted: Fri 03 Nov '06 12:58    Post subject: Reply with quote

Thanks

It works perfectly, clearly I have lots to learn

Wink

Ciao

P.S.
You wouldn't hapen to know, how I would read in a CSV file, using SQL so that I can import it into Temporay tables and combine certain data and replace the main table with the new data?
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7288
Location: Germany, Next to Hamburg

PostPosted: Fri 03 Nov '06 13:44    Post subject: Reply with quote

To import csv file I asked google and found some nice on mysql.com page Wink
Code:

mysqlimport --fields-optionally-enclosed-by=""" --fields-terminated-by=, --lines-terminated-by="\r\n" --user=YOUR_USERNAME --password YOUR_DATABASE YOUR_TABLE.csv

from here

some other SQL statement here

google keywords : read csv php put in mysql Idea
Back to top
kr33



Joined: 19 Sep 2006
Posts: 64
Location: South Africa

PostPosted: Fri 03 Nov '06 14:55    Post subject: Reply with quote

Thanks for that, its a real help.

How would I copy values, ALL values, from one field in a table, to a field in the SAME table, replacing the values in the destination field.

Basically, i need to do this because these are values that are needed monthly, so for example the two field names are

previous and current

they both have values, so in the next month when the data is updated, all the values in the current column have to now be moved to the previous column and the new data for the month replaces the previous months data in the current column.

Hope that made sense.

How would I achieve this.

Thanks again
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7288
Location: Germany, Next to Hamburg

PostPosted: Fri 03 Nov '06 15:08    Post subject: Reply with quote

You should make a date field in the data row with now() (from MySQL) than you can select by the date or put the name of that month into it.

e.g.
Code:

SELECT * FROM `mydata` WHERE `month` = 'may'
Back to top
kr33



Joined: 19 Sep 2006
Posts: 64
Location: South Africa

PostPosted: Fri 03 Nov '06 15:20    Post subject: Reply with quote

The data in the table fields are actual raw data, as in, integers.

So the field names is how i reference the values. So the values need to be replaced.

eg.

previous month

field name: PRO
row1 : 29192342
row2 : 12345678

current month
field name: PRO
row1 : 12345678 //row1 now equals previous months row2
row2 : 90112345 //row2 now gets replaced by the new data from my csv file

this would apply to only selected data rows.

is this possible?
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7288
Location: Germany, Next to Hamburg

PostPosted: Fri 03 Nov '06 16:23    Post subject: Reply with quote

Why replace? Here some schema how I would create that DB

[value1][othervalue][date]

e.g.
[00000001][12345678][may]
[00000002][12345678][may]
[00000003][90112345][june]
[00000004][90112345][june]

Did you mean rows or cols?
Back to top
Brian



Joined: 21 Oct 2005
Posts: 209
Location: Puyallup, WA USA

PostPosted: Fri 03 Nov '06 21:03    Post subject: Reply with quote

I hope this comes across with the intention in which it is posted with ... LOL, just offering a little something I came across. First of all, I have never used or built a script that uploads files that are stored in a db. I do however have a multitude of sites with file uploaders that simply save the file.

I found this article, maybe it is also useful:

http://www.php-mysql-tutorial.com/php-mysql-upload.php

But equally important and not so much a matter of how the file is stored is security. See the info at:

http://us3.php.net/manual/en/function.is-uploaded-file.php

The fact is you want to be sure the file that was uploaded was the right file, and not a substitute file, regardless of how it is stored. I did not see any mention in this thread of security and while I am sure it was on the radar, I just thought I'd add this as it may be benefical to some.

Enjoy your day.
Back to top
kr33



Joined: 19 Sep 2006
Posts: 64
Location: South Africa

PostPosted: Mon 06 Nov '06 8:31    Post subject: Reply with quote

Hey guys, I figured a way to swap values the way I wanted.

It may not be the shortest way/best way but it gets the job done.

I put it into a function, here it is:

Code:

function swapUpdate($table_name, $col_name, $row1, $row2)
{
#---------------------------------[Get the data]-----------------------------------
   //fetch data from first row
   $sql_r1 = "SELECT $col_name FROM $table_name ".
                  "WHERE row_position = $row1";
   
   $result_1 = mysql_query($sql_r1)
      or die (mysql_error());
      
   $row_1 = mysql_fetch_array($result_1);
   $data_r1 = $row_1[$col_name]; //store retrieved data

   //fetch data from 2nd row
   $sql_r2 = "SELECT $col_name FROM $table_name ".
                  "WHERE row_position = $row2";
   
   $result_2 = mysql_query($sql_r2)
      or die (mysql_error());
      
   $row_2 = mysql_fetch_array($result_2);
   $data_r2 = $row_2[$col_name]; //store retrieved data

#---------------------------------[**********]-----------------------------------

#---------------------------------[Set the data]-----------------------------------
   //Swap and update row1
   $sql_u = "UPDATE $table_name SET $col_name=$data_r2 ".
                 "WHERE row_position = $row1";
   
   $result_u = mysql_query($sql_u)
      or die (mysql_error());
      
   //Swap and update row2
   $sql_u = "UPDATE $table_name SET $col_name=$data_r1 ".
                 "WHERE row_position = $row2";
   
   $result_u = mysql_query($sql_u)
      or die (mysql_error());

#---------------------------------[*********]-----------------------------------

}


All the above does is, gets two values from a specified table, from a specified column, then stores the values in temp variables and then updates the table with the values swapped (if that made any sense, but if you go through the code, u'll get it)

P.S. if you're wondering where row_position came from, its a field heading in my table.

Hope this may come in useful, if ever something like this may be need.
And if there is a better way of achieving the above, I'm open to suggestions.

Ciao
Back to top


Reply to topic   Topic: Uploading Files using php View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner