while trying to allow users to upload images of theirs into my site, I did this(Using the pattern taught in php: noviceToNinja 6th edition coauthored by @kevinyank and @TomB).
And in the registration controller, I did this(after frustrated, just to make sure it upload successfully), and it dosent work…
$file = $_FILES['user']['pic']['name'] . time() . $_SERVER['REMOTE_ADDR'] . '.jpeg';
//$user['pic'] = $file;
//$destination = 'C:/me/ijdb/Project/public/imgs/';
$filename = 'C:\me\ijdb\Project\public\imgs\\' . $file;
if (!is_uploaded_file($_FILES['user']['pic']['tmp_name']) or !copy($_FILES['user']['pic']['tmp_name'], $filename)){
$valid = false;
$errors[] = "Could not save file as $filename!";
}
NB: just extract the image section as it dosen’t work and I thought I have to use $_FILES['user']['pic']['tmp_name'] to extract or get the neccessary info just as plain text like $user['username'](will get what is in the username textarea).
You should use move_uploaded_file instead of copy. Otherwise, check is_uploaded_file is returning true, if not, use var_dump($_POST) to check that the file is being uploaded correctly.
thanks all. It now working but the only problem is it doesn’t move the file to the imgs folder which is located inside the public folder but put it in the public folder. How can I go about it
As $destination is the destination filename, not just the folder, it seems that you are asking it to move the image into a file called “c:\me\ijdb\project\public\imgs”, not into a folder of that name.
Warning: move_uploaded_file(c:/me/ijdb/Project/public/imgs/tmp/phpiUJIbr): failed to open stream: No such file or directory in /home/vagrant/Code/Project/classes/Ndb/Controllers/Register.php on line 128