CodeCharge Studio
search Register Login  

Visual PHP Web Development

Visually Create Internationalized Web Applications, Web Reports, Calendars, and more.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 HELP: make uploaded filename unique

Print topic Send  topic

Author Message
Tam
Posted: 03/28/2005, 8:43 AM

Hi there,
By default, a name of the file that is uploaded is added timestamp by CCS. I found the following code in the Class file:

// move uploaded file to temporary folder
$file_exists = true;
$index = 0;
while($file_exists) {
$ActualFileName = date("YmdHis") . $index . "." . $FileName;
$file_exists = file_exists($ActualFileName);
$index++;
}

Since I don't want the name is long, I remove date("YmdHis"), so the code become:
$ActualFileName = $index.".".$FileName;

I thought that the variable $index is good enough to make the filename unique. Since $file_exists variable will return true if the file exists, the while loop runs again to get another name of filename. Unfortunately, it doesn't.

As I upload an image img1.gif, the filename become 0.img1.gif. After that, I upload the same image again and again, but I just see only one image in my folder, and it is 0.img1.gif.

Is the code supposed to make the file increments the $index variable and make the filename become 1.img1.gif, 2.img1.gif, etc. If anyone deals with this issue before, please give me some hints to make the filename unique. Or do I have to keep the timestamp?

Thanks.
Nicole

Posts: 586
Posted: 03/29/2005, 1:12 AM

Tam,
The problem is that file is not checked to exist in File Folder.
To make the code work properly you need to add File Folder to $ActualFileName:
  
while($file_exists) {  
// $ActualFileName = date("YmdHis") . $index . "." . $FileName;  
$ActualFileName = $index . "." . $FileName;  
$file_exists = file_exists($this->FileFolder.$ActualFileName);  
$index++;  
}  

_________________
Regards,
Nicole
View profile  Send private message
Tam
Posted: 03/29/2005, 8:42 AM

You point out a very good point for me. I try your code, but it still doesn't work. Do I miss anything else? Please help. Thanks.
Nicole

Posts: 586
Posted: 03/30/2005, 12:26 AM

Tam,
I really have no idea what doesn’t work for you. It works pretty good on my side. For debug purpose you can print $ActualFileName and $file_exists values, it helps you to find out on what operation the code fails.
You can also contact CCS support regarding that.

_________________
Regards,
Nicole
View profile  Send private message
Tam
Posted: 03/31/2005, 3:31 PM

Hi Nicole,
I think I see the problem here. The code that I tried to figure out is for moving uploaded file to TEMPORARY folder. Since all file are uploaded to TEMP folder first and then to actual file folder, no files exist in TEMP folder. That's why the function file_exists always return false.

Since the code works on your side, would you tell me where you modify your code in the Classes.php file?

Thank you.
Tam
Posted: 03/31/2005, 6:07 PM

Hi Nicole,
I figure out how it works in your case. The reason is that I need to set the File Folder property of the FileUpload to be something. However, since I am working on multiple folder, which the File Folder is changable, I don't get it working yet.

Here is my current work. I have a listbox named Directory with value Folder1, Folder2, ..., From the value I select, the upload file will be stored in there. I have the follwing code in the BeforeFileProcess event:

$Form1->FileUpload1->FileFolder = $Form1->Directory->GetValue();

I know your code work; by chance, do you think it is possible to work in my case? If so, please suggest.

Thank you.
Nicole

Posts: 586
Posted: 04/01/2005, 1:44 AM

Tam,
In theory $this->FileFolder in Classes.php should return a value you assigned to it in Before Process File event. Have you tried to print a folder from Classes.php?
BTW, the code snippet I modified is in Upload function of clsFileUpload class, approximately lines 1161-1165.

_________________
Regards,
Nicole
View profile  Send private message
Tam
Posted: 04/01/2005, 11:21 AM

Here is what I do:
1) In the Classes.php file, I put
// move uploaded file to temporary folder
$file_exists = true;
$index = 0;
while($file_exists) {
$ActualFileName = $index . "." . $FileName;
$file_exists = file_exists($this->FileFolder.$ActualFileName);
$this->Errors->addError($this->FileFolder);
$index++;
}
2) In BeforeProcessFile event, I have
$Form1->FileUpload1->FileFolder = $imagelist->directory->GetValue();
$Form1->FileUpload1->Errors->addError($Form1->FileUpload1->FileFolder);

As I use ONLY one of them, the error for (1) is ./ and the error for (2) is images/Folder1/

As I use BOTH of them, only the error for (1) return. I think that means the line of code in Classes.php file executes before the one on BeforeProcessFile. For this reason, I can't get the FileFolder right.

Please tell me what you think. Thanks.
Nicole

Posts: 586
Posted: 04/02/2005, 6:18 AM

Tam,
The code is Classes.php is really executed before Before Process File event.
The workaround is to change File Folder in page’s After Initialize event.

_________________
Regards,
Nicole
View profile  Send private message
Tam
Posted: 04/02/2005, 11:21 AM

I try the following code

global $NewPage1;
// Write your own code here.
$NewPage1->Form1->FileUpload1->FileFolder = $NewPage1->Form1->directory->GetValue();

but I got an error

Fatal error: Call to a member function on a non-object in c:\inetpub\wwwroot\vietET\admin_imageupload_events.php on line 41

:-( I try the old code in Before Insert event, and I also find out that the code in Classes.php file also execute before it. Is there any other way, you think?
Nicole

Posts: 586
Posted: 04/04/2005, 1:55 AM

Tam,
If your page is not Includable page than declare form as global variable and do not use page:
  
global $Form1;  
$Form1->FileUpload1->FileFolder = “path to file folder”;  
Assuming that Form1 and FileUpload1 are real names.
In page’s After Initialize event you cannot access control values ($Form1->directory->GetValue()). They just do not exist in the moment the event is fired.

_________________
Regards,
Nicole
View profile  Send private message
Tam
Posted: 04/04/2005, 12:33 PM

It works, thanks a lot, Nicole. I really appreciate it.

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Web Database

Join thousands of Web developers who build Web applications with minimal coding.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.