Archive for the 'PHP' Category

The best regular expression examples

Regular expression is one of the most important topics in programming world. Unlike some plain explanations, here is a tutorial with some very beautiful examples. Every example is put on a figure like the following.

Continue »

The problem of truncating text from SQL Server in PHP

PHP and SQL Server are a powerful combination, however sometimes data stored in a text type column is truncated for no apparent reason after 4096 characters.

I got this problem today when I want to display text data from SQL Server 2000. Apparently, I need to increase the maximum size of a text column to be returned from SQL Server by PHP. Since I have control over the Web Server. Here is how I fixed the problem.

Continue »

How to upload files directly to a remote server?

It’s very simple for a web-application to allow users to upload images to a website. You can use a simply HTML

However, instead of having those images uploaded to a web server – I would to have those images uploaded directly to another server.

Is this possible to have a web-application allow a user to upload images directly to another server?

Continue »

Build a quick online notepad

What I am thinking is that there is a notepad which can be opened anywhere. I searched online and found there are several such kind of websites, among them ubernote looks very professional. It likes gmail, and have a lot of function. But what if I only want a notepad which is like a post. I input the webpage and start typing, when I use another computer I can also open it fast. I made a tool by myself, called Quick Notes.

The main feature is it’s speed. If a computer is only used by one person, the user can register this computer as a private machine. Whenever, he inputs the URL, the notes opens directly, and there is no need to login. If a registered user use a public computer, it will require username and password to access his notes. This solution extremely shorten the usage process.  However, there is a big issue here. Since there is no login, it may be not secure. So the Quick Notes is designed for normal non-important information. The following is a screen shot of the Beta version. http://notes.programcreek.com

Quick Notes

Quick Notes

With this nice tool, Continue »

Debug program using deleting method

Recently I am maintaining an in-house PHP content management system. There is an error on this page, but only for one record in database. Since the code works well for all other record, it should also work for this one. While I could not find the error, by checking the code line after line. Finally, I decide to delete some parts to find which line has problem. This page contains several sections, such as header, several container sections for ajax to load dynamically, footer, etc.  So I just remove sections first, then I find which section has problem. After getting the wrong section, then I remove functions one after one. Finally, I get to know where the problem is.

The problem is JavaScript. There is a single quote ‘ in a person’s name, like O’lley. The programmer also used single quote in JavaScript, such as:

onclick='show_center("")'

When the page is loaded, the JavaScript becomes:

onclick='show_center("O'lley")'

The solution is to change the single quote ‘ to double quote ” as following:

onclick="show_center(\"\")"

In summary, Deleting is a way to find error in a page, and remember to consider this problem in your JavaScript code.

Zend Studio 5 doesn’t list php files

I have to say that this is the best discovery this week, and it seems very stupid when it is solved. The php files can not be listed in zend studio.

Solution:

Just open the file manager window, at the bottom there is a drop-up list which let you choose what kind of files you want to list.

I got this problem one year ago, and searched everywhere but couldn’t find the solution. At that time, I reinstalled zend studio, still doesn’t work. Holy crap. It is solved this way.

Using Alfresco PHP Web Service to upload files

The following is the PHP code for uploading files using Alfresco Web Service. I put some comments inside the code. If there is anything confuses you, please leave your comment and I hope I can help.

if (isset($_SERVER["ALF_AVAILABLE"]) == false)
				{
					require_once "./alfresco_sdk/remote/Alfresco/Service/Repository.php";
					require_once "./alfresco_sdk/remote/Alfresco/Service/Session.php";
					require_once "./alfresco_sdk/remote/Alfresco/Service/SpacesStore.php";
					require_once "./alfresco_sdk/remote/Alfresco/Service/ContentData.php";
				}
				$repositoryUrl = "http://localhost:8080/alfresco/api";
				$userName = "admin";
				$password = "admin";
 
				// Authenticate the user and create a session
				$repository = new Repository($repositoryUrl);
				$ticket = $repository->authenticate($userName, $password);
				$session = $repository->createSession($ticket);
 
				// Create a reference to the 'SpacesStore'
				$spacesStore = new SpacesStore($session);
 
				$dir_name =  "s".$irbID;
 
				// Use a serach to get the guest home space we will use to place the new content in
				$nodes = $session->query($spacesStore, "PATH:\"app:company_home/cm:testing/cm:$dir_name\"");
 
				if(!empty($nodes)){
					echo "good, no need to do anything.";
				}else{
					$nodes = $session->query($spacesStore, "PATH:\"app:company_home/cm:testing\"");
					$contentNode = $nodes[0];
					$newSpace = $contentNode->createChild('cm_folder', 'cm_contains', 'cm_'.$dir_name);//here has to be cm_ as it is here.
					$newSpace->cm_name = $dir_name;
					$newSpace->cm_title = "This is a title";
					$newSpace->cm_description = 'Description of Space';
					$session->save();
					unset($session);
					$session = $repository->createSession($ticket);
					$nodes = $session->query($spacesStore, "PATH:\"app:company_home/cm:testing/cm:$dir_name\"");
				}
 
				$guestHome = $nodes[0];
 
				// Get the name of the new node
				$name = $_POST['txt_docs_title'];
 
				$contentNode = $guestHome->createChild("cm_content", "cm_contains", "cm_".$name);//!!the folder's name has to begin with 'cm_' otherwise, using web service can not find it later.
				$contentNode->addAspect("cm_titled");
 
				// Set the name, title and description property values
				$contentNode->cm_name = $name;
				$contentNode->cm_title = $_POST['num_ct_ID'];
				$contentNode->cm_description = "testing description --program creek";
 
				// Set the content onto the standard content property for nodes of type cm:content.
				// We are going to assume the mimetype and encoding for ease
				$contentData = $contentNode->updateContent("cm_content", $_FILES['document']['type'], "UTF-8");
 
				// Set the content to be the content file uploaded from the client
				$contentData->writeContentFromFile($_FILES["document"]["tmp_name"]);
 
				// Save the new node
				$session->save();

PHP ftp file upload and download code

The following is the code for file upload and download using PHP ftp function.
The interface will be like this:

php file upload using ftp

php file upload using ftp

PHP has this function, I believe Java will have ftp function too. So using ftp to transfer file and using web service to store related information, I believe this is the correct way.
The following is the complete code. For editing reason, I separate php and html code, but you should put them in the same file, that should work properly.

Continue »