Passer les arguments de nom de fichier à PHP de la coquille et de l'espace numéro ...



Aujourd'hui, tout en travaillant avec un projet d'importer des milliers d'articles à partir de fichiers texte pour wordpress, je avait fait face à un trouble alors que je suis l'exécution d'un script shell pour parcourir tous les fichiers texte et appeler un script PHP avec l'envoi le nom de fichier comme argument. Le script PHP serait alors traiter le fichier comme notre exigence. Le script shell ressemblait:

for i in `find . -type f -name '*.txt'`;
do
php import_posts.php $i
done

Here, $i would be the filename and it is passed to PHP script for processing. But the problem is that, when the filename (including directory name) consists any whitespace in its name, the script would broken. For example, some files path looked like
/home/user/articles/Internet Business/Web Hosting/how_to_start_your_company.txt

Here, notice the space after Internet and Web. The shell script was breaking just after Internet. It was sending /home/user/articles/Internet as argument to PHP script and obviously PHP script was failed to find this file.

After searching a lot, finally I found a solution .

It was really simple. I am quoting from his blog:

If none of your files or folders have spaces in their names, then this isn’t a problem, but that is increasingly unlikely nowadays.  If your songs do have spaces in their filenames, then the solution is to change how the shell splits that output.  This is controlled by the variable $IFS, which is set to spaces, tabs, and newlines by default.  We’d like it set to just newlines, so we do:

So, here is the solution. I just put the following line at the top of my shell script.

export IFS=$'\n'

After that everything is fine. The script is running exactly how I intended it to run.

Thanks for Anthony for his solution.

Smarter DOMDocument for PHP



PHP 5 includes a great built in class DOMDocument to DOM parsing of HTML/XML document. The class includes a number of methods to easily traversing a DOM.

However, it has a few shortfalls like it fails to handle encoding correctly and includes some tags which may often seem irritating for developers.

Artem Russakovskii has made an extension (named SmartDOMDocument) of this class to eliminate such shortcomings. His class inherits the built in DOMDocument and includes a few extra methods that may make developers life peaceful.

Extra Features of SmartDOMDocument


saveHTMLExact()

DOMDocument has an extremely badly designed “feature” where if the HTML code you are loading does not contain <html> and <body> tags, it adds them automatically (yup, there are no flags to turn this behavior off).

Thus, when you call $doc->saveHTML(), your newly saved content now has <html><body> and DOCTYPE in it. Not very handy when trying to work with code fragments (XML has a similar problem).

SmartDOMDocument contains a new function called saveHTMLExact() which does exactly what you would want – it saves HTML without adding that extra garbage that DOMDocument does.

Encoding Fix

DOMDocument notoriously doesn’t handle encoding (at least UTF-8) correctly and garbles the output.

SmartDOMDocument tries to work around this problem by enhancing loadHTML() to deal with encoding correctly. This behavior is transparent to you – just use loadHTML() as you would normally.

Continue reading →

PDF output generating library using PHP

If you are looking for creating PDF output of your HTML pages, you can do it using PHP’s PDFlib module. However, there are some libraries for faster development of your script. Some even are not dependent on PDFlib. Please see below for quick overview.

DOMPDF
dompdf is an HTML to PDF converter. At its heart, dompdf is (mostly) CSS2.1 compliant HTML layout and rendering engine written in PHP. It is a style-driven renderer: it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also supports most presentational HTML attributes.

Features

* handles most CSS2.1 properties, including @import, @media & @page rules
* supports most presentational HTML 4.0 attributes
* supports external stylesheets, either local or through http/ftp (via fopen-wrappers)
* supports complex tables, including row & column spans, separate & collapsed border models, individual cell styling, (no nested tables yet however)
* image support (gif, png & jpeg)
* no dependencies on external PDF libraries, thanks to the R&OS PDF class
* inline PHP support. See below for details. Continue reading →

URL Shortening using PHP

Yesterday while developing PunBB Twitter extension, Invarbrass of Projanmo Forum helped me by providing some URL shortening snippets for several websites. Those were very simple to use. I am sharing so that it comes to others’ usages.

to.ly
function CompressURL($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://to.ly/api.php?longurl=".urlencode($url));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$shorturl = curl_exec ($ch);
curl_close ($ch);
return $shorturl;
}
echo CompressURL("http://projanmo.com");

Continue reading →

PunBB 1.3x Extension…. Convert numbers to Bangla (Bengali) on the fly!

