This topic is locked
[SOLVED]

 Upload folder name change on IIS

11/7/2011 3:20:50 PM
PHPRunner General questions
F
fmbma author

I followed the example in Help to save documents to subfolders with the userID or other variables. On IIS, the file is still saved in root files folder. Even if I create the subfolder first, it doesn't work. No errors are generated. is the syntax different for IIS? I tried changing the / to \\ but that didn't work either. This is in the latest build of PHPRunner 6

thanks,

Fred

C
cgphp 11/7/2011

Post the code.

Admin 11/7/2011

Fred,
do you use any code in your BeforeAdd/BeforeEdit event that might change the uploaded file name or location.

F
fmbma author 11/7/2011



Fred,
do you use any code in your BeforeAdd/BeforeEdit event that might change the uploaded file name or location.


This is in the Before Record Added event on the Add page
DocumentName is the name field, DocID is the Key field.


global $pageObject;

foreach( $pageObject->filesToMove as $key=>$val)

{

if($pageObject->filesToMove[$key]->sourceFilename==$_FILES["value_DocumentName_".postvalue("id")]["tmp_name"])

{

//create directory if don't exist

$dirname= "files/".$_SESSION["MasterAcctCode"];

if (is_dir($dirname)) {

} else {
mkdir("files/".$_SESSION["MasterAcctCode"], 0777);

}

$pageObject->filesToMove[$key]->destFilename =
$_SESSION["MasterAcctCode"]."/".$values["DocID"];
}

}
$values["DocumentName"] = $values["DocID"];

return true;
Admin 11/7/2011

As a first step I would try to make sure that $_SESSION["MasterAcctCode"] variable is not empty. Hard to tell what's else might be causing this issue.

F
fmbma author 11/8/2011

The session variable holds the correct information.



As a first step I would try to make sure that $_SESSION["MasterAcctCode"] variable is not empty. Hard to tell what's else might be causing this issue.

Admin 11/8/2011

You need to troubleshoot this code.
Add several echo statements to make sure this code is executed, variables are populated properly etc.

F
fmbma author 11/9/2011

OK, I stepped through.

This line does not test true:

if($pageObject->filesToMove[$key]->sourceFilename==$_FILES["valueDocumentName".postvalue("id")]["tmp_name"])
I'm not exactly sure what the correct syntax is for this, or what exactly it's doing. DocumentName is the database field that holds the document name. I substituted it per the example in the tutorial:

if($pageObject->filesToMove[$key]->sourceFilename==$_FILES["valueFieldName".postvalue("id")]["tmp_name"])
thanks,

Fred

Admin 11/9/2011

FieldName is the name of the field that stores the name of uploaded file, the one you setup as File.
Make sure that field name is DocumentName (case-sensitive).
Another thing that is not related to the current issue but still required to make it work. If DocID is your key column, instead of $values["DocID"] you need to use $keys["DocID"].
If nothing helps post your application to Demo Account and open a ticket at http://support.xlinesoft.com sending your Demo Account URL. 'Demo Account' button can be found on the last screen in the program.

F
fmbma author 11/9/2011

Sergery,

Thank you for you help and patience. Sometimes it's hard for this old VB programmer to get the PHP syntax. My mistake - I was using the field for the friendly Document Name, rather than the File field that holds the actual document. So i've made some progress, and it creates the subfolder now. However, I get this error:
move_uploaded_file(): The second argument to copy() function cannot be a directory
Here is the call stack:
[size="2"]Call stack[/size][size="2"] [/size][size="2"]File: line[/size][size="2"]Function[/size][size="2"]Arguments[/size][size="2"]#0. [/size][size="2"]include\phpfunctions.php:421[/size][size="2"]runner_error_handler[/size][size="2"]1. 2;

2. move_uploaded_file(): The second argument to copy() function cannot be a directory;

3. C:\inetpub\wwwroot\donoraction\donorsys1\include\phpfunctions.php;

4. 421;

5. Array ( [source] => C:\Windows\Temp\php1912.tmp [dest] => C:\inetpub\wwwroot\donoraction\donorsys1/files/demo/ ) ;[/size][size="2"]#1. [/size]size="2":0[/size][size="2"]move_uploaded_file[/size][size="2"]1. C:\Windows\Temp\php1912.tmp;

2. C:\inetpub\wwwroot\donoraction\donorsys1/files/demo/;[/size][size="2"]#2. [/size][size="2"]include\phpfunctions.php:421[/size][size="2"]runner_move_uploaded_file[/size][size="2"]1. C:\Windows\Temp\php1912.tmp;

