This topic is locked

Rename uploaded file based on ID

10/26/2011 5:35:54 AM
PHPRunner General questions
T
tr4ding author

Morning all,
i'm new!!
I'd like to rename uploaded files based on the current id.....
example:
default.jpg------->21.jpg
I've used this reference :
Rename uploaded files
adapting the code to:
function BeforeAdd($values,$message,$inline)

{

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

{

if($pageObject->filesToMove[$key]->sourceFilename==$_FILES["value_image_1"]["tmp_name"])

$pageObject->filesToMove[$key]->destFilename= $values["ID"].".jpg";

}

$values["image"] = $values["ID"].".jpg";
// Place event code here.

// Use "Add Action" button to add code snippets.
return true;

}
SITUATION:
PHPRUNNER version = 6.0
Fieldname = image

ID = id

upload folder = files/
but when i insert a new record, the file is renamed to ".jpg" without any filename.....
where i'm wrong?

C
cgphp 10/26/2011
$values["ID"]

or

$values["id"]

?
If the id field is autoincrement its value will be set only after the record is added.

T
tr4ding author 10/26/2011


$values["ID"]

or

$values["id"]

?
If the id field is autoincrement its value will be set only after the record is added.


Thank you for you answer!!
Otherwise.....yes...."id" (NOT "ID") field is autoincrement.....how can i solve this issue?

I've to put that code in "After record added" event?
I've founded this post but it's not clear how can i insert that code in mine....
Thank you......

C
cgphp 10/26/2011

In the "After record added" event, enter this code:

global $conn;

db_exec("UPDATE you_table_name SET image='".$keys['id'].".jpg' WHERE id=".$keys['id'],$conn);
$old_path_to_file = '/path/to/the/'.$values['image'];

$new_path_to_file = '/path/to/the/'.$keys['id'].'.jpg';
rename($old_path_to_file,$new_path_to_file);


Change /path/to/the/ according to your image path.

T
tr4ding author 10/27/2011



In the "After record added" event, enter this code:

global $conn;

db_exec("UPDATE you_table_name SET image='".$keys['id'].".jpg' WHERE id=".$keys['id'],$conn);
$old_path_to_file = '/path/to/the/'.$values['image'];

$new_path_to_file = '/path/to/the/'.$keys['id'].'.jpg';
rename($old_path_to_file,$new_path_to_file);


Change /path/to/the/ according to your image path.


Thank you so much!! It works!!

Now i'm working to modify this function in order to insert it into the EDIT PAGE.

In fact there's another problem.....when i try to update a photo that is already uploaded, that function can't delete current photo in order to replace with the new upload.....stay in touch!!!
Thank you <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=61752&image=1&table=forumreplies' class='bbc_emoticon' alt=';)' />