Just now I have completed the first version of bangla_numbers extension for PunBB 1.3.x. When enabled, this function will converts numbers used in PunBB to Bangla. It will specially converts numbers of posts, topics, replies, members etc.

Installation:

Upload the unzipped files to extensions directory of PunBB and install the extension from Admin panel’s Extension tab. You’re done!

Download 1.1.1

PunBB 1.3 Extension – Post Editing Period – Control how long a post can be edited

Today I have developed a simple PunBB Extension for Projanmo Forum that allows administrators to control until how long a post can be edited. Such control is necessary as often members removes the post whimsically making replies irrelevant.

If you install this plugin, it will stop from such happening. From admin panel’s Settings -> Features section you can mention how many minutes are allowed to edit a post within. After such duration the post will not be editable.

Click here to download

PunBB and OpenInviter

Recently I have worked with OpenInviter to work with PunBB. OpenInviter already has a plugin (mod) that enables it to work with PunBB . But Idid some more works that will enable PunBB forum admins to tracks how many users have been invited, which users has invited, when invited, when the invited friends visited the site and when they registered (if they did). So, it will be easier for the admins to manage invitations. Admins can even award the top referrals.

But this script or OpenInviter do not store the importer’s (user’s) email login details. So you can use it safely.

When someone visits the site upon getting invitations from forum user, a cookie is set in his computer so that he can be tracked if in future (untils 6month s or deleted) he directly visits the site.

I have worked with PunBB 1.2.x. But I don’t have any plan to release this script for 1.2x because manual source hacking, which I dislike. I wish to release as an extension for PunBB 1.3.x. But if someone extreamly need this modifcation, please let me know. I will try to deliver to you.

WordPress Read/Hit Counter Plugin Update (v1.1)

As some of users of my plugin, wordpress read counter , requested me to release an update so that they can show hit counter along with post. Few days ago, I have made the necessary update but I forgot to release it.

Click here to download

The installation steps are available in the previous post. I am just showing how to show the counter with post.

For showing the counter you need to edit the template file. You need to put the following line anywhere in the theme/template to show the counter.

< ?php if(function_exists(‘rc_showread’)) echo rc_showread();?>

Showing counter on homepage:
Go to theme editor. Click on index.php file and open it. Put the above line where you want to show it.
here is the excerpts after applying this in wordpress default theme:
——————
<div class=”post” id=”post-<?php the_ID(); ?>”>
<h2><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to < ?php the_title_attribute(); ?>”>< ?php the_title(); ?></a></h2>
<small>< ?php the_time(‘F jS, Y’) ?> <!– by <?php the_author() ?> –> < ?php if(function_exists(‘rc_showread’)) echo rc_showread(); ?></small>
</div>

——————-

Showing counter on post details (single post):
Go to theme editor. Click on single.php file and open it. Put the above line where you want to show it.
here is the excerpts after applying this in wordpress default theme:
——————-
< ?php } edit_post_link(‘Edit this entry’,”,’.’); ?>
< ?php if(function_exists(‘rc_showread’)) echo rc_showread(); ?>
——————-

Don’t forget to clear caches if you use any caching in your blog to see the effect.

Download OpenLD Directory Script

I have used OpenLD Directory Script for Projanmo Web site. OpenLD is really simple and cool script. But it was wonderful extension system for injecting your own code using the hook.

But the worse news is that the homepage is down for a quite long time. So we don’t get any update of this script anymore. I have seen many people were looking for it.

Today Arafat Rahman was asking me if I have any backup of this script. I searched my computer ending without positive result. Later entering my website’s account I have found a backup. So I planned to post it so that interested people can download it.

Click Here to Download OpenLD 1.2.6.1

How to submit a form using PHP (cURL, fsockopen)

How to submit a form using PHP

There are situations when you want to send data using POST to a URL, either local or remote. Why would you want to do this? Probably you want to submit data to an optin form, but without taking a valuable visitor away from your site. Or maybe you want to send data to several applications for various purposes, which would be impossible to do in the usual manner. So how can we deal with this problem?

Simulate submitting a form using cURL

So what is cURL anyway? cURL stands for “Client URL”, and it is a library of functions that can be used to connect and communicate to a wide range of servers, such as HTTP, FTP, telnet and so on. cURL also speaks HTTPS, so it can be used to communicate with secure servers.

What are we going to use are cURL HTTP capabilities. cURL supports POST and GET methods, file uploads, cookies, user/password authentications, even using proxy servers for connecting.
Continue reading →