Restricting to a maximum of one picture upload

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'])){
           echo' Your picture must not exceed 30k in size and be a jpg, gif,or png';
	DB::getInstance()->update('associates',$user->data()->id, array(
$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>';

I would just check a database table to see if the user has already uploaded a picture, if he or she already has then give them the option of replacing with a new picture or keeping the existing picture. An example it might look like this?

/* Check database table to see if an image has already be uploaded */
function check_for_existing_image ($imageName, $pdo) {
  try {
    /* Setup up the query, it doesn't matter if nothing is selected, for  
      we are just trying to see if an image is in the database table.
      Execute a prepared statement by passing an array of values */
    $query = "SELECT 1 FROM " . DATABASE_TABLE . " WHERE imageName = :ImageName";

    $stmt = $pdo->prepare($query);

    $stmt->bindParam(':imageName', $imageName);


    /* The fetch() method returns an array representing the "next" row from 
      the selected results, or false if there are no more rows to fetch. */
    $row = $stmt->fetch();
    /* If a row was returned, then we know an image has already been uploaded in
      the database already and we should return a true value back. */
   return $row; // True for image already uploaded and false if not (1=true, 0=false):
  } catch (PDOException $e) { // Report the Error!	
    echo "DataBase Error: Could not check imageName against database table.<br>" . $e->getMessage();
  } catch (Exception $e) {
    echo "General Error: imageName could not be checked for some general reason.<br>" . $e->getMessage();
} // End of check_for_existing_image function:

Yes, I think I’d do it in the wording where you offer the opportunity to upload a picture. When you draw that form, check if they already have a picture uploaded - if they do, the wording would be “replace your existing picture”, if they do not, it would read “upload a picture”. Then your upload routine, once it’s sure the new one is suitable, just deletes the old one and removes the need for some of the back-and-forth. Obviously you might want some kind of clear warning it will delete the old one.

thank you Pepster, I will give this a try.

thank you droppsnoot, I appreciate the logic.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.