I have a script that I use to give a user the opportunity to upload a picture. I would like to restrict file upload to only one picture per user. The idea would be to check and see if a user already uploaded a picture and give them a choice to keep the old one or replace it with a new one.
the way I setup the script for picture upload adds the user id at the beginning of the file name separated by an underscore. Example: User id = 850 picture uploaded would be saved as 850_userpic.jpg in my picture folder and name saved to my database.
I been trying on and off to revise this code so only one picture can be saved per user.
what would be the trick to restrict the number of pictures uploaded? Give the user the choice to delete old picture and save new one, or cancel the upload of a new picture?
here is the code that I use for now:
if(!empty($pic)) {
//this is the part I tried but failed------------------------------------------------------------------
//$files = array_diff( scandir(“images/associates”), array(“.”, “…”) );
//foreach($files as $file){
//if (preg_match("'/^". $user->data()->id . "_". "/'", $file)){
//javascript here
//echo 'You already have a picture on file ->'.$file;
// }
//}--------------------------------------------------------------
if ((($pic_type == 'image/jpg') ||($pic_type == 'image/jpeg') ||($pic_type == 'image/gif') ||
($pic_type == 'image/png')) && ($pic_size <= PIC_MAXSIZE) && ($pic_size > 0)){
preg_replace('#[\s\&\@\#\$\%\(\)\[\]\&]#','', $pic);
$destination = 'images/associates/';
$updatepicname= $user->data()->id . "_" .$_FILES['pic']['name'];
// move the file to the upload folder and rename it
if(move_uploaded_file($_FILES['pic']['tmp_name'], $destination . $user->data()->id . "_" .$_FILES['pic']['name'])){
}
else
{
echo' Your picture must not exceed 30k in size and be a jpg, gif,or png';
}
DB::getInstance()->update('associates',$user->data()->id, array(
'pic'=>$updatepicname));
}
else
{
$filetoobig =' <p class="error"> There was a problem uploading your picture. picture must not exceed 30K and must be in jpg, jpeg or pjpeg format</p>';
}
}
}