2. C:\inetpub\wwwroot\donoraction\donorsys1/files/demo/;[/size][size="2"]#3. [/size][size="2"]classes\files.php:29[/size][size="2"]MoveFile->Move[/size][size="2"]N/A[/size][size="2"]#4. [/size][size="2"]classes\runnerpage.php:2499[/size][size="2"]RunnerPage->ProcessFiles[/size][size="2"]N/A[/size][size="2"]#5. [/size][size="2"]include\commonfunctions.php:4864[/size][size="2"]DoInsertRecordSQL[/size][size="2"]1. tblcampaigndocs;

2. Array ( [DocumentName] => test 667 [DocDescription] => [File] => [MasterAcctCode] => demo [CampaignID] => 2 ) ;

3. Array ( ) ;

4. 1;

5. AddPage;[/size][size="2"]#6. [/size][size="2"]include\phpfunctions.php:867[/size][size="2"]DoInsertRecord[/size][size="2"]1. tblcampaigndocs;

2. Array ( [DocumentName] => test 667 [DocDescription] => [File] => [MasterAcctCode] => demo [CampaignID] => 2 ) ;

3. Array ( ) ;

4. 1;

5. AddPage[/size];
Here is the code:


global $keys;

global $pageObject;

foreach( $pageObject->filesToMove as $key=>$val)

{
if($pageObject->filesToMove[$key]->sourceFilename==$_FILES["value_File_".postvalue("id")]["tmp_name"])

{

//create directory if don't exist

$dirname= "files/".$_SESSION["MasterAcctCode"];
if (is_dir($dirname)) {

} else {
mkdir("files/".$_SESSION["MasterAcctCode"], 0777);

}

$pageObject->filesToMove[$key]->destFilename =
$_SESSION["MasterAcctCode"]."/".$keys["DocID"];

}

}
$values["File"] = $keys["DocID"];

return true;




FieldName is the name of the field that stores the name of uploaded file, the one you setup as File.
Make sure that field name is DocumentName (case-sensitive).
Another thing that is not related to the current issue but still required to make it work. If DocID is your key column, instead of $values["DocID"] you need to use $keys["DocID"].
If nothing helps post your application to Demo Account and open a ticket at http://support.xlinesoft.com sending your Demo Account URL. 'Demo Account' button can be found on the last screen in the program.


Admin 11/9/2011

global $keys; is not required here, $keys is a parameter in this event.

F
fmbma author 11/10/2011

I get an undefined variable error if I don't declare it. I've uploaded to the demo account. It's a somewhat stripped down version of the project without security, so I've hard coded the session variable. Go to "Campaigns" then "Campaign Docs" for the upload.

thank you!



global $keys; is not required here, $keys is a parameter in this event.

Admin 11/10/2011

A question - is DocID an autoincrement field? If yes, it won't be available in BeforeAdd event.

F
fmbma author 11/10/2011

Yes it is autoincrement. I was wondering about that. What should be used in before Add vs. Update?
Fred



A question - is DocID an autoincrement field? If yes, it won't be available in BeforeAdd event.

Admin 11/10/2011

I guess you can use some other way to name file, since it's being moved to a separate folder anyway. You can keep the original file name for example.

F
fmbma author 11/10/2011

The example in Help is not finished code - it just points out a simple approach to doing it. It would be nice if there was more complete documentation on the file upload process, unless I just haven't seen it. Here is code that I got working. It saves the file in the subfolder defined by the value in $_SESSION["MasterAcctCode"].


global $pageObject;

foreach( $pageObject->filesToMove as $key=>$val)

{
if($pageObject->filesToMove[$key]->sourceFilename==$_FILES["value_File_".postvalue("id")]["tmp_name"])

{

//create directory if don't exist

$dirname= "files/".$_SESSION["MasterAcctCode"];
if (is_dir($dirname)) {

} else {
mkdir("files/".$_SESSION["MasterAcctCode"], 0777);

}
$extension = pathinfo($_FILES["value_File_".postvalue("id")]["name"], PATHINFO_EXTENSION);
$pageObject->filesToMove[$key]->destFilename =

$_SESSION["MasterAcctCode"]."/".$_FILES["value_File_".postvalue("id")]["name"]."-".date('ymdhis', strtotime('now')).".".$extension;
}

}
$values["File"] = $_SESSION["MasterAcctCode"]."/".$_FILES["value_File_".postvalue("id")]["name"]."-".date('ymdhis', strtotime('now')).".".$extension;

return true;


It adds a timestamp to the document name as well as retaining the original file extension. So it will work with document uploads other than .jpg as shown in the Help example. There are lots of things that can be done to make file upload more sophisticated, but this provides a basic solution.
Sergey - Thanks for your help and patience as I worked through this.



I guess you can use some other way to name file, since it's being moved to a separate folder anyway. You can keep the original file name for example.