Once I post the form with and image upload the first time it works. But when I do it again it gives me this error,
Undefined variable: file_destination
Here is all of my code that uses file_destination:
if(isset($_FILES['img'])) {
$file = $_FILES['img'];
// File properties
$file_name = $file['name'];
$file_tmp = $file['tmp_name'];
$file_size = $file['size'];
$file_error = $file['error'];
// Work out the file extension
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
$allowed = array('png', 'jgp', 'jpeg', 'gif');
if(in_array($file_ext, $allowed)) {
if($file_error === 0) {
if($file_size <= 180000000) {
$file_name_new = uniqid('', true) . '.' . $file_ext;
$file_destination = 'img/content-imgs/' . $file_name_new;
if (move_uploaded_file($file_tmp, $file_destination)) {
echo '<img src="' .$file_destination. '">';
}
}
}
}
}
$title = Input::get('title');
$slug = Input::get('slug');
$body = Markdown::parse(Input::get('body'));
$draft = Input::get('draft');
$created_at = date("Y-m-d H:i:s");
$updated_at = date("Y-m-d H:i:s");
$post = DB::table('posts')
->insert(array(
'title' => $title,
'slug' => $slug,
'body' => $body,
'img' => $file_destination,
'draft' => $draft,
'created_at' => $created_at,
'updated_at' => $updated_at
));
Can someone please help me understand why I'm getting this error.