Welcome Guest, Not a member yet? Register   Sign In
File uploads: check if any uploaded and specify filename
#1

[eluser]Martin Penev[/eluser]
Hi there,
I'm on a upload form that should upload a file, but there are some things I haven't found yet:

1) How can I check, if a file has been uploaded? (Important if I want to program a
mandatory upload field)
2) How can I specify a filename which will be used as a filename after upload?


Have checked the user guide, but couldn't find any answer.

Thanks for your help in advance.

Martin
#2

[eluser]mjijackson[/eluser]
To tell if the file uploaded correctly, you can use a special callback function and PHP's native is_file_uploaded function. This code is just off the top of my head (so it might need some tweaks) but it should convey what I'm talking about:

In the controller:

Code:
function upload_page()
{
  $rules['my_file'] = 'callback_is_uploaded';
  $this->validation->set_rules($rules);
}

function is_uploaded($name)
{
  return is_uploaded_file($_FILES[$name]['tmp_name']);
}
#3

[eluser]BravoAlpha[/eluser]
[quote author="Martin Penev" date="1186789947"]1) How can I check, if a file has been uploaded? (Important if I want to program a
mandatory upload field)[/quote]
You can use the Upload class.

[quote author="Martin Penev" date="1186789947"]2) How can I specify a filename which will be used as a filename after upload?[/quote]
I'm trying to figure that out too.

Edit:
http://ellislab.com/forums/viewthread/45432/
http://ellislab.com/forums/viewthread/47491/
#4

[eluser]Michael Wales[/eluser]
Using the File Uploading Class return the current filename of the file on the server ($this->upload->data()).

Then use PHP's rename() function to rename it to what you want.
#5

[eluser]Martin Penev[/eluser]
Hi guys,

thanks for your replies. They have been helpful.

About solving the first problem:
==================================


Either use this function will do it for sure:
Code:
function upload_page()
{
  $rules['my_file'] = 'callback_is_uploaded';
  $this->validation->set_rules($rules);
}

function is_uploaded($name)
{
  return is_uploaded_file($_FILES[$name]['tmp_name']);
}

or you can use the error status from the $_FILES-array like this
Quote:$_FILES['userfile']['error']

Error Codes from PHP.net: Error Codes on File upload:
Value: 0; There is no error, the file uploaded with success.
Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
Value: 3; The uploaded file was only partially uploaded.
Value: 4; No file was uploaded.
Value: 6; Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.
Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0.
Value: 8; File upload stopped by extension. Introduced in PHP 5.2.0.

Solving the second problem:
===========================


walesmd has brought in the right idea, just to use the rename() function. I guess I'll put it or extend it via the "File helper". I don't fancy the idea to change the upload class
#6

[eluser]Michael Wales[/eluser]
I would definitely extend the File Helper and add in the rename function there - seems to be a great fit and I am sure you can introduce some additional functionality that could prove useful to yourself and the CI community.




Theme © iAndrew 2016 - Forum software by © MyBB