CodeIgniter Forums
Manipulte uploaded file before saving - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Manipulte uploaded file before saving (/showthread.php?tid=11073)



Manipulte uploaded file before saving - El Forum - 08-25-2008

[eluser]Bursh[/eluser]
Hey

I'm trying to manipulate an uploaded files name before saving it in the folder (so that I have a regular naming convention within the folders) but I can't seem to find any function that would allow that in CI. Can anybody help me out on this one?

Regards,
Bursh.


Manipulte uploaded file before saving - El Forum - 08-25-2008

[eluser]fatnic[/eluser]
You could upload the file into a temp directory. Then do your 'manipulations', copy to permanent folder, then delete the temp file.


Manipulte uploaded file before saving - El Forum - 08-25-2008

[eluser]Bursh[/eluser]
[quote author="fatnic" date="1219696045"]You could upload the file into a temp directory. Then do your 'manipulations', copy to permanent folder, then delete the temp file.[/quote]

I'd rather not do that, unless I have to, because that's a really long way around it.


Manipulte uploaded file before saving - El Forum - 08-25-2008

[eluser]fatnic[/eluser]
Well after a quick re-read of the user guide, I can't see any way of changing the filename before you upload.

The only way I can think of to do it is not use the CodeIgniter upload library and use native PHP instead.


Manipulte uploaded file before saving - El Forum - 08-25-2008

[eluser]Bursh[/eluser]
[quote author="fatnic" date="1219700026"]Well after a quick re-read of the user guide, I can't see any way of changing the filename before you upload.

The only way I can think of to do it is not use the CodeIgniter upload library and use native PHP instead.[/quote]

I thought of that too. I guess I'll try it that way unless anybody else can come up with a way.


Manipulte uploaded file before saving - El Forum - 08-25-2008

[eluser]fesweb[/eluser]
It depends on what you mean by "before." You can rename the file without creating a temporary file to be deleted.
Code:
$base_name = 'upload'.$id;

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'pdf|doc';
$config['max_size'] = '2048';
$config['encrypt_name'] = TRUE;

$this->upload->initialize($config);
$this->fileinfo = $this->upload->data();

$base_name_full_path = $this->fileinfo['file_path'].$base_name.$this->fileinfo['file_ext'];

$rename_it = rename($this->fileinfo['full_path'], $base_name_full_path);
Does that do anything for you?


Manipulte uploaded file before saving - El Forum - 08-26-2008

[eluser]Bursh[/eluser]
[quote author="fesweb" date="1219710167"]It depends on what you mean by "before." You can rename the file without creating a temporary file to be deleted.
Code:
$base_name = 'upload'.$id;

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'pdf|doc';
$config['max_size'] = '2048';
$config['encrypt_name'] = TRUE;

$this->upload->initialize($config);
$this->fileinfo = $this->upload->data();

$base_name_full_path = $this->fileinfo['file_path'].$base_name.$this->fileinfo['file_ext'];

$rename_it = rename($this->fileinfo['full_path'], $base_name_full_path);
Does that do anything for you?[/quote]

Works an absolute charm. Thank you very much.