Tag: PHP/MYSQL

 

You can do allowing of doc/docx file in 2 modes: in html source with the property accept or by php code.

For uploading a doc/docx file you should first have a form where youhave all fields you need to send to server and another field that is of type file.

File:
<form enctype="multipart/form-data" method="POST">This is the code for html:
<table border="0">
<tbody>
<tr>
<td align="left">File:</td>
<td><input accept="doc/docx" name="filename" size="40" type="file" /></td>
</tr>
<tr>
<td><input name="Upload" type="submit" value="Upload" /></td>
</tr>
</tbody></table>
</form>

 

We should specify the enctype of the form to multipart/form-data in order to upload a file.

 

Here is a brief explanation on how we can write the php code to upload one file to our server:

First we should set and verify the file extensions that we will allow the user to upload. We will allow only files with doc/docx extension. We should also verify that the filesize it’s greater then 10 bytes to be sure that is a doc/dox file. If we have no errors then we can use move_uploaded_file function to upload our doc/docx file.

Php code to upload doc/docx file to the server

 

//if we clicked on Upload button
 
if($_POST['Upload'] == 'Upload')
 
  {
 
  //make the allowed extensions
 
  $goodExtensions = array(
 
  '.doc',
 
  '.docx',
 
  );  
 
 
 
  $error='';
 
  //set the current directory where you wanna upload the doc/docx files
 
  $uploaddir = '/home/uername/folderroot/';
 
  $name = $_FILES['filename']['name'];//get the name of the file that will be uploaded
 
  $min_filesize=10;//set up a minimum file size(a doc/docx can't be lower then 10 bytes)
 
  $stem=substr($name,0,strpos($name,'.'));
 
  //take the file extension
 
  $extension = substr($name, strpos($name,'.'), strlen($name)-1);
 
  //verify if the file extension is doc or docx
 
   if(!in_array($extension,$goodExtensions))
 
     $error.='Extension not allowed<br>';
 
<span> </span> //verify if the file size of the file being uploaded is greater then 1
 
   if(filesize($_FILES['filename']['tmp_name']) &lt; $min_filesize)
 
     $error.='File size too small<br>'."\n";
 
  $uploadfile = $uploaddir . $stem.$extension;
 
$filename=$stem.$extension;
 
 
 
if ($error=='')
 
{
 
//upload the file to 
 
if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile)) {
 
echo 'File Uploaded. Thank You.';
 
}
 
}
 
else echo $error;
 
}
Tags: , ,

Fetching results from a mysql table

To connect to a mysql database you should use the command mysql_connect .You should first setup the details of the mysql database.

$connect=mysql_connect($server, $db_user, $db_pass)    or die ("Error: could not connect to database");

$server is the server for the mysql database, usually this is localhost, but you should double check.

$db_user is the user which connects to the server

$db_pass the password for the user

If the can’t connect to the server we should display an error message with die() command.

After we connect to the database we can make a sql query on the database that we want.

//we select  all the rows from table TableName where the Field1 has a value of $value
$results = mysql_db_query($database, "SELECT * FROM TableName WHERE Field1=".$value);
 while($resultrow = mysql_fetch_array($results, MYSQL_ASSOC))
{
  echo $resultrow['Field2'];//we show the result of the field Field1
}

After we make the query we can take each row of the result query and do whatever we want.

For closing a mysql connection we can use mysql_close($connect);

Example Code in PHP:

 
$server="localhost";//this is ussually localhost
 
$db_userName="UserName";
 
$db_password="Password";
 
$database="DatabaseName";
 
