This topic is locked

Changing uploaded photos names - very useful

3/10/2010 1:02:38 PM
PHPRunner Tips and Tricks
H
horsey_kim author

UPDATE: THIS worked for version 5.2 - please note that I have posted what works for version 5.3 in another posting below.
For Version 5.2
Thanks to support, user photos now make sense, for those who want to store photos in a server directory and not in the database.
The Problem: When users upload photos, the original photo name is saved. If two users have the same photo name, then the last photo writes over the first photo. You can use the date stamp feature, but I wanted to be able to look in a directory and know which photo went to which record. And with multiple photos per record, it can be really confusing.
The Quest: That each photo uploaded be automatically named after the "key column id field" and "photo field name". This would create unique names on the photo that would allow you to easily identify what record they belong to. Another benefit is when a user updates their photo the new updated photo would write over the old photo.
The Process: The "key column id field" is not created until the record is added. To have a valid "key column id field" for renaming, you need to break up the process into separate actions.
First using an Add screen have all fields you want for the record, except the photo fields.

Next when a user saves the add screen they need to be redirected to the Edit screen for that "key column id".

The Edit screen needs to have "key column id field (read only)" and your photo upload fields.

when edit screen is saved then the photos can be renamed to include the "key column id field"
The Code:
Add Page Events for After Record Added:

header("Location: );

exit();


Edit Page Events for Before Record Updated: (photo fields and READ ONLY keycolumnid field on the edit page) - Red is what you would change to your settings. Code is set up to name your photos after keycolumnid and photofield name. In my case my files are being named 12photo.jpg 12photo2.jpg and so on.



global $files_save;
[color="#008000"]// for the first photo

$ext = substr(strrchr($values[", '.'), 1);

foreach( $files_save as $key=>$val)

{

if($val["filename"]=="files/".$FILES["value[color="#FF0000"]photofieldname_1"]["name"])

$files_save[$key]["filename"] = "files/".$oldvalues["".".$ext;

}

$values["[color="#FF0000"]photofieldname"] = $oldvalues["".".$ext;
[color="#008000"]// for the second photo field starts
$ext2 = substr(strrchr($values[", '.'), 1);

foreach( $files_save as $key=>$val)

{

if($val["filename"]=="files/".$FILES["value[color="#FF0000"]mysecondphotofieldname_1"]["name"])

$files_save[$key]["filename"] = "files/".$oldvalues[".".$ext2;

}

$values["[color="#FF0000"]mysecondphotofieldname"] = $oldvalues[".".$ext2;
[color="#008000"]// second photo field ends
return true;


Special Thanks to Support for their help! You are great and greatly appreciated.

M
mickna 5/13/2011

Sorry, did not work for me....
I think in the first part (photo1) is a error, isn't it?

$files_save[$key]["filename"] = "files/".$oldvalues["fieldnameofkeycolumid"]."photofieldname.".$ext;

}

$values["photofieldname"] = $oldvalues["fieldnameofkeycolumid"]."photofieldname[color="#FF0000"]".".$ext;


should be

$files_save[$key]["filename"] = "files/".$oldvalues["fieldnameofkeycolumid"]."photofieldname.".$ext;

}

$values["photofieldname"] = $oldvalues["fieldnameofkeycolumid"]."photofieldname.".$ext;


But after redirect to the edit page and uploading images, I get this error:




I have no clue....

Any help?
thx,

mickna

H
horsey_kim author 6/2/2011

Mickna - Sorry did not respond earlier, been busy and not in the forum.
First of all:

$files_save[$key]["filename"] = "files/".$oldvalues["fieldnameofkeycolumid"]."photofieldname.".$ext;

}

$values["photofieldname"] = $oldvalues["fieldnameofkeycolumid"]."photofieldname[color="#FF0000"]".".$ext;
The " in the code is not suppose to be there. It is being generated from the forum because I added code for making the fields names in red for viewing.


Can you please copy all your event code and paste here so that I can see what all you used. Maybe something was left out. I have used this code on several sites and it has worked with Version 5.2. What version of phprunner are you using?
In version 5.3, I actually have used a different code to accomplish the same thing. So let me know what version you are using?
Thanks,
Kim

M
mickna 6/2/2011

Hi Kim,
thank you for answering.

Yes, I am using 5.3! So I think this should be the issue.

May I can ask you to provide me with the code for 5.3?
Thank you for your time. I really appreciate this <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=58666&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />
Take care,

mickna

H
horsey_kim author 6/2/2011

For version 5.3
Be sure that on the editor page you have defined your photo fields with a directory to save the files to. I used a directory called files - if not defined correctly you will get errors or a red x where the image is suppose to be on your list or view pages.
[size="3"]Photo Fields defined on Editor page[/size]
Under "View As" tab I select "Image" and then under the "URL prefix" I have

Under "Edit As" tab I select "File/Image" and under the "Upload Folder" I have [color="#FF0000"]files

Remember under the "Edit As" tab, you might want to check "Delete file when associate record is deleted (global option)

I do not check resize or thumbnails - because it can cause problems with the code. If I want to set the size of my images on the page - I go to "View As" tab and then set the Image Width and Height.
[size="3"]ADD PAGE - Events: after record added[/size]

COMMENT: Here I define the record id and then redirect it to the edit page for the actual renaming of the photo. Replace record_id with your field name and horse_edit with your edit page name.
//** Define Record ID *

$recordid = $values[";

$_SESSION['[color="#FF0000"]record_id'] = $recordid;
//****
Redirect to another page ****

header("Location: .php?editid1=$recordid");
exit();
[size="3"]EDIT PAGE - Events: after record updated[/size]

COMMENT: I include an if statement to check the status of the upload. Since I have multiple photo uploads, I want only the fields that I actually uploaded a photo to change, not ones I am not changing. I hope that makes sense. The rest of the code changes the file name in both the mysql database and the server directory. NOTE my file names end up in this format example: p10n1.jpg (p=photo, 10=record id, n1=photo number 1, then the extension). I use this format so that I can easily see what files are photos. If you want to change i,t then you need to modify [color="#FF0000"]p".$_SESSION['record_id']."n1." sections of the code. BE SURE TO MODIFY the url to match yours.
//** CHECK UPLOAD STATUS AND RENAME First Photo Upload *


if ($REQUEST["type=="upload2")

{global $pageObject;

$ext1 = substr(strrchr($values["[color="#FF0000"]photofield1"], '.'), 1);

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

{ if($pageObject->filesToMove[$key]->sourceFilename==$_FILES[");

$pageObject->filesToMove[$key]->destFilename= "p".$_SESSION['record_id']."n1.".$ext1;
$values["[color="#FF0000"]photofield1"] = "http://www.yourdomain.com/files/p".$_SESSION['record_id']."n1.".$ext1;}

}
//** CHECK UPLOAD STATUS AND RENAME Second Photo Upload *****
if ($REQUEST["type=="upload2")

{

global $pageObject;

$ext2 = substr(strrchr($values["[color="#FF0000"]photofield2"], '.'), 1);

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

{ if($pageObject->filesToMove[$key]->sourceFilename==$_FILES[");

$pageObject->filesToMove[$key]->destFilename= "p".$_SESSION['record_id']."n2.".$ext2;
$values["[color="#FF0000"]photofield2"] = "http://www.yourdomain.com/files/p".$_SESSION['record_id']."n2.".$ext2;}

}

M
mickna 6/2/2011

Hi Kim,
I only can say: Thank You SO Much!

Works great and smooth. I am really happy.

I have had some headache, as this is a function I definitely need.
Again, thank you so much for your efforts!

Have a great day,

mickna