This topic is locked

Rename upload file based on ID field

8/23/2008 4:47:02 AM
PHPRunner General questions
K
kadidarm author

Hello,

I would like to rename upload files or image based on ID field. So I would like to rename myimage.jpg to 112.jpg where the 112 is the ID field of the record. Can we do this?
Kris

J
Jane 8/25/2008

Kris,
do you want to store files on the disc on in the database?

K
kadidarm author 8/25/2008

Kris,

do you want to store files on the disc on in the database?


In disc or folder

J
Jane 8/25/2008

Hi,
use Before record added/updated event on the Events tab for this purpose.

Here is a sample:

global $files_move;

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

{

if($val[0]==$_FILES["file_FieldName"]["tmp_name"])

$files_move[$key][1] = "files_folder/".$values["ID"].".jpg";

}
$values["FieldName"] = $values["ID"].".jpg";



where FieldName and ID are your actual field names, files_folder is directory where your files will be stored.

K
kadidarm author 8/26/2008

Hi sorry,

cant get this to work as it just keeps saving the original filename in disk.

global $files_move;

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

{if($val[0]==$_FILES["image_url"]["tmp_name"])

$files_move[$key][1] = "photo/PH".$values["employee_number"].".jpg";

}

$values["image_url"] = "PH".$values["employee_number"].".jpg";

J
Jane 8/26/2008

Hi,
this code works on my test box.

Please publish your project on Demo Account and send to support@xlinesoft.com a URL to your pages along with instructions on reproducing this error.

I'll find what's wrong with your project inspecting it at Demo account site.

A
amuro 4/10/2009

Hi sorry,

cant get this to work as it just keeps saving the original filename in disk.


I have the same problem.

I use PHPR5.1(build 1379).

I add the code "return true;" at the end, the function works. <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=40022&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />

U
unimatrix 4/16/2009



I have the same problem.

I use PHPR5.1(build 1379).

I add the code "return true;" at the end, the function works. <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=40225&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />


I'm having the same problem. It saves the correct name to the database field, but does not rename the uploaded file.

J
Jane 4/17/2009

Hi,
please see my changes below:

global $files_move;

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

{if($val[0]==$FILES["**file**image_url"]["tmp_name"])

$files_move[$key][1] = "photo/PH".$values["employee_number"].".jpg";

}

$values["image_url"] = "PH".$values["employee_number"].".jpg";

B
bussb 4/23/2009

Hi,

please see my changes below:


In this example, the admin specified that the file type was JPG. What if you are not sure what type of file will be uploaded, but still want to name the file with the index field? For example, if the index is 1234, then the user could upload a .pdf file, a .jpg file, a .doc file etc. Is there a method to name the file 1234.pdf or 1234.jpg or 1234.doc once the file type is known?
Brian

J
Jane 4/27/2009

Brian,
you can check image extension using following code:

$ext = CheckImageExtension($_FILES["file_FieldName"]["name"]);