неделя, 17 февруари 2013 г.

How to add timestamp in the name of an uploaded image in PDW file browser

What is PDW file browser

PDW file browser as the name suggest is a ready to use file browser that you can include in your web projects. It is written on PHP and if you want to include it easily in your projects you should put the application in the web root directory of your web server (for xampp it is htdocs directory). It this aspect it is different from some other external applications which by default expect to be put in the directory of your current project. The application is used for all cases in which you want to provide to the user the ability to manage files on the server (in most cases images). The application allows the user to upload, delete, rename the files, to add directories, to search and more.


What did I need the PDW file browse for

I am doing a project which uses CK Editor as a "what you see is what you get" (WYSIWYG) system. In other words I needed a web editor with which the user could prepare articles, which could afterwords be automatically published on the web site. The articles needed to include not only text but also images. The CK Editor provides the interface for image selection, but it didn't have a free file manager. To be more specific - the company provides a file manager, but you need to pay for it. At this point I started searching for a free file manager and from what I have read the PDW file browser was what I needed. Indeed I was able to make the two systems work together easy enough. You just need to specify that you use the PDW file browser in filebrowserBrowseUrl and filebrowserImageBrowseUrl as it is shown bellow:


$site .= "<textarea name=\"editor\">" . $editor . "</textarea>\n";
$site .= " <script>\n";
$site .= "  CKEDITOR.replace( 'editor', {\n";
$site .= "    filebrowserBrowseUrl: '/pdw_file_browser/index.php?editor=ckeditor',\n";
$site .= "    filebrowserImageBrowseUrl: '/pdw_file_browser/index.php?editor=ckeditor&filter=image',\n";
$site .= "    filebrowserUploadUrl: 'ck_editor/uploader.php',\n";
$site .= "    filebrowserImageUploadUrl: 'ck_editor/uploader.php',\n";
$site .= "    filebrowserImageWindowWidth: '720',\n";
$site .= "    filebrowserImageWindowHeight: '560'\n";
$site .= "  });\n";
$site .= " </script>\n";

Adding a time stamp

The problem that I had with the PDW file browser was the file name of the uploaded images. When the user uploaded an image the name of the uploaded image file was the same as the name of the original file.



This meant that if for example the user uploaded on 01/02/2013 the file Ferrari.jpg and on 05/02/2015 he uploaded another image with the same name, the new image would replace the old one. If this was allowed to happen than all the articles that used the old Ferrari.jpg would now show the new image. For that reason I wanted every time the user uploaded an image to add the current time stamp at the beginning of the file name. This way every image uploaded would have a unique name and I would not have to worry that the user could overwrite an existing image file.


A lot of the functions in the PDW file browser can be configuratted, but there is no build-in function to add a time stamp in front of the file name. For that reason I had to change the code of the PDW browser itself. The version I was working on was PDW File Browser v1.3 beta. The change has to be done here: \swfupload\upload.php. You need to find the place where the actual file is uploaded to the server. In my case it was on row 114. The original code looked like this:

if (!@move_uploaded_file($_FILES[$upload_name]["tmp_name"], $save_path.$file_name)) {
 HandleError("File could not be saved.");
 exit(0);
}


The file name is taken here: $save_path.$file_name. In order to add the time stamp I have modified it like this: $save_path.time()."_".$file_name.

In my code the end result looked like this:

//if (!@move_uploaded_file($_FILES[$upload_name]["tmp_name"], $save_path.$file_name)) {if (!@move_uploaded_file($_FILES[$upload_name]["tmp_name"], $save_path.time()."_".$file_name)) {
    HandleError("File could not be saved.");
    exit(0);
}

I hope you find that useful and this fix works for you as good as it did for me.

Няма коментари:

Публикуване на коментар