Hello. I’m using Sitepoint’s PHP Novice To Ninja book as a reference to build my own website framework.
I’m trying to add functionality to upload images, which I’ve done. However I was trying to move the PHP code into a reusable ‘framework’ function and have run into difficulties.
I have a form that someone adds a name and picture to and the code uploads the images and stores a refernce to it in the database.
I had it working on on my controller like this:
Blockquote
public function add() {
$author = $this->authentication->getUser();
$item = $_POST['item'];
//the above is from form, below is others
$item['authorId'] = $author['id'];
//upload files
$file = $_FILES['file'];
//print_r($file);
//die;
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png', 'pdf');
if (in_array($fileActualExt, $allowed)){
if($fileError === 0){
if ($fileSize < 500000) {
$fileNameNew = $item['itemPicture'].'.'.$fileActualExt;
$fileDestination = 'uploads/'.$fileNameNew;
move_uploaded_file($fileTmpName,$fileDestination);
$item['itemFileName'] = $fileNameNew;
} else {
echo 'Your file was too big! Reduce size to less than 500kb';
}
} else {
echo 'There was an error uploading your file';
}
} else {
echo 'This is not an allowed filetype! Convert to jpg or png';
}
//end upload files
$this->itemsTable->save($item);
header('location: /item/list');
}
This worked fine. So then I tried to move most of that code to my DatabaseTable of functions (where the save() function is) and call it like so:
Blockquote
public function add() {
$author = $this->authentication->getUser();
$item = $_POST['item'];
//the above is from form, below is others
$item['authorId'] = $author['id'];
//upload files
$this->itemsTable->upload($item['itemPicture']);
//end upload files
$item['itemFileName'] = $fileNameNew;
$this->itemsTable->save($item);
header('location: /item/list');
With the following function in the DatabaseTable file:
Blockquote
public function upload($value) {
$file = $_FILES['file'];
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png', 'pdf');
if (in_array($fileActualExt, $allowed)){
if($fileError === 0){
if ($fileSize < 500000) {
$fileNameNew = $value.'.'.$fileActualExt;
$fileDestination = 'uploads/'.$fileNameNew;
move_uploaded_file($fileTmpName,$fileDestination);
} else {
echo 'Your file was too big! Reduce size to less than 500kb';
}
} else {
echo 'There was an error uploading your file';
}
} else {
echo 'This is not an allowed filetype! Convert to jpg or png';
}
return $fileNameNew;
}
I get undefined variable errors for $fileNameNew after the upload function completes. The file uploads, the database is updated with the new entry with the exception of the itemFileName field