$connect=mysql_connect($server, $db_userName, $db_password  or die ("Mysql connection error");
 
$results = mysql_db_query($database, "SELECT * FROM TableName WHERE Field1=".$value);
 
while($resultrow = mysql_fetch_array($results, MYSQL_ASSOC))
{
  echo $resultrow['Field2'];//we show the result of the field Field1
}
 
mysql_close($connect);
Tags:

cURL Post Data – PHP

Introduction

In this post I will talk about using cURL to post data to a server. Curl it’s a library that you can use to get data from a server or send it back. It was created by Daniel Stenberg and it works with ftp,gopher,telnet,file,dict,ldap, http and https protocols. I am not gone write here how you can make to configure and install, I want to show you a method to post data to a server on the http protocol.

How it works? 

You need to initiate a cURL session, put the url of the server you wanna POST data, put the options for the current session, execute session and get the data you need.

 

Here is a list of cURL functions that can help you to post data:

 
curl_init – it’s is used to initiate a cURL session

curl_setopt – it helps you to pass option to cURL for the next transfer.

curl_exec – executes a cURL session

curl_error – containts the last error of cURL sesion that was executed

 

Here is a sample code for posting data in PHP:

 

function datapost($URLServer,$postdata)
{
 
 
 
$agent = "Mozilla/5.0";
 
$cURL_Session = curl_init();
 
curl_setopt($cURL_Session, CURLOPT_URL,$URLServer);<span> </span>
 
curl_setopt($cURL_Session, CURLOPT_USERAGENT, $agent);
 
curl_setopt($cURL_Session, CURLOPT_POST, 1);
 
curl_setopt($cURL_Session, CURLOPT_POSTFIELDS,$postdata);
 
curl_setopt($cURL_Session, CURLOPT_RETURNTRANSFER, 1);
 
curl_setopt($cURL_Session, CURLOPT_FOLLOWLOCATION, 1);
 
$result = curl_exec ($cURL_Session);
 
return $result;<span> </span>
 
}
 
 
 
$htmlsource= datapost("http://www.example.com/script.php","param1=value1&param2=value2")

 

 

So first we initiate the cURL sesion with curl_init() and after that we set some options for the current session with curl_setopt and after that we execute the session and get the result.

 

CURLOPT_URL,$URLServer – this option sets the target script.

CURLOPT_USERAGENT,agent – this is optional, it sends to the server an agent.

CURLOPT_POST – this should be set to 1 because we make a post to the server.

CURLOPT_POSTFIELDS – here we should pass the parameters and there values

CURLOPT_RETURNTRANSFER – should be set to 1(because we store the source of the result in a variable) otherwhise the result will be printed on page

CURLOPT_FOLLOWLOCATION – is important if the server it redirects you after you post the data. If the server redirects should be set to 1.

 

If you wanna store a cookie when you execute the sesion you should use:

 

 
curl_setopt($cURL_Session, CURLOPT_COOKIEFILE, $path_to_cookie);
 
curl_setopt($cURL_Session, CURLOPT_COOKIEJAR, $path_to_cookie);
Tags:

Description

I want to begin the first post about PHP with something easy, but very useful. Writing and reading to/from files in PHP.
Here is a short description for reading from file in PHP:

  1. Open the file for reading
  2. If the open was successful then we can read from the output file
  3. After we read from file we should close the file.

    Here is a short description for writing to files in PHP:

    • Open the file for writing
    • If the open was successful then we can Write to the output file
    • After we wrote to file we should close the file.

    OPENING

    To open a file in PHP you should use the function fopen:

    pointer fopen( string $filepath, string $mode)

    This function return a pointer resource when the $filepath exists or FALSE when the file doesn’t exist.

    $filepath is the path to the file that we want to open.
    $mode – this is the type of access when reading or writing to files

    • r – opens the file for reading only
    • r+ – open the file for reading/writing and places the file at the begining of the file
    • w – opens the file for writing only
    • w+ – opens the file for reading/writing places the pointer at the beginning of the file and truncate the size of the file to 0
    • a – places the pointer at the end of the file for writing(append)
    • a+ – places the pointer at the end of the file for reading and writing(append).
    • x – opens for writing, places pointer at the begining of the file.
    • x+ – opens for writing/reading, places pointer at the begining of the file.

    READING

    You can use more functions for reading from file after you open it:
    fread($filepointer,$nbytes)
    $filepointer – a pointer to the file
    $nbytes – number of bytes you wanna read from file. (a character have a size of a byte)

    fgets($filepointer) – reads a line from the file
    fgetc($filepointer) – reads a caracter from file

     

    WRITING

    You can used fwrite($filepointer, $datastring) to write a string to file

    CLOSING

    After you finished writing/reading from file you shuld use:
    fclose($filepointer) – to close the file

     

    Example of codes:

    For reading the first 10 caracters from a file you can use the folowing block of code:

     

    $f=fopen("c:\\path\\file.txt","r");
    $string = fread($f,10);
    fclose($f);

    Reading an entire file to a string variable character by character:

    $f=fopen("c:\\file.txt","r");
    $textstring="";
    while (!feof($f))
    {
    $c=fgetc($f);
    $textstring=$textstring.$c;
    }
    fclose($f);

    Reading a file quickly:

    $path = "c:\\file.txt";
    $f=fopen($path,"r");
    $textstring = fread($f,filesize($path));
    fclose($f);

    Writing a two lines of string to a file

    $path = "myfile.txt";
    $f = fopen($path, 'w') or die("error opening the file");
    $allstring="this is the first line of the test\nthis is the second line of the test";
    fwrite($f,$allstring);
    fclose($f);

    Additional functions that you can be useful for file handling are:

    feof($filepointer) – test if the end of file was reached.
    file_exists($filepointer) – tests if a a file exists
    die() or exit() – terminate the script that is currently executed.

    Tags:
    Next posts » Back to top