2

Am doing multiple file upload in the controller but the file doesn't get uploaded controller code: for the upload

$images = $_FILES['evidence'];
$success = null;

$paths= ['uploads'];

// get file names
$filenames = $images['name'];


// loop and process files
for($i=0; $i < count($filenames); $i++){
//$ext = explode('.', basename($filenames[$i]));
$target = "uploads/cases/evidence".DIRECTORY_SEPARATOR . md5(uniqid()); //. "." . array_pop($ext);
if(move_uploaded_file($images['name'], $target)) {
    $success = true;
    $paths[] = $target;
} else {
    $success = false;

    break;
}

echo $success;
}
// check and process based on successful status 
if ($success === true) {
        $evidence = new Evidence();
        $evidence->case_ref=$id;
        $evidence->saved_on=date("Y-m-d");
        $evidence->save();

$output = [];
} elseif ($success === false) {
$output = ['error'=>'Error while uploading images. Contact the system administrator'];

foreach ($paths as $file) {
    unlink($file);
}
} else {
$output = ['error'=>'No files were processed.'];
}

//  return a json encoded response for plugin to process successfully
echo json_encode($output);

I have tried var_dump($images['name'] and everything seems okay the move file does not upload the file

1
  • I have posted the answer.. Commented Nov 22, 2015 at 20:32

2 Answers 2

2

Check what you obtain in $_FILES and in $_POST and evaluate your logic by these result...

The PHP manual say this function return false when the filename is checked to ensure that the file designated by filename and is not a valid filename or the file can be moved for some reason.. Are you sure the filename generated is valid and/or can be mooved to destination?

this is the related php man php.net/manual/en/function.move-uploaded-file.php

Sign up to request clarification or add additional context in comments.

5 Comments

Am also getting an error when saving the path to the database after the controller returns success the other parameters of evidence are not saved. After doing var_dump($evidence->save) I get a Boolean false on firebug
Please post this last question like another question... otherwise .. is not easy ... navigate this layering of questions..
Not yet. Am trying to post another one but I have a question limit of 90 minutes
Well if you can post the question today... i can see it tomorrow morning.. comment me for remeber the question if not solved..
1

Have you added enctype attribute to form tag?

For example:

 <form action="demo_post_enctype.asp" method="post" enctype="multipart/form-data">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
</form> 

1 Comment

it has workec after checking the files at php.net/manual/en/function.move-uploaded-file.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.